| Reference | Downloads | Github

keyDown/keyUp to start/stop an event in psychoJS

Hi everyone,

I’m having a lot of trouble solving a problem in PsychoJS. I want a rating bar to move when either the left or right arrow keys are pushed and stop when the key is released. I can get the bar to start moving but I simply cannot find a way to make PsychoJS recognize any form of KEYUP.

I have created a toy example of the issue here:

This is make or break for my study as I’m taking an existing Presentation paradigm online (so using another measurement is unfortunately not available).

Any help would be greatly appreciated!

EDIT: even any general pointers about the best way to approch the problem would be really helpful.

1 Like

Hi all,

If anyone needs both keyup and keydown in PsychoJS you can do the following:


function TrialRoutineEachFrame(trials) {
let KEY_TYPE_ARROW_LEFT = "ArrowLeft";
let KEY_TYPE_ARROW_RIGHT = "ArrowRight";
let KEY_DOWN = "keydown";
let KEY_UP = "keyup";
let LEFT_BOUNDARY = -360;

window.onkeydown = window.onkeyup = window.onkeypress = slideHandler;

function slideHandler(e) {

    // start of Arrow LEFT keydown
    if (e.type === KEY_DOWN && e.key === KEY_TYPE_ARROW_LEFT) {
        if (xaxis > LEFT_BOUNDARY) {
            xaxis = xaxis - 5;
        } else {
            xaxis = LEFT_BOUNDARY;

    // start of Arrow LEFT keyup
    if (e.type === KEY_UP && e.key === KEY_TYPE_ARROW_LEFT) {

    // start of Arrow RIGHT keydown
    if (e.type === KEY_DOWN && e.key === KEY_TYPE_ARROW_RIGHT) {
        if (xaxis < RIGHT_BOUNDARY) {
            xaxis = xaxis + 5;
        } else {
            xaxis = RIGHT_BOUNDARY;

    // start of Arrow RIGHT keyup
    if (e.type === KEY_UP && e.key === KEY_TYPE_ARROW_RIGHT) {


xaxis is a variable that set the x pos of a polygon and the boundries defined how far left or right it could move. When the button was down the polygon moved and stopped when released.

1 Like