1

我试图让用户能够通过按向上或向下键来调整在psychopy中显示的行的长度。我正在使用 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()
4

1 回答 1

3

psychopy.event.getKeys()返回自事件模块实例化以来的键列表或自上次getKeys()调用以来或自event.clearEvents(). None如果在此帧中没有注册键盘事件,则返回。

在您的情况下,主题在到达线之前可能有大约 0.1 秒的时间来按下,event.getKeys()因为之间没有任何时间填充,例如 core.wait 或 multiple win.flip()

我确实怀疑您真的想使用event.waitKeys()which 等待第一个键盘事件并返回它。这保证了返回的列表中始终只有一个键。

对您的代码的其他一些评论:

  1. 看 coder --> demos --> stimuli 中的 demos 看看如何呈现 ShapeStims(Line、Rect、Circle 等都是 ShapeStims)。您将看到实例化和绘图应该以不同的方式进行并且更简单。特别是,你在每次试验中多次实例化一个完整的刺激,而你真的应该画它(更快更清晰)。
  2. keys在寻找特定值时无需循环。只需在键中执行 ``if 'up' 即可。

这是一个修改后的代码,它可能更接近你想要的:

# 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()
于 2014-06-04T20:42:22.583 回答