# Checking for keyboard response using code component online

**URL:** <https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202>\
**Category:** Online experiments\
**Created:** [July 21, 2020, 1:40pm UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202 "2020-07-21T13:40:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felicia](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Felicia](https://discourse.psychopy.org/u/Felicia)\
**Post date:** [July 21, 2020, 1:40pm UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202/1 "2020-07-21T13:40:48Z")

</div>

URL of experiment: [https://gitlab.pavlovia.org/Felicia/sliderexp](https://gitlab.pavlovia.org/Felicia/sliderexp)

Description of the problem:

Hey there,

I am using a slider element in an online experiment. At first, I let them continue to the next question using a keyboard component (so pressing SPACE would end the routine). But like this they could jump to the next question without choosing a rating on the slider which lead to missing answers. So I exchanged the keyboard component for a code component where I check for a rating first before checking for a keyboard response. Locally this works absolutely fine, but online it doesn’t work. Now I can’t jump to the next question without choosing a rating, but even after choosing a rating, the routine will not end when pressing SPACE.

Does anyone know how to solve this? I guess there is something wrong with the js syntax. I tried to do it like in the auto-generated js file from the previous version where I used a keyboard component to end the routine. This is the js part of my code component:

```auto
key_response = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
let theseKeys = key_response.getKeys({keyList: ['space'], waitRelease: false});

if ((slider.getRating() !== null)) {
    valid_response = true;
}
if (valid_response) {
    if (theseKeys.length > 0) {
        if ((theseKeys[0] === "space")) {
            continueRoutine = false;
        }
    }
}

```

this is the PsychoPy-Version which works locally:

```auto
key_response = keyboard.Keyboard()
theseKeys = key_response.getKeys(keyList=['space'], waitRelease=False)

if slider.getRating() != None:
    valid_response = True

if valid_response:
    if len(theseKeys):
        if theseKeys[0] == "space":
            continueRoutine = False

```

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 22, 2020, 5:39am UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202/2 "2020-07-22T05:39:12Z")

</div>

Switch the code type to Both and change null to undefined

Best wishes

Wakefield

---

<div class="post-metadata">

**Author:** ![Felicia](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Felicia](https://discourse.psychopy.org/u/Felicia)\
**Post date:** [July 22, 2020, 7:30am UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202/3 "2020-07-22T07:30:03Z")

</div>

Hey Wakefield,

thanks again for your help! I tried it with undefined, but unfortunately it still does not work. Do you have any other idea what I could do?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 22, 2020, 9:48pm UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202/4 "2020-07-22T21:48:11Z")

</div>

I haven’t managed to get the new keyboard class working online. Try event.getKeys() with event defined in code\_JS as per my crib sheet

---

<div class="post-metadata">

**Author:** ![Felicia](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@Felicia](https://discourse.psychopy.org/u/Felicia)\
**Post date:** [July 23, 2020, 7:28am UTC](https://discourse.psychopy.org/t/checking-for-keyboard-response-using-code-component-online/15202/5 "2020-07-23T07:28:38Z")

</div>

That worked 🙂

Thank you so much!
