psychopy.org | Reference | Downloads | Github

Problem getting a list of pressed keys in JavaScript

**OS: macOS Catalina 10.15.4
**PsychoPy version: 2020.1.2
**Standard Standalone: YES

What are you trying to achieve?:
Get a list of keys pressed for a specific Keyboard component in an online experiment created in the builder of Psychopy.

What did you try to make it work?:
I have a keyboard component which is set to allow only two keys. Additionally, I have a code component which is set to check the number of keys pressed every frame. The code works well when running the experiment locally in Python. The following minimal code gets a list of the pressed keys (which always has one element because the keyboard component was set to store only the last key pressed), and store its length in a new variable.

Python code:

numKeysPressed = len(myKeyboard.keys)

However, when piloting the test online in Pavlovia, its equivalent in JavaScript returns an error specifying that “myKeyboard.keys” is undefined.

JavaScript code:

numKeysPressed = myKeyboard.keys.length;

After trying to figure out the source of the problem and comparing the scripts in Python and Javascript, I found that there is slight but important difference. When the parameters of the keyboard component are set before starting the routine, in Python “myKeyboard.keys” is defined as an empty list while in JavaScript it is set as undefined. See the snippets below taken form both scripts uploaded to Pavlovia.

In Python

In JavaScript

If I manually change the JavaScript script and define the “keys” and “rt” attribute as empty lists. It works exactly as it is supposed to work. The problem is that having both attributes as undefined returns the error I mentioned in the beginning when attempting to get the list of pressed keys. In Python it works well and does show any error because the attribute “keys” is defined as an empty list.

Every time I make a change now and compile the while script, these two attributes are set automatically to undefined. It would be great if by default these attributes are set automatically as empty lists. It is a hassle to modify every single time these values whenever something is changed in the experiment and the script is compiled.

Can I somehow change how this attributes are defined automatically every time the script is compiled?

In the mean time, to avoid to have to modify the script of the experiment manually every time it is compiled, I added the following lines of JavaScript code just before the time I make use of the “keys” and “rt” attributes of the keyboard component. This is a work around to solve the problem with the undefined attributes caused by Psychopy when the script of the experiment is compiled for online testing. It defines both attributes as empty lists (as in the Python version of the code) in case they are set to undefined. It works well.

I used the code below in the “every frame” tab in a code component.

if ((myKeyboard.keys === undefined)) {
    myKeyboard.keys = [];
    myKeyboard.rt = [];
}

numKeysPressed = myKeyboard.keys.length;