Keyboard - several keys pressed simultaneously

Hello,

I would like to find a way for my routine to stop only if several keyboard keys are pressed simultaneously.

How do I code for this?

Thanks,
Margot

In Python…

Something like this would tell you if > 1 press event has occurred since the last screen retrace / call to event.getKeys():

from psychopy import visual, event, core

win = visual.Window([800, 400])  # , fullscr=True, allowGUI=False)
msg = visual.TextStim(win, text="No keys pressed.")
stime = core.getTime()
while core.getTime() - stime < 5:
    key_events = event.getKeys()
    if len(key_events) > 0:
        msg.setText("%d keys were pressed at about the same time: %s"%(len(key_events), str(key_events)))
    msg.draw()
    win.flip()

You could do a similar type thing using the psychopy.hardware.keyboard.Keyboard() class as well.

If you want to know if > 1 key is being pressed down, regardless of when each key was first pressed, you could use iohub keyboard:

from psychopy import visual, iohub, core

keyboard = iohub.launchHubServer().devices.keyboard

win = visual.Window([800, 400])  # , fullscr=True, allowGUI=False)
msg = visual.TextStim(win, text="No keys pressed.")

stime = core.getTime()
while core.getTime() - stime < 5:
    pressed_keys = keyboard.state
    if len(pressed_keys) > 0:
        msg.setText("%d keys are currently pressed: %s"%(len(pressed_keys), str(pressed_keys.keys())))
    else:
        msg.setText("No keys pressed.")
    msg.draw()
    win.flip()

core.quit()

Hi,
I’m working with PsychoPy builder and was trying to use your proposed solution for my purposes, i.e. I want a routine to end only if the two allowed keys are pressed simultaneously. However, with the solution above it looks like after a certain time has lapsed the routine ends also if only one of the keys is pressed. Do you have an idea, how I could fixe it?