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