PsychoPy- event.getKeys()未正确记录按键列表

时间:2014-06-04 18:03:05

标签: events 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()

1 个答案:

答案 0 :(得分:3)

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

在你的情况下,主题可能在它到达event.getKeys()行之前有大约0.1秒的时间按下,因为它们之间没有时间填充,如core.wait或多个win.flip() ' S

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

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

  1. 查看编码器中的演示 - >演示 - >看看如何呈现ShapeStims的刺激(Line,Rect,Circle等都是ShapeStims)。您将看到实例化和绘图应该以不同的方式完成并且更加简单。特别是,当你真正应该绘制它时,你会在每次试验中多次实例化完全刺激(更快更清洁)。
  2. 当您查找特定值时,无需循环浏览keys。只要做“如果' up'在钥匙。
  3. 这是一个经过修改的代码,可能更符合您的要求:

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