psychopy, changing poligon characteristics with keyboard

65 views Asked by At

I need to run a test where the subject is able to rotate a line with the keyboard during the test.

I have been looking a lot, but I have not found a good answer.

So far I have tried this:

p = 0
polygon = visual.Line(
    win=win,
    name='polygon',
    units='cm', 
    start=(-(2, 0.5)[0]/2.0, 0),
    end=(+(2, 0.5)[0]/2.0, 0),
    ori=p,
    pos=(0, 0),
    lineWidth=1,
    lineColor=[1,1,1],
    lineColorSpace='rgb',
    fillColor=[1,1,1],
    fillColorSpace='rgb',
    opacity=1,
    depth=0.0,
    interpolate=True
)

while True:
   resp_key = event.waitKeys(keyList=['n','m','return']) 

   if resp_key == 'n':
      p = p-1
   elif resp_key == 'm':
      p = p+1
   elif resp_key == 'return':
      break

Thanks!

1

There are 1 answers

0
Michael MacAskill On

After altering the value of p, you then need to actually apply it to your line object to update its characteristics, and then draw it to the screen.

e.g. something like:

polygon.ori = p
polygon.draw()
win.flip()

Also, note that event.waitKeys() returns a list of keys, even for just a single keypress. So your checks for keys should actually look like this:

if 'n' in resp_key:

rather than:

if resp_key == 'n':

Also check out the shapes.py demo under the Demos menu in PsychoPy coder view for some examples of dynamically updating polygons. Although that demo doesn't use keyboard interaction, it might still be useful for you.