【问题标题】:Failure to Psychopy GetKeys无法对 GetKeys 进行心理测试
【发布时间】:2021-03-25 04:11:37
【问题描述】:

我的任务是设计一个方块。它将随机从屏幕的一侧运行到另一侧。有时会发出哔哔声。一听到哔声,就必须按下按钮(这里是键盘)。问题是,当我使用“getkeys”方法时,它总是返回一个空列表。 RT 显示它主要获取缓冲区中的第一个输入。我也试过eventclear,但似乎没用。

这里是代码。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from psychopy import visual, core, event,gui
from psychopy import sound as sd
#from psychopy.iohub.client.keyboard import Keyboard as kbd
import random, math,time
from psychopy.iohub import launchHubServer
import numpy as np

win = visual.Window(fullscr = False, size = [960,540], color = 'black', units = 'pix')

io = launchHubServer()

kbd = io.devices.keyboard

x,y = win.size

ran = [random.randint(-400,400),random.randint(-400,400),random.randint(-150,150),random.randint(-150,150)]

mode = random.randint(0,3)

start_pos = [[ran[mode],-269],[ran[mode],269],[-479,ran[mode]],[479,ran[mode]]]

rect = visual.Rect(win, width = 50, height = 50, fillColor='white', pos = start_pos[mode], name = 'target')

timer = core.Clock()

dx = [0,0,1,-1]
dy = [1,-1,0,0]

def present(rect,t):
    timer.reset()
    while timer.getTime() < t:
        rect.draw()
        win.flip()

    
def movement(rect,time):
    event.clearEvents()
    x,y = win.size
    timer.reset()
    timer_2 = core.Clock()
    timer_2.reset()
    jump = True
    while timer.getTime() < time and (abs(rect.pos[0])< x/2 and abs(rect.pos[1])< y/2):
        timeUse = timer_2.getTime()
        timer_2.reset()
        a,b = rect.pos
        rect.pos += [dx[mode]*timeUse*100,dy[mode]*timeUse*100]

        if int(timer.getTime()) == 3 :
            if jump :
                sd.Sound('D', octave = 5).play()
            #print("D" , timer.getTime())
            if jump :
                timer_jump = core.Clock()
                timer_jump.reset()
            print(timer_jump.getTime(),'++++++++++')
            event.clearEvents()
            if timer.getTime() < time and jump :
                print('============')
                #k_1 = event.getKeys(keyList = ['a','z'])
                while event.getKeys() is not None:
                    
                    RT = timer_jump.getTime()
                    print(RT,'--------')
                    break
                jump = False
        
        #print([timer.getTime(),time])
        #print(rect.pos)
        rect.draw()
        win.flip()


present(rect,1)
movement(rect,15)

我会非常感谢谁提供帮助。

食堂

【问题讨论】:

  • @Michael MacAskill 也许你能帮帮我

标签: python psychopy


【解决方案1】:

问题是,当我使用“getkeys”方法时,它总是返回一个空列表。

event.getKeys() 是对键盘缓冲区的即时检查。除非自上次调用该函数后按下了某个键,否则它将始终返回一个空列表。所以它通常在一个循环中调用,通常每次屏幕刷新一次,以检测何时发生了按键操作。结果是它会多次返回一个空列表,直到实际按下某个键。

但 PsychoPy 的 event 模块实际上已被弃用,取而代之的是较新的 Keyboard 类。这样做的好处是它返回按键被按下的实际时间:当使用event.getKeys() 时,只需从调用函数的时间推断反应时间。这是按键按下后的一段时间 - 即使您在每次屏幕刷新时调用该函数(通常为 60 Hz),这也会将时间分辨率限制为 16.7 ms。与此同时,Keyboard 类在单独的进程中运行,并不断检查键盘硬件,从而实现毫秒级的精度。

Keyboard 类的文档在这里:https://www.psychopy.org/api/hardware/keyboard.html

【讨论】:

  • 感谢您的回复。但我也试过键盘类,返回仍然是空列表。我只是从文档中复制相同的代码,但它不起作用@Michael MacAskill
  • 这将需要比 SO 的单一问答格式提供的更多来回次数。我建议您将此问题提交至discourse.psychopy.org 的专用论坛,以获得您需要的支持。
猜你喜欢
  • 2020-08-29
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
相关资源
最近更新 更多