Question

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?

Was it helpful?

Solution

I think the problem is that you are checking for whether the enter key is pressed, but not whether it gets released before being detected again. i.e. this code checks once in every screen refresh period for the key state. It is possible that the subject pushes enter in response to one trial but then that key is still down when the image is reset to its original size and the next trial begins.

You should keep track of whether two successive key press detections are due to the enter key, without an intervening period of either no key being pressed, or one of the others being detected. I guess this isn't necessary for the other two keys, as you want to respond to those continuously, whereas enter keys are supposed to be treated as discrete events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top