Keyboard.getKeys() does not work, but .waitKeys() does

Hello,

I have been unable to get Keyboard.getKeys() to return any key presses, but have found success using Keyboard.waitKeys(). I am worries that I don’t understand how to use .getKeys() and am seeking guidance.

The following minimal code works, and reports on any key presses that I hit:
##################

kb = keyboard.Keyboard()
print('begin')
kb.clock.reset()
keys = kb.waitKeys(maxWait=5, waitRelease=False, clear=True)
for key in keys:
      print(key.name, key.rt, key.duration)

##########################

However, this code returns an empty list for keys:
##########################

kb = keyboard.Keyboard()
print('begin')
kb.clock.reset()
tStart = kb.device.clock.getTime()
while kb.device.clock.getTime() - tStart < 5:
     keys = kb.getKeys()
     for key in keys:
          print(key.name, key.rt, key.duration)

#####################

For both cases, I have a Window() that is open. In fact, I needed the Window to exist in order for waitKeys to have any success.

Am I using getKeys incorrectly? I am worried that I am not using the Keyboard class correctly.

Thanks for your time and guidance

Hi There,

When I am trying to figure these things out I find it is helpful to see what Builder is doing that is different from my code. So, for example I made an empty PsychoPy Builder file, put a keyboard component in, turned of force end routine (which would use waitKeys) and set duration to 10 seconds (file attached).

The compiled code from this shows:

theseKeys = key_resp.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)

So to start with I might try testing what happens if you add those extra parameters that Builder is using with getKeys which are not currently present in your code.

Hopefully that helps!
Becca
untitled.psyexp (8.8 KB)