Keyboard entry in PsychoJS

URL of experiment: https://run.pavlovia.org/MJB/libet_keyentry/html/

Description of the problem:

I am trying to adapt the keyboard entry demo provided by @dvbridges for my experiment.
I want to restrict keyList of allowed keys to just numerical keys, return, and backspace.
The code below – adapted from the demo – throws an error because it does not like keyList. How do I use this in JS?

let theseKeys = psychoJS.eventManager.getKeys(keyList=['0','1','2','3','4','5','6','7','8','9','0','backspace','return']);
if (theseKeys.length > 0) {  // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 
  }


if (textAdd === 'return') {
    textAdd = '';  
    continueRoutine = false;  
} else if (textAdd === 'backspace') {
    text.text = '';
    textAdd = undefined;
    estimateVale = 0;
    handOri = 90;
} else if (textAdd !== undefined) {
        text.text = text.text + textAdd;
        estimateVale = Number.parseFloat(text.text);
        handOri = ((estimateVale * 6) + 90);
        textAdd = undefined;
}

I believe you need to wrap KeyList in curly brackets:

let theseKeys = psychoJS.eventManager.getKeys({keyList= ['0','1','2','3','4','5','6','7','8','9','0','backspace','return']});

That does not seem to be it — throws a different error message then

Here’s the code I’ve used to get it to work, allowing the number keys and a few extra ones:

let theseKeys = psychoJS.eventManager.getKeys({keyList: ['delete', 'insert', 'end', 'down', 'pagedown', 'left', 'right', 'home', 'up', 'pageup', 'num_decimal', 'num_0', 'num_1', 'num_2', 'num_3', 'num_4', 'num_5', 'num_6', 'num_7', 'num_8', 'num_9', 'num_0', '.', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'backspace', 'return', 'period']});
if (theseKeys.length > 0) {  
  textAdd = theseKeys[theseKeys.length-1]; 
  }

Ignore the num keys and their numlock counterparts. This is also in the “each frame” tab of the code component. What is the error you are getting?

I had this issue today and added a keyboard component in the code in addition to the keyboard routine.

keys = event.getKeys()
if len(keys) > 0:
    if "t" in keys:
        if target_flag < 1:
            thisExp.addData('Target_RT', response.rt)
            target_flag = 1
    continueRoutine = False

N.B. This auto-translates because I have already defined event in an initial code_js.

HI @wakecarter. I am not sure I understand. Are you suggesting I use the code you cite?
What is target_flag?

@Marc_Buehner, the error you probably received was probably related to how you defined the keyList property. In JavaScript objects, you use a colon, not equals sign, to assign a value to a property. So, the original suggestion should work with an amendment. Try:

theseKeys = psychoJS.eventManager.getKeys({keyList: ['0','1','2','3','4','5','6','7','8','9','0','backspace','return']});

I’m offering an alternative method in case you can’t get David’s method working.

In my code I want to record the first time ‘t’ is pressed but only end the routine if something else is pressed. Once t has been pressed I set the target flag to 1 in order to ignore future presses of t.

1 Like