# Building a keyboard slider that works online

**URL:** <https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268>\
**Category:** Builder\
**Tags:** pavlovia\
**Created:** [May 29, 2021, 4:28pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268 "2021-05-29T16:28:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mellon](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mellon](https://discourse.psychopy.org/u/Mellon)\
**Post date:** [May 29, 2021, 4:28pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/1 "2021-05-29T16:28:15Z")

</div>

**OS** (Win10):  
**PsychoPy version** (v2021.1.4):  
**What are you trying to achieve?:**  
Trying to build a slider whose marker moves with arrow keys and people can select their answer with the RETURN key.

**What did you try to make it work?:**  
Followed the solution provided by @jon here: [Rating scale reponse failure via keyboard](https://discourse.psychopy.org/t/rating-scale-reponse-failure-via-keyboard/10141)

```auto
if slider_kbControl.markerPos is None:
    # sets to our first position (or a random number?)
    # and makes marker visible
    slider_kbControl.markerPos = 3

#check keys (just the ones we want)
keysPressed = defaultKeyboard.getKeys(['left', 'right', 'return'])
for key in keysPressed:
    if key == 'left':
        slider_kbControl.markerPos -= 0.5 # subtract 0.5
    elif key == 'right':
        slider_kbControl.markerPos += 0.5 # add 0.5
    elif key == 'return':
        # confirm rating by setting to current markerPos
        slider_kbControl.rating= slider_kbControl.markerPos

```

**What specifically went wrong when you tried that?:**  
The slider does not work ONLINE. No marker is shown and the keyboard method for responding is not working. The response is recorded (and the routine ends) only when a mouse click happens.

---

<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 29, 2021, 6:15pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/2 "2021-05-29T18:15:01Z")

</div>

Have a look at my interactive slider demo which works with arrow keys and mouse hover.

> [@PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319):
>
> If you have routines or experiments that are likely to be useful to others, please create a post in this thread, editing your post to add new demos so there is one post per creator. Ideally set your demos so they can be tested without consuming a credit (if you don’t have a site license) by adding a final routine that can’t end, disabling save incomplete results and adding a small number of credits. Please do not discuss the demos in this thread or create more than one post unless you have a la…

---

<div class="post-metadata">

**Author:** ![Mellon](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mellon](https://discourse.psychopy.org/u/Mellon)\
**Post date:** [May 29, 2021, 6:41pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/3 "2021-05-29T18:41:19Z")

</div>

Hi @wakecarter, thanks for the quick reply. I’ve had a look at your demo, but unfortunately, I get some errors concerning “slider\_granularity” being not defined (I’ve basically copy-pasted your codes in my experiment.

Also, did I get it right that I just need one of the two routines in your demo (trial or iSlide)?

Sorry, I’m new to PsychoPy, and I’m really trying everything I can to make it work.

Cheers

---

<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 29, 2021, 6:56pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/4 "2021-05-29T18:56:22Z")

</div>

You also need the contents of the start routine. However, you do only need trial or iSlider.

---

<div class="post-metadata">

**Author:** ![Mellon](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mellon](https://discourse.psychopy.org/u/Mellon)\
**Post date:** [May 29, 2021, 7:36pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/5 "2021-05-29T19:36:32Z")

</div>

Yes, I’ve also included the contents of the start routine, that is why I don’t know what is going on. But if that is the only solution, I will try again. I might be doing something in the wrong way.

Thank you.

---

<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 29, 2021, 8:06pm UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/6 "2021-05-29T20:06:48Z")

</div>

Check that slider\_granularity is set in Begin Experiment in a code component that is either above the slider or in an earlier routine.

---

<div class="post-metadata">

**Author:** ![Mellon](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mellon](https://discourse.psychopy.org/u/Mellon)\
**Post date:** [May 30, 2021, 10:06am UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/7 "2021-05-30T10:06:43Z")

</div>

Thank you @wakecarter, that was it!

I also just discovered that the online experiments only work with the UK keyboard (not the Italian one, as left and right keys correspond to up and down). Probably that’s why I wasn’t able to make it work with all the material I previously found on this forum.

Thank you again!

---

<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 30, 2021, 10:08am UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/8 "2021-05-30T10:08:12Z")

</div>

If the issue is that left is up and right is down on an Italian keyboard you can always code it for both eventualities.

---

<div class="post-metadata">

**Author:** ![Mellon](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Mellon](https://discourse.psychopy.org/u/Mellon)\
**Post date:** [May 30, 2021, 10:12am UTC](https://discourse.psychopy.org/t/building-a-keyboard-slider-that-works-online/23268/9 "2021-05-30T10:12:44Z")

</div>

Yes, I will try and do that, thank you 🙂
