2015-09-23 33 views
1

我对psychopy相当陌生,但迄今为止我在录制键盘事件方面遇到了很多麻烦。键盘演示工作,所以我知道这是可能的,但是当我在我自己的程序中实现代码时,它根本不记录任何键盘活动。现在我的程序非常简单,因为我只是试图去掌握它。我有一张照片,并且想要在照片显示在屏幕上时记录按键的持续时间。我有几个打印语句作为理智检查,当我运行它(并使用键盘)时,它不会打印任何内容。下面是我的代码的轮廓:关键发布Psychopy

from psychopy.iohub import launchHubServer, EventConstants 

io = launchHubServer() 
keyboard = io.devices.keyboard 
keyboard.reporting = True 

#-------Start Routine "trial"------- 
continueRoutine = True 
io.clearEvents('all') 
duration_lst=[] 
while continueRoutine and routineTimer.getTime() > 0: 
    # get current time 
    t = trialClock.getTime() 
    frameN = frameN + 1 # number of completed frames (so 0 is the first frame) 
    # update/draw components on each frame 

    # *image* updates 
    if t >= 0.0 and image.status == NOT_STARTED: 
     # keep track of start time/frame for later 
     image.tStart = t # underestimates by a little under one frame 
     image.frameNStart = frameN # exact frame index 
     image.setAutoDraw(True) 
    if image.status == STARTED and t >= (0.0 + (5.0- win.monitorFramePeriod*0.75)): #most of one frame period left 
    image.setAutoDraw(False) 
    # *ISI* period 
    if t >= 0.0 and ISI.status == NOT_STARTED: 
     # keep track of start time/frame for later 
     ISI.tStart = t # underestimates by a little under one frame 
     ISI.frameNStart = frameN # exact frame index 
     ISI.start(0.5) 
    elif ISI.status == STARTED: #one frame should pass before updating params and completing 
     ISI.complete() #finish the static period 
    # get keyboard events 
    for event in keyboard.getEvents(): 
     print event 
     if event.type == EventConstants.KEYBOARD_CHAR: 
      key_press_duration=event.duration 
      duration_lst.append(key_press_duration) 
      print duration_lst 

    # check if all components have finished 
    if not continueRoutine: # a component has requested a forced-end of Routine 
    break 
    continueRoutine = False # will revert to True if at least one component still running 
    for thisComponent in trialComponents: 
     if hasattr(thisComponent, "status") and thisComponent.status != FINISHED: 
      continueRoutine = True 
      break # at least one component has not yet finished 

    # check for quit (the Esc key) 
    if endExpNow or event.getKeys(keyList=["escape"]): 
     core.quit() 

    # refresh the screen 
    if continueRoutine: # don't flip if this routine is over or we'll get a blank screen 
     win.flip() 
+0

我刚才看了一下'keyboard.py'的ioHub演示源代码,它使用'keyboard.getKeys()'而不是'keyboard.getEvents()'。这有什么区别吗? –

+1

嗨 - 是的,它的确如此。不幸的是,他们在演示中做到这一点的方式并不适用于我的... key.duration没有返回任何内容。我只是通过获取keyPresses()和keyReleases()来减少它们,而不是使用似乎完全无响应的事件(尽管计算持续时间更容易),谢谢! –

回答

2

我想在这里剥离所有不必要的东西(变量,窗口​​,刺激等),并使用keyboard.getReleases()所以它归结为:

from psychopy import iohub 
io = iohub.launchHubServer() 
keyboard = io.devices.keyboard 
print 'press something now!' 
while True: 
    # get keyboard releases 
    for event in keyboard.getReleases(): 
     print 'Released!' 

这应该打印控制台中每个密钥版本的所有信息。至少,它适用于我。如果它也适用于您,则问题可能与脚本中的其他内容有关。