Keyboard🔗

To interact with the keyboard during an experiment, you can use a Keyboard object which monitors in a separate thread for key inputs.

Keyboard([keys, clock, on_press, on_release])

Object monitoring inputs on the keyboard.

KeyEvent(key, press_time, release_time)

Object representing a keyboard event.

Example usage:

from stimuli.keyboard import Keyboard
from stimuli.time import sleep

kb = Keyboard(keys=["esc", "space"]).start(suppress=True)
while True:
    events = kb.get_keys()
    if any(event.key == "esc" for event in events):
        print("Escape key pressed")
        break
    spaces = [event.key == "space" for event in events]
    print(f"Space bar pressed {spaces.count(True)} times in the last 500 ms.")
    sleep(0.5)