psychopy.org | Reference | Downloads | Github

Redrawing window while waiting for keypress


#1

How can I solve a problem with redrawing window while waiting for keypress? If I move the window while waiting, the window is erased.

from psychopy import visual, core, event

win = visual.Window(allowGUI=True)

msg1 = visual.TextStim(win, text=u"Hello world!")
msg1.draw()
win.flip()
event.waitKeys()

win.close()
core.quit()

Do I have to wait for a key-press in a loop (in non-blocking way and call msg1.draw() and win.flip()), or there is another approach (using event.waitKeys() as showed)?

Usually I use fullscreen configuration and do not have this problem.


#2

Hi @psychopm, the waitKeys function will halt everything in your experiment, including your drawing see docs. A better method would be to get any key presses from the event buffer on every screen refresh by using event.getKeys() in place of event.waitKeys(). To have your image drawn continuously, you will need to draw your image within a loop.E.g.,

for frames in range(100):  # draw for 100 frames
    msg1.draw()
    win.flip()
    keys = event.getKeys()  # get keys from event buffer
    if keys:
          print(keys)  # if key was pressed, print keys
    
win.close()
core.quit()