Python/psycopy:getKeys收集密钥太早

2024-09-30 16:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我显示pic1 3秒,然后显示固定十字1秒钟,然后显示pic2,直到用户按下一个键。据我所知,它应该只在我的第三个“for”循环中收集按键,因为这是我创建键列表和检查键等的地方。但是,如果我在pic1或固定十字期间按了一个键,pic2一出现,它就会立即执行代码。它似乎是在我的第三个“for循环”之前注册按键,然后在我的第三个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()

干杯, 史蒂夫


Tags: 代码inforframesifwindowrunningprint
1条回答
网友
1楼 · 发布于 2024-09-30 16:30:06

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

相关问题 更多 >