psychopy.org | Reference | Downloads | Github

Controlling slider with keyboard

Hi,

I am wondering if anyone can help with a code snippet for controlling the slider with the keyboard?

I have tried using the following code:

arrowKeys = event.getKeys(keyList=[‘left’,‘right’])

if 'left' in arrowKeys:
    slider.markerPos -= 1
if 'right' in arrowKeys:
    slider.markerPos += 1

But when I run the experiment it says the event is undefined. Does anyone have a simple way to control the marker on the slider using the keyboard?

Many thanks for your help!

Hello LisaFitz,

the following worked for me offline:

Begin routine tab

event.clearEvents('keyboard')
slider.markerPos = 3

Each frame tab

keys = event.getKeys()

if len(keys):
    if 'left' in keys:
        slider.markerPos = slider.markerPos - 1
    elif 'right' in keys:
        slider.markerPos = slider.markerPos  + 1 

Best wishes Jens

2 Likes

Hi Jens,

Thanks so much for your reply!

I’ve tried to run that code but I am experiencing a similar error that clear events undefined. There must be something obvious I’m missing.

Many thanks,

Lisa

Hello LisaFitz

does your code-component follow the slider-component?

Best wishes Jens

1 Like

Hello Lisa,

do you test the experiment locally or on-line? In case you testing it on-line, you might want to add the following line to a Begin experiment tab JavaScript only code-component

event = psychoJS.eventManager;

Check wakecarter’s crib sheet for more information.

Best wishes Jens

Hi Jens,

Yes! I was just about to mention that I have JS only code components in my tasks so I need to run it online.

Thanks, I’ll try this!

Hi Jens,

Thank you so much for your time and advice. That worked! I can’t thank you enough :slight_smile:

Hello,

That is good news. Do you mind marking it as a solution? Then it will be found faster.

Best wishes Jens

1 Like

Hi Jens,

First of all, this has been extremely helpful for me as well, so thank you very much!
However, I have one question. When I use this solution slider.response is still ‘None’ on the .csv file on data. I get the participants’ answer from the log but it is not very elegant… I have tried with getHistory or getRating but they only work when I use the mouse to move the slider…
Not sure if you have any insights on this…
Thanks a lot in advance!

Pablo

Dear pablorom,

did you check the Store rating on the Data tab of the slider properties?

Rating responses are stored in the csv when this option is checked, at least in my case.

Best wishes Jens

1 Like

Hi Jens,

Thanks a lot for your response!
Yes, it is checked. I think that the problem might be that, in my task, the participants do not push a button to validate the response and that is why PsychoPy does not save the answer on the csv. The task will be used in an fMRI study and I will most likely use only two buttons (left and right). As you probably know, the mobility inside an fMRI scanner is limited and the will use one hand to hold a “panic button” to stop the machine and get them out, and the other hand to hold the two-button control pad with two buttons (left and right). They will have 10 seconds to answer each item and we will record the last position of the slider. Another option might be pressing both buttons (left and right) at once to validate the response but I am not sure how to do it…
Does this make sense to you?
BW,

Pablo

Hello,

set your slider to the duration you need and add the following to the End routine tab:

thisExp.addData("Rating", slider.markerPos)

The participant will be able to move the slider response to the desired point on the scale and then has simply to wait for the routine (presentation duration of the slider) to finish.

Best wishes Jens

1 Like

That worked, Jens, thank you very much, I really appreciate it!
BW,

Pablo

Before you leave this topic, I would like to use slider in Builder, but have the marker default to a particular value (like ‘marker start’ in the old Rating scale). From Pablo’s example I thought I could proceed the slider with a code segment like:
mySlider.markerPos = 6 #in Begin Routine
or
mySlider.markerPos = 6 #in Begin Routine
mySilder.draw()

I can get the slider to work perfectly but I would like it to default to a value & marker at that value. I have tried the code component both before and after the slider component. Thanks.

This should work but I think only if it occurs after the slider has started, so the code component would need to come below the slider, or you could put this in Each Frame. Something like

Begin Routine
slider_started = False

Each Frame

if slider_started == False:
     slider_started = True
     mySlider.markerPos = 6