Await user key pressing

Hey everyone. I’ve only started coding in psychopy since a couple of days ago. I want to display a text stim and wait for the user to push a button and then go to the next stim. I’ve tried many things but when i run the code, the window appears for a second and then disappears, without waiting to recieve a key from the user

Hi Dara,

If you are using coder view you can use

from psychopy import event

then after presenting your window you use:

event.waitKeys()

You can also see an implementation if this in the pre-coded demo at “demo>input>what_key” from coder view

Best of luck,
Becca

Dear Becca
Thanks for your answer. In my code i have used this method but still wont get the desired result:

win = visual.Window([800, 800], pos=(0, 0))
instruction1 = visual.TextStim(win, text='First Instruction')
instruction1.draw()
win.flip()

k1 = keyboard.Keyboard()
k1.waitKeys(keyList=None)

if not k1.getKeys():
    instruction1.text = 'Hello'

Could you maybe tell me what’s wrong with this code?

Hi Dara,

The keyboard class doesn’t yet contain a “waitKeys method” (see this post Equivalent of event.waitKeys() using keyboard.KeyBoard). So you still need to use event.waitKeys() to wait for a keyboard response.

This should achieve what you want:


from psychopy import visual, event, core
from psychopy.hardware import keyboard

win = visual.Window([800, 800], pos=(0, 0), monitor='testMonitor')
instruction1 = visual.TextStim(win, text='First Instruction')
instruction1.draw()
win.flip()

k1 = keyboard.Keyboard()
event.waitKeys(keyList=None)
keyPressed = k1.getKeys()[0]
print(keyPressed.name)

For more details see the demo mentioned above :slight_smile:

Thanks,
Becca

Dear Becca
Thanks for your reply