【问题标题】:PsychoPy- event.getKeys() is not correctly recording a list of keypressesPsychoPy- event.getKeys() 没有正确记录按键列表
【发布时间】:2014-06-04 18:03:05
【问题描述】:

我试图让用户能够通过按向上或向下键来调整在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()

【问题讨论】:

    标签: events psychopy


    【解决方案1】:

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

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

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

    您的代码的其他一些 cmets:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多