按键后跳转到下一个试用版

2024-09-28 03:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的实验中,我有10次试验,在每次试验中,参与者必须在看到动画中颜色的变化后尽快按下一个键(“空格”)。作为确认按键的反馈,我想在按键后进入下一个试验(移动到下一个循环迭代)。我试图用breakcontinue在我的代码中实现这个想法,但它不起作用:

for i in range(nTrials):

    # Start with fixation cross 
    fixation.draw()
    win.flip()
    core.wait(2)

    # Play the video for 200 frames 
    for Nframes in range(200):
        optic_flow_movie.draw()
        fixation.draw()
        win.flip()

    # Get Participants responses
    keys = psychopy.event.getKeys(keyList=["space"],timeStamped=clock)

    if (keys[0][0] == 'space') is True:
        break
    else:
        continue

# Take only the timestamps from the variable key and store it in the variable RTs  
RTs = [sublist[1:2] for sublist in keys]                     # This stores only the timestamps
RTs = [item for sublist in RTs for item in sublist]          # This converts from list of lists to a flat list

非常感谢你的帮助


Tags: theinforrangespacekeyswin按键
1条回答
网友
1楼 · 发布于 2024-09-28 03:24:12

还不完全清楚您的试验的结构是什么,但是如果您想在动画期间监视响应,那么对event.getKeys()的调用需要嵌入到绘图循环中(即for Nframes in range(200):)。这样,每次屏幕刷新时都要检查按键,以便动画可以实时停止

目前,代码显示了整个动画,然后才检查键盘(但每次测试只检查一次)。不管那里发生了什么,下一个试验都将开始,因为这是主试验循环中的最后一个代码(即^{

最后,不推荐使用event模块进行键盘响应。为了获得更好的性能,您应该真正转向较新的Keyboard类:

https://www.psychopy.org/api/hardware/keyboard.html

https://discourse.psychopy.org/t/3-ways-to-get-keyboard-input-which-is-best/11184/3

相关问题 更多 >

    热门问题