# How to present stimulus depending on keyboard response

**URL:** <https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371>\
**Category:** Programming Help\
**Created:** [August 26, 2020, 12:24pm UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371 "2020-08-26T12:24:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cai](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Cai](https://psychtoolbox.discourse.group/u/Cai)\
**Post date:** [August 26, 2020, 12:24pm UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/1 "2020-08-26T12:24:19Z")

</div>

I am using Psychtoolbox on MATLAB R2020a, on Windows 10. I am trying to present a certain stimulus depending on the user’s keyboard response. I have used if-else statements to achieve this but only the fixation cross is displayed. I also do not know how to differentiate between the keyboard response given to begin the trial and that which will be used to determine the next stimulus.

For instance, I want the user to press an arrow key. Then, the user presses any button to begin the next stage. The next stimulus presented depends on the response provided after the question mark. Thanks!

```matlab
    % Check the keyboard to see if a button has been pressed
    [keyIsDown,secs, keyCode] = KbCheck;

    % Present stimulus   
    if keyCode(leftKey) 
        Screen('DrawText', window, num2str(seq1), rightX, rightY, [0 1 0]);
        Screen('DrawText', window, num2str(seq3), leftX, leftY, [1 0 1]);
        Screen('Flip', window);
        WaitSecs(0.5);
    elseif KbCheck == rightKey
        Screen('DrawText', window, num2str(seq2), leftX, leftY, [1 1 0]);
        Screen('DrawText', window, num2str(seq3), rightX, rightY, [1 0 1]);
        Screen('Flip', window);
        WaitSecs(0.5);
    elseif KbCheck == upKey
        Screen('DrawText', window, num2str(seq1), rightX, rightY, [0 1 0]);
        Screen('DrawText', window, num2str(seq2), leftX, leftY, [1 1 0]);
        Screen('Flip', window);
        WaitSecs(0.5);
    end

```

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [August 26, 2020, 11:48pm UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/2 "2020-08-26T23:48:36Z")

</div>

Your question is quite unclear, but you can use `KbWait` rather than `KbCheck` if you need to wait until a user responds.

[http://psychtoolbox.org/docs/KbWait](http://psychtoolbox.org/docs/KbWait)

---

<div class="post-metadata">

**Author:** ![Cai](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Cai](https://psychtoolbox.discourse.group/u/Cai)\
**Post date:** [August 27, 2020, 12:41am UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/3 "2020-08-27T00:41:47Z")

</div>

Hi, thank you for that. Sorry, my question is a little confusing. I did put in a KbStrokeWait beforehand to wait for the user’s response. However, now I want the next stimulus presented to depend on what arrow the user pressed when the KbStrokeWait was executed. Hope that makes sense, thanks again.

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [August 27, 2020, 10:01am UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/4 "2020-08-27T10:01:02Z")

</div>

`[secs, keyCode, deltaSecs] = KbStrokeWait([deviceNumber][, untilTime=inf][, more optional args for KbWait]);`

Use the `keyCode` returned, which you can parse in the same way as you do for `KbCheck`. If you need to record multiple presses, make sure you store keyCode in an array, then reference the correct one to choose what to do next. Step debug your way through the code to see what exactly is going on.

---

<div class="post-metadata">

**Author:** ![makis6](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@makis6](https://psychtoolbox.discourse.group/u/makis6)\
**Post date:** [August 2, 2021, 8:03am UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/5 "2021-08-02T08:03:34Z")

</div>

> [@Ian-Max-Andolina](#):
>
> deviceNumber

what exactly is `deviceNumber` and how can I define this correctly?

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [August 2, 2021, 9:25am UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/6 "2021-08-02T09:25:25Z")

</div>

GetKeyboardIndices (or similar for mice or keypads) should do the trick

---

<div class="post-metadata">

**Author:** ![makis6](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@makis6](https://psychtoolbox.discourse.group/u/makis6)\
**Post date:** [August 2, 2021, 11:27am UTC](https://psychtoolbox.discourse.group/t/how-to-present-stimulus-depending-on-keyboard-response/3371/7 "2021-08-02T11:27:40Z")

</div>

Thank you @dcnieho! Indeed this worked
