[英]PsychoPy- event.getKeys() is not correctly recording a list of keypresses
我试图让用户能够通过按向上或向下键来调整心理状态下显示的行的长度。 我正在使用event.getKeys(),但是,它没有记录所按下的键。 我不确定为什么,但是它总是显示一个空的键列表。 这是我的代码:
class line(object):
def makeLine(self,length):
line = visual.Line(win=win,ori=-45,lineRGB=[-1,-1,-1],lineWidth=3.0, fillRGB=None,
pos= [0,0],interpolate=True,opacity=1.0,units='cm',size=length)
#describes the line
return line.draw()
line2length=2#original length of the line
line2=line()#makes line2 an instance of line class
line2.makeLine(line2length)#calls the makeLine function of the line class
win.flip()#updates the window
keys = event.getKeys()
expInfo['KeyPress']=keys
event.waitKeys(['return'])
print keys
for key in keys:
if 'up' in key:
line2length+=.5
line2.makeLine(line2length)
win.flip()
if 'down' in keys:
line2length-=.5
line2.makeLine(line2length)
win.flip()
event.clearEvents()
thisExp.nextEntry()
psychopy.event.getKeys()
返回键,因为事件模块被实例化的列表或自上次getKeys()
调用或自从event.clearEvents()
如果此框架中未注册任何键盘事件,则返回None
。
在您的情况下,在到达event.getKeys()
行之前,主题可能需要按下约0.1秒,因为它们之间没有时间间隔,例如core.wait或多个win.flip()
。
我确实怀疑您确实要使用event.waitKeys()
来等待第一个键盘事件并返回该事件。 这样可以保证返回的列表中始终只有一个键。
您的代码的其他一些注释:
keys
。 只需在按键中``如果'向上'。 这是经过修改的代码,它可能更接近您想要的代码:
# Create stimulus. Heavy stuff
line = visual.Line(win=win,ori=-45,lineRGB=[-1,-1,-1],lineWidth=3.0, fillRGB=None,
pos= [0,0],interpolate=True,opacity=1.0,units='cm',size=length)
# Change attribute, light stuff
line.size = 2 # set length
# Present stimulus
line.draw()
win.flip()
# Register responses after approximately 1 second (time by frames if you want exact timing) and have an extra "return"
core.wait(1)
keys = event.getKeys(['up', 'down']) # you probably want to restrict which keys are valid? Otherwise you have to react to invalid keys later - which is also ok.
event.waitKeys(['return'])
# React to response (no win-flip here, I assume that you only need this change on next trial, where the above win.flip() will execute
if keys != None:
if 'up' in keys:
line.length += 0.5
if 'down' in keys:
line.length -= 0.5
else:
pass # you can do something explicitly on missing response here.
# Mark change of trial
thisExp.nextEntry()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.