How to present stimulus depending on keyboard response

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!

    % 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

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

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.

[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.

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

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

Thank you @dcnieho! Indeed this worked