Search code examples
pythonpsychopy

How to populate psychopy.event.getKeys()?


Hitting apostrophe is not doing anything. start_scan is just a textStim.

while True:
    print event.getKeys()
    start_scan.draw()
    win.flip()
    if "'" in event.getKeys():
        break
    event.clearEvents()

Solution

  • That's because event.getKeys() returns apostrophe as the string 'apostrophe'. To see that do

    from psychopy import visual, event
    win = visual.Window()
    while True:
        response = event.getKeys()
        if response:
            print response  # check what the key was
            if 'apostrophe' in response:
                break
    

    The reason you don't see that printed is that each call to event.getKeys() clears the event buffer. Similarly of course for event.clearEvents(). Since your script will spend like 99.9% of it's time hanging at win.flip() it is very unlikely that the key press happened just prior to your print event.getKeys(), thus it never prints how the event module represents the keys you pressed.

    So this is something to look out for. The event module doesn't always represent keys by the characters they produce. However,psychopy.iohub module does just that. So e.g. something like SHIFT+r becomes "R". It does require a few more lines of code to get running, though. See the documentation and the demo under Coder --> demos --> iohub --> keyboard.