gpt4 book ai didi

Python/Psychopy : getKeys collecting keys too early

转载 作者:行者123 更新时间:2023-12-01 03:30:37 27 4
gpt4 key购买 nike

在下面的代码中,我显示 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()

干杯,史蒂夫

最佳答案

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

关于Python/Psychopy : getKeys collecting keys too early,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40982278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com