Tracking Key release

I’ve found a way to do this without iohub (which I still have trouble with on my mac, the permissions thing has nearly bricked my laptop twice now). I sadly don’t have the original reference but I picked this up from a thread in the old google group and digging through some of the pyglet documentation.

Basically when you create your stimulus window, which by default is a pyglet window, you can bypass PsychoPy’s event system and get pyglet to pass keypress events directly to you. Declare your window with the usual win=visual.Window and then you just need three lines of code:

key=pyglet.window.key
keyboard = key.KeyStateHandler()
win.winHandle.push_handlers(keyboard)

Now, to determine whether a given key is being pressed, you can just use statements like:

if keyboard[key.A]:

The keys are pretty transparent. A=a, B=b etc. This value will return true if the key is being down at that moment (i.e. it’s not a key buffer), and false otherwise. I’m not sure if you can have this and use getKeys at the same time or if they will conflict with each other.

EDIT: Almost forgot, this requires you to import pyglet in order to work.

4 Likes