| Reference | Downloads | Github

event.getKeys() not working online

URL of experiment:

Description of the problem:
I am trying to add a code component to my experiment so that the participants cannot end a routine if the slider components did not get an answer. My code works in Python but not online, I have activated the code component “Auto-JS” but it didn’t translate correctly. I don’t have any experience with JS and would be happy if someone could help, thanks in advance.

Update: I replaced keys = event.getKeys() by keys = psychoJS.eventManager.getKeys() and I don’t get the error message anymore but the code still doesn’t work --> it doesn’t allow the key response “space” to end routine only if the slider got a response

What does it do instead?

Nothing, instead of using this code component I have added a keyboard response to confirm the answers and added a start condition nameofslider_getRating() to the keyboard properties. This works if I have only one slider in the routine but not if I have multiple sliders in the same routine, maybe I am doing something wrong in the listing, I have tried these two listings: nameofslider1_getRating() & nameofslider2_getRating() and nameofslider1_getRating(), nameofslider2_getRating()

This is probably going to boil down to a logic issue with the conditional statements, but I also see an issue in the for loop.

Right now, it will only end on spacebar press if completed_ratings is exactly 1, but your code increments completed_ratings for each rating that is not null. So, if you are intending that they should advance after completing ALL ratings, then completed_ratings is greater than 1, not exactly 1.

However there may also be some issue in determining whether a rating has been filled out. I would recommend adding some logging statements to check which conditions are being met. Auto->JS will directly convert print statements to console.log statements, so here’s what the Python might look like:

keys = event.getKeys() # you'll still need to correct this after it's converted to JS

if 'space' in keys:
    print("spacebar pressed, checking ratings")
    completed_ratings = 0
    for slider in [sliderfiller]:
        if slider.getRating() is not None: # Note *slider.getRating*, not sliderfiller.getRating
            print("A slider has a rating")
            completed_ratings = completed_ratings + 1

    if completed_ratings == 1:
        print("Completed ratings is exactly 1, ending routine")
        continueRoutine = False

That should convert to a bunch of console.log statements you can see in your browser’s JS console, and it will help you figure out exactly which pieces of this aren’t working as intended.

Not sure if you were able to resolve this yet.
I was able to get it to wait for multiple responses by replacing keys = event.getKeys() with keys = psychoJS.eventManager.getKeys() and null with undefined, as such:

keys = psychoJS.eventManager.getKeys();
 if (_pj.in_es6("space", keys)) {
    completed_ratings = 0;
    for (var slider, _pj_c = 0, _pj_a = [rating_1, rating_2, rating_3, rating_4], _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
        slider = _pj_a[_pj_c];
        if ((slider.getRating() !== undefined)) {
            completed_ratings = (completed_ratings + 1);
    if ((completed_ratings >= 4)) {
        continueRoutine = false;