# Controlling Slider in Pavlovia with keyboard

**URL:** <https://discourse.psychopy.org/t/controlling-slider-in-pavlovia-with-keyboard/11712>\
**Category:** Online experiments\
**Created:** [March 24, 2020, 6:07pm UTC](https://discourse.psychopy.org/t/controlling-slider-in-pavlovia-with-keyboard/11712 "2020-03-24T18:07:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![enniin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/enniin/32/7588_2.png) [@enniin](https://discourse.psychopy.org/u/enniin)\
**Post date:** [March 24, 2020, 6:07pm UTC](https://discourse.psychopy.org/t/controlling-slider-in-pavlovia-with-keyboard/11712/1 "2020-03-24T18:07:00Z")

</div>

I’m trying to create a 7-point Likert rating scale in Pavlovia, and I want keyboard left and right arrows to control position of the marker. Accepting and end of routine should happen when space is hit.

I’m trying to make it happen with the following javascript snippet in Each Frame, but no cigar:

```auto
try {
    arrowKeys = trial_response.getKeys({keyList: ['left', 'right'], waitRelease: false});
      if (arrowKeys.length > 0) {
          if (arrowKeys[0].name === 'left') {
              slider.setRating(slider.getRating()-1);
              slider.setMarkerPos(slider.getRating()-1);
          }
          if (arrowKeys[0].name === 'right') {
              slider.setRating(slider.getRating()+1);
              slider.setMarkerPos(slider.getRating()+1);
          }
      }
}
catch (err) {}

```

Corresponding python code works just fine (offline):

```auto
arrowKeys = event.getKeys(keyList=['left','right'])
if arrowKeys:
    if 'left' in arrowKeys:
        slider.markerPos -= 1
    if 'right' in arrowKeys:
        slider.markerPos += 1

```

Any hints on where I’m going wrong? I also tried using the private slider.\_recordRating() function, but that didn’t work either.

---

<div class="post-metadata">

**Author:** ![enniin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/enniin/32/7588_2.png) [@enniin](https://discourse.psychopy.org/u/enniin)\
**Post date:** [March 30, 2020, 10:01am UTC](https://discourse.psychopy.org/t/controlling-slider-in-pavlovia-with-keyboard/11712/2 "2020-03-30T10:01:12Z")

</div>

Update on this: I finally managed to get this to work, with the following code snippet:

```auto
arrowKeys = psychoJS.eventManager.getKeys({keyList:['left', 'right']});
if (arrowKeys.includes('left')) {
    slider_practice._recordRating(slider_practice.getRating()-1);
    }
if (arrowKeys.includes('right')) {
    slider_practice._recordRating(slider_practice.getRating()+1);
    }

```

Also, in the Begin Routine the value is preset to the default value to make things easier for the subjects, and also to prevent None-values:

`slider_practice._recordRating(4.0);`

The entire (test case) code is viewable here: [https://gitlab.pavlovia.org/jennisaaristo/slider\_test](https://gitlab.pavlovia.org/jennisaaristo/slider_test) .

---

<div class="post-metadata">

**Author:** ![Phili](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/phili/32/15794_2.png) [@Phili](https://discourse.psychopy.org/u/Phili)\
**Post date:** [April 23, 2021, 7:47am UTC](https://discourse.psychopy.org/t/controlling-slider-in-pavlovia-with-keyboard/11712/3 "2021-04-23T07:47:58Z")

</div>

Hey @enniin, thank your very much for your posts. I was struggeling with the same problem.

A quick update:  
For me it worked when I removed the “\_” in front of the recordRating. The code I am currently running is:

```auto
arrowKeys = psychoJS.eventManager.getKeys({keyList:['e', 'o']});
if (arrowKeys.includes('e')) {
    confidence.recordRating(confidence.getRating()-1);
    }
if (arrowKeys.includes('o')) {
    confidence.recordRating(confidence.getRating()+1);
    };

```

My slider is named confidence and I use “e” and “o” instead of left right.
