To handle input from keyboard (supersedes event.getKeys)

The Keyboard class was new in PsychoPy 3.1 and replaces the older event.getKeys() calls.

Psychtoolbox versus event.getKeys

On 64 bits Python3 installations it provides access to the Psychtoolbox kbQueue series of functions using the same compiled C code (available in python-psychtoolbox lib).

On 32 bit installations and Python2 it reverts to the older psychopy.event.getKeys() calls.

The new calls have several advantages:

  • the polling is performed and timestamped asynchronously with the main thread so that times relate to when the key was pressed, not when the call was made

  • the polling is direct to the USB HID library in C, which is faster than waiting for the operating system to poll and interpret those same packets

  • we also detect the KeyUp events and therefore provide the option of returning keypress duration

  • on Linux and Mac you can also distinguish between different keyboard devices (see getKeyboards())

This library makes use, where possible of the same low-level asynchronous hardware polling as in Psychtoolbox

Example usage

from psychopy.hardware import keyboard
from psychopy import core

kb = keyboard.Keyboard()

# during your trial
kb.clock.reset()  # when you want to start the timer from
keys = kb.getKeys(['right', 'left', 'quit'], waitRelease=True)
if 'quit' in keys:
for key in keys:
    print(, key.rt, key.duration)

Classes and functions

class psychopy.hardware.keyboard.Keyboard(deviceName=None, device=-1, bufferSize=10000, waitForStart=False, clock=None, backend=None)[source]
class psychopy.hardware.keyboard.KeyPress(code, tDown, name=None)[source]

Class to store key presses, as returned by Keyboard.getKeys()

Unlike keypresses from the old event.getKeys() which returned a list of strings (the names of the keys) we now return several attributes for each key:

.name: the name as a string (matching the previous pyglet name) .rt: the reaction time (relative to last clock reset) .tDown: the time the key went down in absolute time .duration: the duration of the keypress (or None if not released)

Although the keypresses are a class they will test ==, != and in based on their name. So you can still do:

kb = KeyBoard()
# wait for keypresses here
keys = kb.getKeys()
for thisKey in keys:
    if thisKey=='q':  # it is equivalent to the string 'q'
        print(, thisKey.tDown, thisKey.rt)

Get info about the available keyboards.

Only really useful on Mac/Linux because on these the info can be used to select a particular physical device when calling Keyboard. On Win this function does return information correctly but the :class:Keyboard can’t make use of it.


USB Info including with name, manufacturer, id, etc for each device

Return type:

A list of dicts

Back to top