| Reference | Downloads | Github

TypeError: Cannot read property 'getKeys' of undefined

URL of experiment:

Description of the problem: I am attempted to translate the Python code for my age question in my PsychoPy template file javascript.

If I have keys = event.getKeys(); then I get an error message TypeError: Cannot read property ‘getKeys’ of undefined
Whenever I’ve made a change which avoids this error, the flashing cursor code works but keys pressed aren’t registered.

I made modifications using the inputText example, but it didn’t change the error.

I am hoping that the auto translate will work if I can clean up my Python code, since I will hopefully be writing and converting several experiments over the next month.

Best wishes,


You’re using code component of type auto->JS, right?

event.getKeys() doesn’t exist in JS (event doesn’t really exist in JS). What you want is to use is a keyboard component in the builder, and if you need to reference the keyboard in the code of a given trial, use that. so, keyCompnent.getKeys()

If for whatever reason that doesn’t work, there’s also psychoJS.eventManager.getKeys(), which functions largely like event.getKeys().

Thank you. I thought I’d tried psychoJS.eventManager.getKeys() already but you’ve encouraged me to try it again. I don’t have a keyboard component in the routine, so I thought I’d go straight to the generic one.

I was hoping that I might be able to edit my Python code in such a way that the auto translator would work (I do this when using Google translate for English to French). I also had to change thisExp.addData to psychoJS.experiment.addData but now I’m ready to move on to something more complicated.

Best wishes,


I would like to know if “psychoJS.eventManager.getKeys()” will wait for the key response.

Thank you

As in, whether it will function like event.waitKeys() in Python? No, though you can embed it in a loop to achieve the same result:

keysPressed = false;
while (keysPressed == false){
    keys = psychoJS.eventManager.getKeys();
    if (keys.length > 0){
        keysPressed = true;

Or something to that effect, it’s early so I don’t entirely trust myself on this one but something like that should achieve the same result as using event.waitKeys() in Python.

it did not solve my problem. Actually I have nested loop in my experiment. inner loop is to repeat trials and outer is to go to next row from the condition file. It is working fine but while saving data it does not save the last trial information, but whin i give as extra key press outside the inner loop, it saves all the data. Now with this I have extra key presses for each trial.

Can I somehow avoid these extra key press and save the data for the ladt trial also?

I though of adding a key press from code component but I am not able to do it successfully.

Can you help me in this?

Thank you in advance.

That one sounds like a more complicated problem. You should make a new post specifying that the problem you are having is that Pavlovia is not saving data from the final trial and someone who knows more about how data are saved might be able to help you better than I can.