# Accuracy of psychopy keystroke duration measurement

I am using Psychopy and this code for measuring keystroke duration as suggested in this answer for my previous question:

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

io.clearEvents('all')

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 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 compare 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:

1. Does anyone know, what the reason might be that approximately every tenth measurement has higher signal processing times?
2. How could I make the measurement of the keystroke duration more accurate?
3. 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?
• This is a really good question and most welcome here! However, in case you do not immediately get answers here, I recommend you to ask a similar question (rephrased to focus more on the specific technology) on Stack Overflow. If you get a response there, of course you could add your own answer here! Sep 9 '17 at 11:03
• What exactly is iohub or pyglet? What is the accuracy of 16.67 ms? I mean what did you measure and how do you define accuracy? And you say ...the measured data is somewhere around 8 ms - what data? What did you measure? The question is quite unclear at this stage. Also, there are many questions buried in the post. What is it you need to know? Please be clear and explicit.
– AliceD
Sep 9 '17 at 20:26
• I've tried to rephrase the question and make the information more clear. Thank you Steven Jeuris for the suggestion, that will be my second step! Sep 9 '17 at 20:47
• Thanks for this. So you are basically asking what the reason might be that ...[A]pproximately every tenth measurement I have 24-36 ms signal processing ?
– AliceD
Sep 9 '17 at 21:39
• the camera with this framerate are just a way to test the measurement (but the camera is not so accurate because of the low framerate), to see if what I measure is correct I use the camera, because at the moment I don't have any other reliable method and because I dont know how long it was actually pressed, thats why I am recodring the pressing of the key with the camera and comparing it to the measured values with my device. And approximately every tenth key press, there are longer times measured than the camera records. Sep 10 '17 at 17:38

• One would be to change the code: take out the print command, no redefining of variables
• USB, USB device, the python and system libraries should be the newest and fastest, if possible, because these could have an influence on the accuracy.
• And that Psychtoolbox forum could have the solution, but so far I haven't found it.
• This is sadly a total absence of response. Referring to external sources without checking is not a good contribution to this site. Oct 16 '17 at 0:03
• This is not my answer, as stated, and therefore also not checked as solving the problem. Following Steven Jeuris advice I've asked the question in a different forum and just summarized the answer I've received here, to share this bit of information or to trigger some ideas. Perhaps a comment would have been better, but the question above had so many comments, that I thought it would be too confusing Oct 17 '17 at 14:43

This frankly should have been asked on SO as it's 99% a programming question. There are a lot of things that can contribute to the "spiky" accuracy you've seen:

• language/runtime: Python is hardly a good idea for anything requiring real-time accuracy. It's an interpreted language with garbage collection. The [re]assignments in your loop will (eventually) trigger that. I've written once a Python GTK interface for a real-time device, but the code controlling the device was entirely in C, and nothing time-critical was done on Python side.

• Ultimately, the C runtime dynamic memory allocations (which Python eventually relies on) are also not predictable in terms of timing, although this is probably not what you're seeing. For real-time software dynamic allocation is generally avoided.

• operating system: not running at high enough process/thread priority so you get occasionally preempted by other processes/threads.

• hardware/driver: USB sends packetized data at limited poll rate; 8ms (125 Hz) being the default for most input devices. Unless you can change the default, a PS/2 keyboard is typically faster; see table 2 in Shimizu 2002; the worse standard deviation (SD) for response time seen there for a USB keyboard was around 10ms, which seems less than what you saw, so you're probably having additional issues. Only one PS/2 keyboard exceed 3ms SD (by a little).

• the microcontroller (or possibly additional chips, e.g.) in your keyboard implement something called debouncing, which smoothes out the presses and releases to compensate for the mechanical characteristics of the keyboard's switches. Without this, you'll garbage presses as in the case of some Dell laptops some years ago. Debounce applies both to key presses and releases. For most keyboards debounce will not be configurable or very easy to find out what the settings are.

From an engineering perspective, the first thing to ask how good of an accuracy do you actually need. And for a science perspective, what do you need this measurement for? Your question smacks of a big xy problem.