I am searching for freeware to measure the duration of a keystroke or mouse-click, both would work. Does anyone have an idea?
Matlab toolboxes are, unfortunately, of no use to me.
I am searching for freeware to measure the duration of a keystroke or mouse-click, both would work. Does anyone have an idea?
Matlab toolboxes are, unfortunately, of no use to me.
Psychopy (http://psychopy.org) can do this. It can do it in two ways.
Using iohub. Iohub is a crazy powerful way to get any information about any kind of input to your system, from keyboard, mouse, eye-trackers, etc. You can see the documentation for keyboard here.
from psychopy.iohub import launchHubServer
io = launchHubServer()
keyboard = io.devices.keyboard # handy name
print 'start pressing a key now'
key_info = keyboard.waitForReleases()[0] # Just grab first release, even if there are multiple
print 'Press time was', key_info.duration
print '... and BTW, all key event info was', key_info
Using pyglet:
from psychopy import visual, event, core
win = visual.Window() # Disadvantage: you need to have a window open to record it.
clock = core.Clock()
# Start listening
print 'start pressing a key now'
key, time = event.waitKeys(timeStamped=True)[0]
clock.reset() # Key down time
while event.getKeys():
pass
print 'key lasted', clock.getTime(), 'seconds'
If measuring key strokes is the only thing you need, psychopy may be a bit heavy since it is meant for running full experiments, but it will do the job!
from psychopy.iohub import launchHubServer it works great. Thanks.
– Nathalie
Jun 10 '17 at 00:51
Thanks to Jonas Londelov, I've been able to rewrite the script for measuring multiple keys until esc is pressed and writing the key duration, the key and the info into an excel sheet. It might not be very good written I a python beginner, but it is useful. Perhaps someone else will need it.
from __future__ import division
from psychopy import visual, event, core, data, iohub
from psychopy.iohub import launchHubServer
io = iohub.launchHubServer
import csv
io = launchHubServer()
keyboard = io.devices.keyboard # handy name
count = 0
keyinfo = ""
duration = 0
key = ""
time = core.getTime()
date = data.getDateStr(format='%Y_%b_%d_%H%M')#(format="%Y_%m_%d %H:%M (Year_Month_Day Hour:Min)")
io.clearEvents('all')
win = visual.Window([400, 400])
msg = visual.TextStim(win, text='press a key\n < esc > to quit')
msg.draw()
win.flip()
datafile = open("touch duration.csv", "wb") #$not overwriting but longer
writer = csv.writer(datafile, delimiter=";")
# create output file header
writer.writerow([
date,
"round",
"keyinfo",
"duration",
"key",
])
while key not in ['escape', 'esc']:
key_info = keyboard.waitForReleases()[0] # Just grab first release, even if there are multiple
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
writer.writerow([
"",
count,
keyinfo,
duration,
key,
])
from psychopy import visual, event, core, data, iohub and then do io = iohub.launchHubServer. Second, no need to call win.close, data.file.close and core.quit. This will happen automatically when the script closes. Third, you re-define some variables, e.g. time and date,key etc. Just delete the first one since it is "overwritten" anway.
– Jonas Lindeløv
Jun 12 '17 at 12:10
If accuracy is important, perhaps try the GPL'd TScope, which is a C library; there's a paper about it (alas for old version) with a moderate number of citations. It's also maintained (current version is 5) and they have an example measuring keypress duration. It's easy enough to use as they pack everything you'd need on Windows, including a customized version of Notepad++ with shortcuts for compiling and running a program. The odd thing is that the examples from the website seem missing from the 145Mb package, but they work compiled with it.
I don't have any high-speed camera to test the accuracy, but from a quick test on myself, I didn't see any large deviations. Using a long-stroke PS/2 keyboard I get around 95ms press time (that seem on par with the paper you mentioned). With a short stroke USB keyboard , I get as low as 55ms, if I focus specifically on just making short strokes (and this does seem to come at the cost of reaction time, I need sorta "prepare myself" to make a really short stroke; in normal typing I'm still in the habit of pressing longer, even though this keyboard doesn't need it). Here's an example of the later: