stimuli.keyboard.Keyboard🔗

class stimuli.keyboard.Keyboard(keys=None, *, clock=<class 'stimuli.time._clock.Clock'>, on_press=None, on_release=None)[source]🔗

Object monitoring inputs on the keyboard.

Parameters:
keysstr | list of str | None

The list of keys to monitor. If None, all keys will be monitored. Keys should be specified as strings, for instance ['a', 'enter', 'space', 'shift_r'].

clockstimuli.time.BaseClock class

Clock object to use for timing measurements. By default, the stimuli.time.Clock class is used.

on_presscallable() | None

Additional callback function to call on a button press. The function should have the signature callback(key: KeyCode | Key | None) where key is None if the key is unknown or KeyCode or Key otherwise.

on_releasecallable() | None

Additional callback function to call on a button release. The function should have the signature callback(key: KeyCode | Key | None) where key is None if the key is unknown or KeyCode or Key otherwise.

Attributes

t0

The time of instantiation of the Keyboard in seconds.

Methods

get_keys()

Get a list of keys that were pressed since the last call.

reset()

Reset the clock and events of the Keyboard.

start(*[, suppress])

Start monitoring the keyboard.

stop()

Stop monitoring the keyboard.

wait_keys(*[, timeout])

Wait until a key is pressed.

get_keys()[source]🔗

Get a list of keys that were pressed since the last call.

Returns:
keyslist of stimuli.keyboard.KeyEvent | None

The list of keys that were pressed. If the keyboard is not running, None is returned.

Notes

Note that calling this method will reset the buffer.

reset()[source]🔗

Reset the clock and events of the Keyboard.

start(*, suppress=False)[source]🔗

Start monitoring the keyboard.

Parameters:
suppressbool

If True, the events are not propagated to the system meaning that they will be only received by the Keyboard object and not by any other application or process.

Returns:
keyboardKeyboard

The current Keyboard object, modified in-place.

stop()[source]🔗

Stop monitoring the keyboard.

Returns:
keyboardKeyboard

The current Keyboard object, modified in-place.

wait_keys(*, timeout=None)[source]🔗

Wait until a key is pressed.

Parameters:
timeoutfloat | None

The maximum time to wait for a key press in seconds. If None, the function will wait indefinitely.

Returns:
keystimuli.keyboard.KeyEvent | None

The key that was pressed or None if the timeout was reached.

property t0🔗

The time of instantiation of the Keyboard in seconds.

All timestamps of keyboard events are relative to this time.

Type:

float