matlabkeyboardscreenpsychtoolbox

How to display stimuli depending on keyboard response


I am using MATLAB R2020a on Psychtoolbox on a Windows 10 system. I am trying to present a stimulus on the screen based on the keyboard response provided by the user. I am using if-else statements to code for this but only the fixation cross is displayed and there is no error message displayed. The user presses an arrow key when the question mark is presented, and based on this, the next stimulus is presented.

I also am not sure how to differentiate between the Kb response given to start the task and that given after the question mark. Could someone please help me with this?

% Question mark
Screen('FillRect', window, white);
Screen('DrawTextures', window, imageTexture, [], dstRects);
Screen('Flip', window)
KbStrokeWait;

% Experimental instructions
Screen('FillRect', window, grey);
Screen('DrawText', window, line3, screenXpixels*0.2, screenYpixels*0.4, white);
Screen('DrawText', window, line4, screenXpixels*0.2, screenYpixels*0.5, white);
Screen('Flip', window);
KbStrokeWait;

% Fixation cross
Screen('DrawLines', window, allCoords,lineWidthPix, white, [xCenter yCenter], 2);
Screen('Flip', window, [], 1);
WaitSecs(1);

% Draw rect frames

% Check the keyboard to see if a button has been pressed
    [keyIsDown,secs, keyCode] = KbCheck;
    
% Present stimulus   
if KbCheck == 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

Solution

  • In your example you are calling the KbCheck function and comparing the result to leftKey, rightKey etc. However the first output of KbCheck is just a boolean value that indicates whether any key is down. Instead, use the keyCode matrix from the KbCheck function, and compare whether each key is pressed.

    In your example, you are also not continuously polling the keyboard, instead the fixation is presented for 1 second, then the keyboard is checked once after that second has elapsed. Below is an example with corrected key comparison, and the checking of the keyboard for 1 second following fixation onset. Here I have replaced the calls to draw to the screen to simply printing messages to the MATLAB console, to simplify the example.

    leftKey = KbName('LeftArrow');
    rightKey = KbName('RightArrow');
    upKey = KbName('UpArrow');
    
    % don't display write keyboard responses into the MATLAB window
    ListenChar(2);
    
    % how long to poll for a response following fixation onset
    responseSeconds = 1;
    
    % Question mark
    disp('?');
    KbStrokeWait(-3);
    
    % Experimental instructions
    disp('Instructions Here');
    KbStrokeWait(-3);
    
    % Fixation cross
    
    disp('+');
    fixationOnset = GetSecs();
    keyPressed = 0; % store whether a response has been made
    
    while (GetSecs - fixationOnset) <= responseSeconds
        
        % Check the keyboard to see if a button has been pressed
        [keyIsDown,secs, keyCode] = KbCheck(-3);
        
        if keyPressed == 0 && keyIsDown
            if keyCode(leftKey)
                disp('pressed the left key');
            elseif keyCode(rightKey)
                disp('pressed the right key');
            elseif keyCode(upKey)
                disp('pressed the up key');
            end
            keyPressed = 1; % indicate the response has been made
        end
        
        % Wait 1 ms before checking  again to prevent
        % overload of the machine at elevated priority
        WaitSecs(0.001);
    end
    
    disp('response loop over');
    
    WaitSecs(0.5);
    
    disp('wait time over');
    
    ListenChar();