如何根据键盘响应显示刺激
How to display stimuli depending on keyboard response
我在 Windows 10 系统的 Psychtoolbox 上使用 MATLAB R2020a。我正在尝试根据用户提供的键盘响应在屏幕上呈现刺激。我正在使用 if-else 语句对此进行编码,但只显示注视十字,没有显示任何错误消息。当出现问号时,用户按下箭头键,并以此为基础,呈现下一个刺激。
我也不确定如何区分开始任务时给出的 Kb 响应和问号后给出的响应。有人可以帮我解决这个问题吗?
% 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
在您的示例中,您正在调用 KbCheck
函数并将结果与 leftKey
、rightKey
等进行比较。但是 KbCheck 的第一个输出只是一个布尔值,指示是否任何键都按下了。相反,使用 KbCheck 函数中的 keyCode 矩阵,并比较每个键是否被按下。
在您的示例中,您也不是连续轮询键盘,而是注视 1 秒,然后在该秒过去后检查一次键盘。下面是一个示例,其中包含更正的键比较,并在注视开始后检查键盘 1 秒。在这里,我将绘制到屏幕的调用替换为简单地将消息打印到 MATLAB 控制台,以简化示例。
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();
我在 Windows 10 系统的 Psychtoolbox 上使用 MATLAB R2020a。我正在尝试根据用户提供的键盘响应在屏幕上呈现刺激。我正在使用 if-else 语句对此进行编码,但只显示注视十字,没有显示任何错误消息。当出现问号时,用户按下箭头键,并以此为基础,呈现下一个刺激。
我也不确定如何区分开始任务时给出的 Kb 响应和问号后给出的响应。有人可以帮我解决这个问题吗?
% 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
在您的示例中,您正在调用 KbCheck
函数并将结果与 leftKey
、rightKey
等进行比较。但是 KbCheck 的第一个输出只是一个布尔值,指示是否任何键都按下了。相反,使用 KbCheck 函数中的 keyCode 矩阵,并比较每个键是否被按下。
在您的示例中,您也不是连续轮询键盘,而是注视 1 秒,然后在该秒过去后检查一次键盘。下面是一个示例,其中包含更正的键比较,并在注视开始后检查键盘 1 秒。在这里,我将绘制到屏幕的调用替换为简单地将消息打印到 MATLAB 控制台,以简化示例。
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();