在 MATLAB 的 PTB 中使用 getchar

using getchar in PTB in MATLAB

    while 1

    ch = GetChar
    KbWait
    if (ch>48) && (ch<53) 
         trial(j).RT =  GetSecs - startTime ;
       break;
    end
    end

这是我的示例代码,我正在处理 Stroop 任务,我希望能缩短反应时间。反应时间从刺激呈现开始,到按键结束。我使用上面的代码来抑制整个键盘期望 1-2-3-4 的数字。 但是,有时可以按下所有键而不仅仅是 1-2-3-4。我已经尝试了很多次,它有时会抑制按键,有时却不会。我真的不明白原因。

GetChar() 函数等待按键,或从队列中检索先前按下的键:http://docs.psychtoolbox.org/GetChar。可能发生的情况是您在队列中有之前的按键,这些按键正在被 GetChar 读取,即使它们不是最近的按键。

但是,Psychtoolbox 开发人员建议不要使用 GetChar() 函数来收集响应时间。这是由于 GetChar() 相对于其他函数(如 KbCheck())的时间预测。

以下代码片段可用于轮询键盘的响应时间:

% find the keyscan codes for the first four number keys
% (top of the keyboard keys, number pad keys have different codes)
keysToWaitFor = [KbName('!1'), KbName('2@'), KbName('3#'), KbName('4$')];

responded = 0;
while responded == 0

    [tmp,KeyTime,KeyCode] = KbCheck(-3);

    if KeyCode(keysToWaitFor)
        trial(j).RT =  KeyTime - startTime;
        responded = 1;
    end

    % time between iterations of KbCheck loop
    WaitSecs(0.001);
end