Python/Psychopy: getKeys 收集密钥太早

Python/Psychopy: getKeys collecting keys too early

在下面的代码中,我显示 pic1 3 秒,然后我显示注视十字 1 秒,然后我显示 pic2 直到用户按下一个键。据我所知,它应该只在我的第 3 个 'for' 循环中收集按键,因为这是我创建键列表并检查键等的地方。但是,如果我在 pic1 或固定交叉期间按下一个键,一旦 pic2 出现,它会立即继续执行代码。它似乎在我的第三个 'for loop' 之前注册按键,然后在我的第三个 for 循环开始起作用时产生效果。我看不出这是怎么发生的,因为在显示 pic1 和固定期间我没有检查任何键。有人可以在这里启发我吗?也许我误解了有关 getKeys 的一些基本知识。

-如果我什么都不按,就会发生预期的行为。它显示 pic2 并等待按键。它只会在按下某个键或经过 60 秒后继续执行代码(我已将图像设置为显示 60 秒,预计用户会在前 5 秒内做出响应,因此 60 只是安全的)。

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()

干杯, 史蒂夫

event.getKeys() returns 内存缓冲区中存在的所有键。在第三个循环之前清除该缓冲区。

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()