psychopy.org | Reference | Downloads | Github

Listening to keypresses - Javascript code component

I am working on coding components in javascript so that my experiment will run on Pavlovia. Right now, I am not able to listen for particular keypresses in order for the experiment to do different things based on those keypresses.

I’m working with a toy version of the experiment, where pressing ‘1’ should end the routine, and pressing ‘2’ should take you out of the current loop. However, I am not able to make this work in Pavlovia. I’ve tried different methods of coding:

document.addEventListener('keydown', function (e) {
  if (e.key === '1') {
    continueRoutine = false;
  }
  if (e.key === '2') {
    trials.finished = true;
  }
});

and

if (key_resp.keys ==='1') {
    continueRoutine = false;
}

if (key_resp.keys ==='2') {
    trials.finished = true;

Any feedback on how to make this work would be much appreciated!