Figuring out a way to track key releases

I am working on an experiment in PsychoPy where I track key presses and releases to make an object move. While a certain key is pressed, a dial of a circle slowly turns, until that key is released: then the moving stops and the response is made.

To track key responses, I used pygame and it’s event.get() function to see which key is pressed and whether it is released. This gave me two issues: when the window type is set to pyglet, the experiment works but sometimes the key responses (both presses and releases) are somehow missed. I figured out that I can solve this issue by changing the window type to pygame, but that gives me the second issue: I can no longer present a visual.textStim() to the screen.

Ideally, I would want to get rid of using pygame to track the key responses, and I found out on this forum that you can do so using a new keyboard module that can also check for key releases, using Psychtoolbox, but I cannot seem to get that installed. @Michael

Yes, you should avoid pygame entirely. In PsychoPy, to track both key up and key down you would need to use the relatively new Keyboard object, rather than the older event module. The Keyboard object has been ported from the PsychToolbox project and is bundled with PsychoPy. You shouldn’t have to install anything extra, if you are using one of the latest PsychoPy releases. All that is required is:

from psychopy.hardware import keyboard

From PsychoPy’s Coder view, you can go to the Demos menu and select “input ->” for an example of how to use it, and docs are here:

Hi Michael, when I try to run something with the new keyboard object, I get the error:
0.1354 WARNING Import Error: No module named ‘psychtoolbox’. Using event module for keyboard component.

Hi There,

This means that you do not have psychtoolbox installed. If you have a recent version of psychtoolbox this should be installed as part of the standalone, if you are running from a different IDE though you may want to install psychtoolbox.

As a side note that may be of help, I also have used tasks where we wanted to track a key release, this post might be of help Measuring key lift time relative to stimulus onset

Hope this helps,

Hi Becca,

Thank you for your response. The problem is that I cannot seem to get Psychtoolbox installed. I tried installing it in my terminal, and then the “no module psychtoolbox” error disappears but I get: zsh: illegal hardware instruction python.

How can I install a version of PsychoPy that just has Psychtoolbox included? Or any other ideas on how I could get this working? Perhaps using anaconda instead?


The standalone should come with psychtoolbox included, but this may depend on the version of psychopy you are running. One thing is, this message is really just a warning (but your keyboard should still work There is a problem after running experiment: No module named 'psychtoolbox')

Please could you share with us some details on:
what version of psychopy you are running
what operating system you are on

If you open your programme files C:\Program Files\PsychoPy3\Lib\site-packages do you find a folder called “psychtoolbox” in there?

For context, please could you let us know whether this is an experiment you will eventually take online (in which case an easier solution could be to track a mouse release if you are tracking the release of a single button :slight_smile:


Yeah, it’s true that it is a warning, but I cannot track key releases because it uses the older “events” module instead.

I’m running the standalone PsychoPy v.2020.2 (I’m starting to suspect this is not actually PsychoPy 3?) on macOS 10.15.5.

What is the alternative of this - C:\Program Files\PsychoPy3\Lib\site-packages - on a mac? If I just search all documents I find a folder called “psychopy” but don’t see a Lib folder.

It’s not an online experiment, and I need to track two buttons, Z to go turn a dial leftward, M to turn a dial rightward.

on mac you should be able to right click the app icon and view package content - from there you should be able to navigate to site-packages to check if psychtoolbox is contained within there.

Ok good to know for context that mouse responses can’t be used!

I think I just managed to solve the issue by creating a psychopy environment in anaconda! Still not sure why it wouldn’t work the other way but at least it’s solved!

I basically followed the instructions above. But had to remove the .txt extension from the “environment file”.

Ah pleased to hear you resolved the issue! sorry that you encountered this error but it is good to hear you found a solution - I will log this thread for future users.


Thanks for your help (and fast replies!)