8
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ A shame Matlab is of no use to you - The PTB-3 toolbox contains functions specifically to record keystrokes, even allowing for quite accurate reaction time measurements on a Windows OS. There are other users on this site that may be of help though. Let's see what comes up! $\endgroup$ – AliceD Jun 9 '17 at 18:48
  • $\begingroup$ thanks a lot! Its at least good to know that this would work, if no one else has an idea. $\endgroup$ – Nathalie Jun 9 '17 at 18:51
  • $\begingroup$ What would this be used in studying psychophysics? $\endgroup$ – Ooker Jun 19 '17 at 20:39
  • 1
    $\begingroup$ It can be used for things that have been worked on in this article "Typing keystroke duration changed after submaximal isometric finger exercises" or I've seen also writing speed for each character. I am using it for something different, my device codes stimuli as keyboard strokes, therefore I can distinguish different stimuli and measure the time of application. $\endgroup$ – Nathalie Jun 20 '17 at 8:27
  • 1
    $\begingroup$ @AliceD I will give you an upvote after you answer and clean up these comments ;) $\endgroup$ – StrongBad Dec 30 '17 at 19:26
6
$\begingroup$

Psychopy (http://psychopy.org) can do this. It can do it in two ways.

  1. 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
    
  2. 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!

$\endgroup$
  • 2
    $\begingroup$ I was just starting to answer this question with reference to PsychoPy :) +1 $\endgroup$ – AliceD Jun 9 '17 at 19:20
  • 2
    $\begingroup$ Thank you! Tried the pyglet solution first, because opening a new window is even better for me. It works perfectly! $\endgroup$ – Nathalie Jun 9 '17 at 19:51
  • 1
    $\begingroup$ while trying the first one an error occured about launch Hub server. After including from psychopy.iohub import launchHubServer it works great. Thanks. $\endgroup$ – Nathalie Jun 10 '17 at 0:51
  • $\begingroup$ I was wondering is there a difference in accuracy between these two solutions? I've been searching for it but haven't found any data for windows. Does anyone know something about this? $\endgroup$ – Nathalie Aug 2 '17 at 2:03
2
$\begingroup$

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,
    ])
$\endgroup$
  • 1
    $\begingroup$ You can simplify this. First, just one import line: 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. $\endgroup$ – Jonas Lindeløv Jun 12 '17 at 12:10
  • $\begingroup$ Thank you I've edited the answer to inlcude your corrections. $\endgroup$ – Nathalie Jun 14 '17 at 23:56
1
$\begingroup$

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:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.