| Reference | Downloads | Github

Accuracy of psychopy keystroke duration measurement



I am using Psychopy and this code for measuring keystroke duration as suggested in this answer for my question “Freeware for measuring keystroke duration” Therefore this question was also asked in the cognitive science stackexchange some days ago without any luck, but hopefully I can find the experts concerning this topic here.

from psychopy.iohub import launchHubServer 
io = launchHubServer()
keyboard = io.devices.keyboard
count = 0
keyinfo = ""
duration = 0
key = ""


print 'begin'

while key not in ['escape', 'esc']:
    key_info = keyboard.waitForReleases()[0] 
    count += 1
    key = key_info.key
    keyinfo = key_info
    duration = key_info.duration
    key = key_info.key
    print 'round number:' , count
    print key_info
    print 'Press time was', key_info.duration
    print 'key', key_info.key

The first tests look promising, but I was wondering if there is a difference in accuracy between iohub or pyglet function in psychopy? Or if there would even be a more accurate solution than using one of theses two? And how is the measurement of the keystroke duration. I’ve been searching for it but haven’t found any data for windows. The only thing I found are these tests!topic/psychopy-dev/u3WyDfnIYBo but I don’t know, how the measurements changed with further development of psychopy, and how exactly these command behave on windows. Has someone stumbled across a publication regarding the accuracy?

Moreover I don’t know how good RAM and CPU would need to be, right know I am using an Asus notebook with Intel Core i7 2,6 GHz and 16GB RAM, (Bluetooth, Ethernet. Wi-Fi, Mobile and other kind of connections disabled; desktop visual effects disabled; antivirus, software updates, background programs, and other kind of asynchronous events sources disabled) and the last version of PsychoPy2.

I tried to do preliminary tests to measure the accuracy of my test and to measure the keystroke duration with psychopy and my keyboard simulating device. Before renting more expensive equipment, I’ve tried to pretest the accuracy with a 60 fps camera recording the moment of touch and without touch to the device and measuring with psychopy at the same, to be able to campare these to values to have the accuracy. The 60 fps means, that in between frames there are 16.67 ms. My cheap camera pretest showed, that most of the processing time would be a maximum of approximately 8ms, so that I think I will achieve even lower values with better recording equipment with a higher framerate. But approximately every tenth measurement I have 24-36 ms signal processing.

My questions summarized:

  • Does anyone know, what the reason might be that approximately every tenth measurement has higher signal processing times?
  • How could I make the measruement of the keystroke duration more accurate?
  • Will saving the data into an excelsheet change the accuracy?
  • Has anyone measured the keystroke duration accuracy of psychopys2 different functions (e.g iohub and pyglet) in windows himself or stumbled upon literature about this?

Help would be much appreciated!


I don’t have an answer, but here is some advice:

• As a first step, try removing the print statements for your timing test. Instead, store timestamps in an array and print them after the test. As a general rule, timing tests which do as little work as necessary when measuring times are good.

• Your example code could be simplified, some variables are re-defined and some defined unnecessarily. Those issues should not introduce significant latencies, but they make your example more confusing than it needs to be.

• The only things that should matter with respect to getting accurate USB timestamps should be the USB ports version (1, 2 or 3) the underlying system APIs or libraries which you call from Python, the Python module which could introduce latencies (for example if it has its own internal event timer), and your USB input device. Also your own code and if your process/thread which reads the ports has priority. CPU and RAM should have nothing to do with it, unless you have something ridiculously low end, for example you are so low on memory the system is paging.

• Per the previous point, if you are reporting timing results, its good to describe all of the relevant conditions, such as the USB version, keyboard model, etc because your results depend on those; There are apparently fast and slow keyboards, or at least gamers seem to think so.

• I recently needed subject input from a gamepad and used a thread blocking on synchronous input from HIDAPI, expecting that should be low-latency. Alternate APIs for reading gamepads, PyGame and Pyglet, are both event based. I didn’t know what was going on inside of those and what latencies those event systems introduced. Note I have not actually measured any times yet so I am not certain that the expectation about blocking on HIDAPI input being fast is correct.

• Over on the Psychtoolbox Forum Denis Pelli points out from time-to-time that variance in subject response times is large relative to variance in USB response button latencies, and that therefore assumed requirements for super-precise input timing are often wrong. Be sure that you really do require the timing precision which you are after.

• Also, check to see what the Psychtoolbox does, the problem might already be have been studied and solved there and could be copied to Python.




No. Unless you are doing that between collecting keypresses, which you should not. Store data in arrays during an experiment, after the experiment dump the data from arrays to files.




Thank you AllenIngling,
the advice was very helpful! I will change the code according to your suggestions . Concerning the USB, unfortunately I am using USB 2.0 because the device is only supporting 2.0. I don’t have other specifications. The keyboard is not a real keyboard, but the custom-built-device behaves like one. I will be able to test it seperately.
So far I haven’t found the solution on Psychtoolbox, if I find something I will post it here.