Controlling Slider in Pavlovia with keyboard

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:

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):

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.

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

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 .

4 Likes

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:

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.