Skip to content

Instantly share code, notes, and snippets.

@alexholcombe
Created July 4, 2012 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexholcombe/3044529 to your computer and use it in GitHub Desktop.
Save alexholcombe/3044529 to your computer and use it in GitHub Desktop.
demonstration of time-stamped keyboard polling with psychopy
from psychopy import visual, event, core
import numpy as np
"""Record response latency
"""
respClock = core.Clock()
Hz = 60
#create a window to draw in
bgColor=np.array([-1,-1,-1])
scrn = 0; fullscrn= 0
myWin =visual.Window(color=bgColor, allowGUI=False,screen=scrn,fullscr=fullscrn, units='pix',waitBlanking=False)
respText = visual.TextStim(myWin,pos=(0, 0),color=(1,1,1),colorSpace='rgb',alignHoriz='center', alignVert='center', units='norm',autoLog=False)
respText.setText('Press up arrow or down arrow')
core.wait(.2)
for i in xrange(50): #go through 50 frames that show nothing.
#Because sometimes the operating system / graphics pipeline needs to clean up its act before accurate timing ensues
myWin.flip()
maxRespTime = 3 #sec
frame = 0
respText.draw()
myWin.flip()
respClock.reset()
#using time stamps. Therefore not polling keyboard explicitly, event will do it automatically, instead in loop until maxRespTime reached
while frame < maxRespTime*Hz:
frame += 1
respText.draw()
myWin.flip()
#check the key buffer
resp = 0
keysAndTimesPressed = event.getKeys(timeStamped=respClock) #time stamping causes it to return a tuple
print 'num keys pressed = ',len(keysAndTimesPressed)
for key in keysAndTimesPressed:
respMade = True
print 'key=',key
if key[0] in ['up','down']: #up arrow or down arrow
respLatencySecs = key[1]
respMade = True
if key[0]=='up':
resp = 1
if key[0]=='down':
resp = -1
print 'Response latency according to respClock = ', np.around(respLatencySecs,3), 'seconds'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment