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()
我剛纔看了一下'keyboard.py'的ioHub演示源代碼,它使用'keyboard.getKeys()'而不是'keyboard.getEvents()'。這有什麼區別嗎? –
嗨 - 是的,它的確如此。不幸的是,他們在演示中做到這一點的方式並不適用於我的... key.duration沒有返回任何內容。我只是通過獲取keyPresses()和keyReleases()來減少它們,而不是使用似乎完全無響應的事件(儘管計算持續時間更容易),謝謝! –