# Controlling slider with keyboard

**URL:** <https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317>\
**Category:** Builder\
**Created:** [April 15, 2021, 10:47am UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317 "2021-04-15T10:47:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![LisaFitz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lisafitz/32/13767_2.png) [@LisaFitz](https://discourse.psychopy.org/u/LisaFitz)\
**Post date:** [April 15, 2021, 10:47am UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/1 "2021-04-15T10:47:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 15, 2021, 1:07pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/2 "2021-04-15T13:07:40Z")

</div>

Hello LisaFitz,

the following worked for me offline:

Begin routine tab

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

```

Each frame tab

```auto
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

---

<div class="post-metadata">

**Author:** ![LisaFitz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lisafitz/32/13767_2.png) [@LisaFitz](https://discourse.psychopy.org/u/LisaFitz)\
**Post date:** [April 15, 2021, 1:42pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/3 "2021-04-15T13:42:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 15, 2021, 2:23pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/4 "2021-04-15T14:23:27Z")

</div>

Hello LisaFitz

does your code-component follow the slider-component?

 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/c/5caf8ba53b6a99f5a45f342773bf750d7b29e0f7.png)

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 15, 2021, 2:27pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/5 "2021-04-15T14:27:45Z")

</div>

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

```auto
event = psychoJS.eventManager;

```

Check wakecarter’s [crib sheet](https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit?usp=sharing) for more information.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![LisaFitz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lisafitz/32/13767_2.png) [@LisaFitz](https://discourse.psychopy.org/u/LisaFitz)\
**Post date:** [April 15, 2021, 3:43pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/6 "2021-04-15T15:43:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![LisaFitz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lisafitz/32/13767_2.png) [@LisaFitz](https://discourse.psychopy.org/u/LisaFitz)\
**Post date:** [April 15, 2021, 5:01pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/7 "2021-04-15T17:01:06Z")

</div>

Hi Jens,

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 16, 2021, 6:08am UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/8 "2021-04-16T06:08:03Z")

</div>

Hello,

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

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![pablorom](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@pablorom](https://discourse.psychopy.org/u/pablorom)\
**Post date:** [April 16, 2021, 7:46pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/9 "2021-04-16T19:46:30Z")

</div>

> [@JensBoelte](#):
>
> ```auto
> keys = event.getKeys()
> 
> if len(keys):
> if 'left' in keys:
> slider.markerPos = slider.markerPos - 1
> elif 'right' in keys:
> slider.markerPos = slider.markerPos + 1 
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 19, 2021, 8:47am UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/10 "2021-04-19T08:47:29Z")

</div>

Dear pablorom,

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

 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/b/eb515019a027098ba8c00f5c32a579a9d48a71d6.png)

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

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![pablorom](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@pablorom](https://discourse.psychopy.org/u/pablorom)\
**Post date:** [April 19, 2021, 12:37pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/11 "2021-04-19T12:37:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 19, 2021, 2:13pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/12 "2021-04-19T14:13:16Z")

</div>

Hello,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![pablorom](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@pablorom](https://discourse.psychopy.org/u/pablorom)\
**Post date:** [April 19, 2021, 2:16pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/13 "2021-04-19T14:16:24Z")

</div>

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

Pablo

---

<div class="post-metadata">

**Author:** ![Bill\_Prinzmetal](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Bill\_Prinzmetal](https://discourse.psychopy.org/u/Bill_Prinzmetal)\
**Post date:** [May 30, 2021, 11:54pm UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/14 "2021-05-30T23:54:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 31, 2021, 7:39am UTC](https://discourse.psychopy.org/t/controlling-slider-with-keyboard/22317/15 "2021-05-31T07:39:46Z")

</div>

> [@Bill\_Prinzmetal](#):
>
> mySlider.markerPos = 6 #in Begin Routine

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

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

```
