Python / Psychopy:getKeys过早收集密钥

时间:2016-12-05 19:55:01

标签: python psychopy

在下面的代码中,我显示pic1持续3秒,然后我显示一个固定十字架1秒钟,然后我显示pic2直到用户按下一个键。据我所知,它应该只是在我的第3个'for'循环中收集按键,因为这是我创建键列表并检查键等的地方。但是,如果我在pic1或固定十字架中按键,一旦pic2出现,它立即继续使用代码。它似乎是在我的第三个'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()

干杯, 史蒂夫

1 个答案:

答案 0 :(得分:2)

event.getKeys()返回其内存缓冲区中的所有密钥。在第三次循环之前清除缓冲区。

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