2

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?

Boo
  • 23
  • 2
  • I'm not sure what you mean by "some trials are skipped". It seems like you get output from all five trials. The ones you note as "skipped" would correspond to ones where only the enter key was detected and hence just the initial size value (1.0) is output. Could you be more specific in describing what the problem is? – Michael MacAskill Jul 30 '14 at 21:48

1 Answers1

2

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.

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
  • 2
    Of course, you were right that the trials were not actually skipped. But as there was no stimulus display I simply called it "skipped". Also you were right with your suggestion that the ENTER key is still detected as pressed when the image is reset to its original size and the next trial begins. I added a loop checking for a released ENTER key and now everything works fine. Thank you!!! – Boo Jul 31 '14 at 11:31