Equivalent of event.waitKeys() using keyboard.KeyBoard

Hi All,

I am implementing the new keyboard component using keyboard.Keyboard() and would like to wait for a key response (equivalent of event.waitKeys())

Running the following (https://www.psychopy.org/api/hardware/keyboard.html)

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'], waitDuration=True)
if 'quit' in keys:
    core.quit()
for key in keys:
    print(key.name, key.rt, key.duration)

Returns the following error message for me:

keys = kb.getKeys([‘right’, ‘left’, ‘quit’], waitDuration=True)
TypeError: getKeys() got an unexpected keyword argument ‘waitDuration’

Looking at the Keyboard Class (https://www.psychopy.org/_modules/psychopy/hardware/keyboard.html) would I be correct to think that “waitDuration” is not currently a modifiable attribute for “getKeys”?

I also see that waitKeys() in the Keyboard class is currently:

def waitKeys(maxWait=None, keyList=None, waitRelease=True, clear=True):
    keys = []
    raise NotImplementedError

as such, running the following:

keys = kb.waitKeys(['right', 'left', 'quit'])
if 'quit' in keys:
    core.quit()
for key in keys:
    print(key.name, key.rt, key.duration)

returns “NotImplementedError”

also running:

keys = kb.waitKeys(['right', 'left', 'quit'], maxWait=5)
if 'quit' in keys:
    core.quit()
for key in keys:
    print(key.name, key.rt, key.duration)

returns:
TypeError: waitKeys() got multiple values for argument ‘maxWait’

Looking at the implementation of event.waitKeys() (https://www.psychopy.org/_modules/psychopy/event.html#Mouse) it looks like this works using getKeys() in a while loop. So, would I be correct in thinking the Keyboard equivalent of event.waitKeys() would be something like:

#in your trial loop
kb.clearEvents ()
kb.clock.reset()
maxWait=5
got_keypress = False
while not got_keypress and kb.clock.getTime() < maxWait:
    keys =  kb.getKeys(keyList = ['left','right','escape'])
    if 'escape' in keys:
        core.quit()
    if len(keys)>0:
        print(keys[0].name, keys[0].rt, keys[0].duration)
        rt = keys[0].rt
        got_keypress = True

Apologies if I have missed somewhere where this is already documented, but I couldn’t seem to find this anywhere already!

Thanks,
Becca

PS. I am running Mac OS High Sierra 10.13.4 (still…) and PsychoPy v3.2.4

note: kb.clearEvents () needed otherwise negative response times recorded (from previous key presses)

That is because this function doesn’t have an argument called waitDuration. As per the documentation at Keyboard — PsychoPy v2023.2.3, there is, however, an argument waitRelease.

That function is not listed in the documentation, because as you found, its not implemented.

That looks pretty good.

1 Like

Thanks Michael,

Perhaps “argument” is the word I should have used instead of “modifiable attribute”.

“waitDuration” is the argument used in the Example usage code segment on that page at the moment, but the documentation below does refer to “waitRelease”.

Oh, thanks for picking up that error. I’ll fix that now. :+1:

1 Like