0

I have a script in Matlab (psychtoolbox) in which I have coded a visual analog scale, anchored at 0 and 100, on which participants move a marker by holding down the left and right arrow-keys. The participant has five-seconds to move the marker to the desired location before the experimental protocol moves on to the next stimuli.

In working to code something similar in PsychoPy, I have created the following test code:

from psychopy import visual, core
win = visual.Window([800,800])

# rating scale
scale = visual.RatingScale(win, low=0, high=100, size=2, tickMarks=['0','100'],
markerStart='50', marker='circle', textSize=.5, showValue=False, 
showAccept=False, noMouse=True, maxTime = 5)

while scale.noResponse:
    scale.draw()
    win.flip()

win.close()

This code actually works pretty well to get started, but I have a problem with the way the keyboard input works. The marker only moves 1-point (between 0 and 100) every time the user presses and releases the key. The desired outcome is to have the marker move continuously as the left or right key is pressed. My thoughts are that I can accomplish this by editing the underlying code for PsychoPy, though I feel this is a little out of my comfort zone. Any help for alternative methods would be greatly appreciated. This is only a start for the code, so anything goes!

Thank you, Patrick

Patrick Williams
  • 694
  • 8
  • 22
  • 2
    EDIT: since RatingScale uses pyglet to catch keyboard events, I'm unsure if this strategy would work, but here it goes (Just a very quick and untested suggestion to get you going): you can use the iohub module to catch key releases: http://stackoverflow.com/questions/32729026/key-releases-psychopy?rq=1 and see iohub demo in Coder --> demos --> iohub --> keyboard. Then you could in your while loop have a ``core.wait(0.2)`` between listening for keyboard releases if it is to move 5 points every second. – Jonas Lindeløv Mar 22 '16 at 21:36

1 Answers1

1

I think this query solves your issue: PsychoPy Key Down Code using ioHub

rating.markerPlacedAt is what you need to update every frame. Here's a stripped version of my (rather raw) adaptation of the each frame section:

for event_io in keyboard.getEvents():
    if event_io.type == EventConstants.KEYBOARD_PRESS:
        if event_io.key == u'right':
            increment = 0.01 # move one step to the right
        elif event_io.key == u'left':
            increment = -0.01 # move one step to the left
    if event_io.type == EventConstants.KEYBOARD_RELEASE:
        increment = 0 # stop changing position

if 0 < rating.markerPlacedAt < 1:
    rating.markerPlacedAt += increment
Community
  • 1
  • 1
Jinglestar
  • 376
  • 1
  • 10