I try to run an experiment in which the stimulus size can continuously (on a frame-by-frame basis) be changed by the participant (i.e. pressing/holding UP-key increases size, DOWN-key decreases size) and confirmed using ENTER-key. I found that using Key.StateHandle() from pyglet is helpful and basically my code works as intended. However, I find that some "trials" are skipped.
Here is my code:
from psychopy import visual, core, event
from pyglet.window import key
win = visual.Window([1280,1024], fullscr=False, units='deg',monitor='testMonitor',winType='pyglet')
keyState=key.KeyStateHandler()
win.winHandle.push_handlers(keyState)
myCircle = visual.Circle(win,edges=128,radius=2.5,lineColor='black',fillColor='black',units='deg')
myCircle2 = visual.Circle(win,edges=128,radius=2.5,lineColor='black',fillColor='black',units='deg')
response=0
for i in range(5):
sizeIni=1.0
myCircle2.setRadius(sizeIni)
myCircle2.setPos([0.0, -10.0])
event.clearEvents()
response=0
while response <> 1:
response=None
if keyState[key.UP]:
sizeIni=sizeIni+0.03333
if sizeIni>=10.0:
sizeIni=10.0
myCircle2.setRadius(sizeIni)
response=0
elif keyState[key.DOWN]:
sizeIni=sizeIni-0.03333
if sizeIni<=1.0:
sizeIni=1.0
myCircle2.setRadius(sizeIni)
response=0
elif keyState[key.ENTER]:
size=sizeIni
print size
response=1
elif keyState[key.ESCAPE]:
core.quit()
size=sizeIni
myCircle.draw()
myCircle2.draw()
win.flip()
This routine should lead to 5 trials, but some trials are skipped. Here is prototypical output:
1.36663
1.0 # this trial is skipped
1.6666
2.23321
1.0 # this trial is skipped
There order of skipped trials also is variable. I received the problem on two different computers (Ubuntu/Windows 7). Are there any obvious errors in my code, or is there any advice how to code the experiment in a different way?