def _onPygletKey(symbol, modifiers): """handler for on_key_press events from pyglet Adds a key event to global _keyBuffer which can then be accessed as normal using event.getKeys(), .waitKeys(), clearBuffer() etc... Appends a tuple with (keyname, timepressed) into the _keyBuffer""" keyTime=psychopy.core.getTime() #capture when the key was pressed thisKey = pyglet.window.key.symbol_string(symbol).lower()#convert symbol into key string #convert pyglet symbols to pygame forms ( '_1'='1', 'NUM_1'='[1]') thisKey = (thisKey.lstrip('_').lstrip('NUM_'),keyTime) # modified to capture time of keypress so key=(keyname,keytime) _keyBuffer.append(thisKey) log.data("Keypress: %s" %thisKey[0])
def _onPygletMouseRelease(x, y, button, modifiers): global mouseButtons if button == pyglet.window.mouse.LEFT: mouseButtons[0] = 0 label = "Left" if button == pyglet.window.mouse.MIDDLE: mouseButtons[1] = 0 label = "Middle" if button == pyglet.window.mouse.RIGHT: mouseButtons[2] = 0 label = "Right" log.data("Mouse: %s button up, pos=(%i,%i)" % (label, x, y))
def _onPygletMouseRelease(x,y, button, modifiers): global mouseButtons if button == pyglet.window.mouse.LEFT: mouseButtons[0]=0 label='Left' if button == pyglet.window.mouse.MIDDLE: mouseButtons[1]=0 label='Middle' if button == pyglet.window.mouse.RIGHT: mouseButtons[2]=0 label='Right' log.data("Mouse: %s button up, pos=(%i,%i)" %(label, x,y))
def _onPygletMousePress(x, y, button, modifiers): global mouseButtons, mouseClick, mouseTimes if button == pyglet.window.mouse.LEFT: mouseButtons[0] = 1 mouseTimes[0] = psychopy.core.getTime() - mouseClick[0].timeAtLastReset label = "Left" if button == pyglet.window.mouse.MIDDLE: mouseButtons[1] = 1 mouseTimes[1] = psychopy.core.getTime() - mouseClick[1].timeAtLastReset label = "Middle" if button == pyglet.window.mouse.RIGHT: mouseButtons[2] = 1 mouseTimes[2] = psychopy.core.getTime() - mouseClick[2].timeAtLastReset label = "Right" log.data("Mouse: %s button down, pos=(%i,%i)" % (label, x, y))
def _onPygletMousePress(x,y, button, modifiers): global mouseButtons, mouseClick, mouseTimes if button == pyglet.window.mouse.LEFT: mouseButtons[0]=1 mouseTimes[0]= psychopy.core.getTime()-mouseClick[0].timeAtLastReset label='Left' if button == pyglet.window.mouse.MIDDLE: mouseButtons[1]=1 mouseTimes[1]= psychopy.core.getTime()-mouseClick[1].timeAtLastReset label='Middle' if button == pyglet.window.mouse.RIGHT: mouseButtons[2]=1 mouseTimes[2]= psychopy.core.getTime()-mouseClick[2].timeAtLastReset label='Right' log.data("Mouse: %s button down, pos=(%i,%i)" %(label, x,y))
def waitKeys(maxWait=None, keyList=None): """ Halts everything (including drawing) while awaiting input from keyboard. Then returns *list* of keys pressed. Implicitly clears keyboard, so any preceding keypresses will be lost. Optional arguments specify maximum wait period and which keys to wait for. Returns None if times out. """ # NB pygame.event does have a wait() function that will # do this and maybe leave more cpu idle time? key = None clearEvents("keyboard") # so that we only take presses from here onwards. if maxWait != None and keyList != None: # check keylist AND timer timer = psychopy.core.Clock() while key == None and timer.getTime() < maxWait: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events() # pump events on pyglet windows keys = getKeys() # check if we got a key in list if len(keys) > 0 and (keys[0] in keyList): key = keys[0] elif keyList != None: # check the keyList each time there's a press while key == None: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events() # pump events on pyglet windows keys = getKeys() # check if we got a key in list if len(keys) > 0 and (keys[0] in keyList): key = keys[0] elif maxWait != None: # onyl wait for the maxWait timer = psychopy.core.Clock() while key == None and timer.getTime() < maxWait: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events() # pump events on pyglet windows keys = getKeys() # check if we got a key in list if len(keys) > 0: key = keys[0] else: # simply take the first key we get while key == None: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events() # pump events on pyglet windows keys = getKeys() # check if we got a key in list if len(keys) > 0: key = keys[0] # after the wait period or received a valid keypress if key: log.data("Key pressed: %s" % key) return [key] # need to convert back to a list else: return None # no keypress in period
def _onPygletMouseWheel(x, y, scroll_x, scroll_y): global mouseWheelRel mouseWheelRel = mouseWheelRel + numpy.array([scroll_x, scroll_y]) log.data("Mouse: wheel shift=(%i,%i), pos=(%i,%i)" % (scroll_x, scroll_y, x, y))
def _onPygletMouseWheel(x,y,scroll_x, scroll_y): global mouseWheelRel mouseWheelRel = mouseWheelRel+numpy.array([scroll_x, scroll_y]) log.data("Mouse: wheel shift=(%i,%i), pos=(%i,%i)" %(scroll_x, scroll_y,x,y))
def waitKeys(maxWait = None, keyList=None): """ Halts everything (including drawing) while awaiting input from keyboard. Then returns *list* of keys pressed. Implicitly clears keyboard, so any preceding keypresses will be lost. Optional arguments specify maximum wait period and which keys to wait for. Returns None if times out. """ #NB pygame.event does have a wait() function that will #do this and maybe leave more cpu idle time? key=None clearEvents('keyboard')#so that we only take presses from here onwards. if maxWait!=None and keyList!=None: #check keylist AND timer timer = psychopy.core.Clock() while key==None and timer.getTime()<maxWait: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events()#pump events on pyglet windows keys = getKeys() #check if we got a key in list if len(keys)>0 and (keys[0] in keyList): key = keys[0] elif keyList!=None: #check the keyList each time there's a press while key==None: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events()#pump events on pyglet windows keys = getKeys() #check if we got a key in list if len(keys)>0 and (keys[0] in keyList): key = keys[0] elif maxWait!=None: #onyl wait for the maxWait timer = psychopy.core.Clock() while key==None and timer.getTime()<maxWait: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events()#pump events on pyglet windows keys = getKeys() #check if we got a key in list if len(keys)>0: key = keys[0] else: #simply take the first key we get while key==None: if havePyglet: wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events()#pump events on pyglet windows keys = getKeys() #check if we got a key in list if len(keys)>0: key = keys[0] #after the wait period or received a valid keypress if key: log.data("Key pressed: %s" %key) return [key]#need to convert back to a list else: return None #no keypress in period
So setting to DEBUG level will include all possible messages, setting to ERROR will include only the absolutely essential messages. """ globalClock = core.Clock( ) #if this isn't provided the log times will reflect secs since python started log.setDefaultClock(globalClock) #use this for log.console.setLevel(log.DEBUG) #set the console to receive nearly all messges logDat = log.LogFile( 'logLastRun.log', filemode='w', #if you set this to 'a' it will append instead of overwriting level=log.WARNING) #errors, data and warnings will be sent to this logfile #the following will go to any files with the appropriate minimum level set log.info('Something fairly unimportant') log.data('Something about our data. Data is likely very important!') log.warning( 'Handy while building your experiment - highlights possible flaws in code/design' ) log.error( "You might have done something that PsychoPy can't handle! But hopefully this gives you some idea what." ) #some things should be logged timestamped on the next video frame #For instance the time of a stimulus appearing is related to the flip: win = visual.Window([400, 400]) for n in range(5): win.logOnFlip('frame %i occured' % n, level=log.EXP) if n in [2, 4]: win.logOnFlip('an even frame occured', level=log.EXP) win.flip()
20:INFO 10:DEBUG So setting to DEBUG level will include all possible messages, setting to ERROR will include only the absolutely essential messages. """ globalClock = core.Clock()#if this isn't provided the log times will reflect secs since python started log.setDefaultClock(globalClock)#use this for log.console.setLevel(log.DEBUG)#set the console to receive nearly all messges logDat = log.LogFile('logLastRun.log', filemode='w',#if you set this to 'a' it will append instead of overwriting level=log.WARNING)#errors, data and warnings will be sent to this logfile #the following will go to any files with the appropriate minimum level set log.info('Something fairly unimportant') log.data('Something about our data. Data is likely very important!') log.warning('Handy while building your experiment - highlights possible flaws in code/design') log.error("You might have done something that PsychoPy can't handle! But hopefully this gives you some idea what.") #some things should be logged timestamped on the next video frame #For instance the time of a stimulus appearing is related to the flip: win = visual.Window([400,400]) for n in range(5): win.logOnFlip('frame %i occured' %n, level=log.EXP) if n in [2,4]: win.logOnFlip('an even frame occured', level=log.EXP) win.flip() #LogFiles can also simply receive direct input from the write() method #messages using write() will be sent immediately, and are often not #in correct chronological order with logged messages
dir=270, nDots=100, fieldShape='circle', fieldPos=(0.0, 0.0), fieldSize=1, dotLife=5, #number of frames for each dot to be drawn signalDots= 'same', #are the signal and noise dots 'different' or 'same' popns (see Scase et al) noiseDots= 'direction', #do the noise dots follow random- 'walk', 'direction', or 'position' speed=0.01, coherence=0.9) message = visual.TextStim(myWin, text='Hit Q to quit', pos=(0, -0.5)) trialClock = core.Clock() myWin.setRecordFrameIntervals() n = 0 while True: #quits after 20 secs n += 1 dotPatch.draw() message.draw() myWin.flip() #redraw the buffer for n in range(10): log.info('%i info' % n) #handle key presses each frame for key in event.getKeys(): if key in ['escape', 'q']: log.data('final fps = %.3f' % myWin.fps()) myWin.close() core.quit() event.clearEvents() #keep the event buffer from overflowing
#create a window to draw in myWin =visual.Window((600,600), allowGUI=False, bitsMode=None, units='norm', winType='pyglet') #INITIALISE SOME STIMULI dotPatch =visual.DotStim(myWin, rgb=(1.0,1.0,1.0), dir=270, nDots=100, fieldShape='circle', fieldPos=(0.0,0.0),fieldSize=1, dotLife=5, #number of frames for each dot to be drawn signalDots='same', #are the signal and noise dots 'different' or 'same' popns (see Scase et al) noiseDots='direction', #do the noise dots follow random- 'walk', 'direction', or 'position' speed=0.01, coherence=0.9) message =visual.TextStim(myWin,text='Hit Q to quit', pos=(0,-0.5)) trialClock =core.Clock() myWin.setRecordFrameIntervals() n=0 while True:#quits after 20 secs n+=1 dotPatch.draw() message.draw() myWin.flip()#redraw the buffer for n in range(10): log.info('%i info' %n) #handle key presses each frame for key in event.getKeys(): if key in ['escape','q']: log.data('final fps = %.3f' % myWin.fps()) myWin.close() core.quit() event.clearEvents()#keep the event buffer from overflowing