PsychoPy- event.getKeys()未正确记录按键列表 [英] PsychoPy- event.getKeys() is not correctly recording a list of keypresses

查看:137
本文介绍了PsychoPy- event.getKeys()未正确记录按键列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户能够通过按向上或向下键来调整心理状态下显示的行的长度.我正在使用event.getKeys(),但是,它没有记录按下的键.我不知道为什么,但是它总是显示一个空的键列表.这是我的代码:

I am trying to have the user be able to adjust the length of a line displayed in psychopy by either pressing the up or down keys. I am using event.getKeys(), however, it is not recording the keys that are pressed. I am not sure why, but it always shows an empty list of keys. This is my code:

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()调用OR,因为 event.clearEvents().如果此框架中未注册任何键盘事件,则返回 None .

psychopy.event.getKeys() returns a list of keys since the event module was instantiated OR since last getKeys() call OR since event.clearEvents(). It returns None if no keyboard events were registered in this frame.

在您的情况下,对象可能在到达 event.getKeys()行之前大约要按下0.1秒,因为它们之间没有时间间隔,例如core.wait或多个 win.flip()的.

In your case, the subject probably had around 0.1 seconds to press before it reached the event.getKeys() line because there's nothing time-padding between, like a core.wait or multiple win.flip()'s.

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

I do suspect that you really want to use event.waitKeys() which waits for the first keyboard event and returns that. This guarantees that there's always exactly one key in the list returned.

您的代码的其他注释:

  1. 查看编码器中的演示->演示->刺激,看看如何显示ShapeStims(线,矩形,圆等都是ShapeStims).您将看到实例化和绘制应以不同的方式完成,并且要简单得多.特别是,当您真的应该绘制一个完整的刺激时(实际上更快,更清洁),您将在每个试验中多次实例化一个完整的刺激.
  2. 寻找特定值时,无需遍历.只需在按键中执行``如果'向上'.
  1. Look at the demos in coder --> demos --> stimuli to see how to present ShapeStims (Line, Rect, Circle etc. are all ShapeStims). You will see that instantiation and drawing should be done differently and much simpler. In particular, you're instantiating a full stimulus several times on each trial when you really should just draw it (much faster and cleaner).
  2. No need to loop through keys when you're looking for particular values. Just do ``if 'up' in keys.

这是经过修改的代码,可能更接近您想要的代码:

Here's a revised code, which might be closer to what you want:

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

这篇关于PsychoPy- event.getKeys()未正确记录按键列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆