Steve
Steve

Reputation: 327

Python/Psychopy: getKeys collecting keys too early

In the code below, I'm displaying pic1 for 3 seconds, then I'm displaying a fixation cross for for 1 second, and then I'm displaying pic2 until the user presses a key. As far as I can tell, it should only be collecting keypresses in my 3rd 'for' loop, as this is where I created the keylist and check for keys etc. However, if I press a key during pic1 or the fixation cross, as soon as pic2 comes on, it instantly moves on with the code. It seems to be registering keypresses before my third 'for loop', which are then having an effect just as my third for loop comes into play. I don't see how this is happening because I am not checking for any keys during the display of pic1 and fixation. Could someone enlighten me here? Perhaps I am misunderstanding something fundamental about getKeys.

-If I don't press anything, then the expected behaviour occurs..it displays pic2 and waits for a key. It only moves on with the code if a key is pressed, OR if 60 seconds elapses (I've set the image to display for 60 seconds, the user is expected to respond in the first 5 seconds, so 60 is just safety).

def block1():

    running = 1

    while running ==1:

        for frames in range(image_frames):   #3 seconds
            pic1[0].draw()
            window.flip()

        for frame in range(fixation):        #1 second
            fix.draw()
            window.flip()

        for frames in range(stim_Frame):   #only moves on with keypress (or 60 secs)
            pic2[0].draw()
            start = window.flip()
            if frames == 0:
                stim_time = start
                print "stim time: "
                print stim_time

            allKeys = event.getKeys(keyList = ('f','h','escape'))
            for thisKey in allKeys:
                if thisKey == 'escape':
                    print "you quit"
                    window.close()
                    core.quit()
                if thisKey == 'f':
                    keyTime=core.getTime()
                    thisResp = 1      
                    print "keytime is: "
                    print keyTime

                elif thisKey == 'h':
                    keyTime=core.getTime()
                    thisResp = 0
                    print "keytime is: "
                    print keyTime

            if thisResp == 1 or thisResp == 0:
                break

        running = 2

    window.flip()

Cheers, Steve

Upvotes: 1

Views: 299

Answers (1)

jkr
jkr

Reputation: 19270

event.getKeys() returns all keys present in its memory buffer. Clear that buffer before your third loop.

def block1():

    running = 1

    while running ==1:

        for frames in range(image_frames):   #3 seconds
            pic1[0].draw()
            window.flip()

        for frame in range(fixation):        #1 second
            fix.draw()
            window.flip()

        event.clearEvents()  # Clear the previously pressed keys.

        for frames in range(stim_Frame):   #only moves on with keypress (or 60 secs)
            pic2[0].draw()
            start = window.flip()
            if frames == 0:
                stim_time = start
                print "stim time: "
                print stim_time

            allKeys = event.getKeys(keyList = ('f','h','escape'))
            for thisKey in allKeys:
                if thisKey == 'escape':
                    print "you quit"
                    window.close()
                    core.quit()
                if thisKey == 'f':
                    keyTime=core.getTime()
                    thisResp = 1      
                    print "keytime is: "
                    print keyTime

                elif thisKey == 'h':
                    keyTime=core.getTime()
                    thisResp = 0
                    print "keytime is: "
                    print keyTime

            if thisResp == 1 or thisResp == 0:
                break

        running = 2

    window.flip()

Upvotes: 2

Related Questions