Tracking Key release

Is there a built in object like the keyboard component, that lets me also track, wheter the last pressed key is still pressed or already released, or do i have to code that myself?

Using iohub you can get the release times as well but not from the standard/simple event.getKeys()

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

Hi,

I was also trying to do something similar - has there been any progression on getting psychopy to detect keyboard release events?

By “progress” you mean doing it with psychopy’s own keyboard events? No. The Pyglet trick still works, though.

Just for people who keep finding this post, an update:

Nowadays, PsychoPy can detect whether a key has been released, using its new Keyboard hardware class, which is meant to supersede the old event module and its event.getKeys() and event.waitKeys() functions:

https://www.psychopy.org/api/hardware/keyboard.html

1 Like