Key Releases Psychopy

孤街浪徒 提交于 2019-12-11 08:27:01

问题


I'm fairly new to psychopy but I'm having a lot of trouble recording keyboard events so far. The keyboard demo works so I know it is possible, but when I implement the code in my own program it doesn't record any keyboard activity at all. Right now my program is super simple as I'm just trying to get the hang of it. I have a picture and want to record the duration of keypresses during the time the picture is on screen. I have a couple print statements as sanity checks and when I run it (and am using the keyboard) it doesn't print anything. Here's an outline of my code:

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

回答1:


I tried stripping off all unnecessary stuff here (variables, window, stimuli etc.) and uses keyboard.getReleases() so it boils down to this:

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!'

This should print all info about each key release in the console. It works for me, at least. If it also works for you, the problem is probably related to something else in your script.



来源:https://stackoverflow.com/questions/32729026/key-releases-psychopy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!