抑制 PsychToolBox 中的特定按键

Suppressing a specific key press in PsychToolBox

我们正在准备李克特量表。必须允许受试者仅按 1-9 的数字。我们知道 ListenChar 但它会抑制整个键盘。我们如何抑制非数字键?

while(1)
    ch = GetChar;
    if ch == 10 %return is 10 or 13
        %terminate
        break
    else
        response=[response ch];
    end
end

如果您只想接受按键 1-9:

while(1)
    ch = GetChar;
    if ch == 10 %return is 10 or 13
        %terminate
        break
    elseif (ch>48) & (ch<58) %check if the char is a number 1-9
        response=[response ch];
        pause(0.1) %delay 100ms to debounce and ensure that we don't count the same character multiple times
    end
end

我还添加了去抖动,这样您就不会不小心多次记录单个输入。

Psychtoolbox 包含通过 RestrictKeysForKbCheck 限制监听特定键的功能。

以下代码限制了 1-9 的可能输入,加上 esc 键:

KbName('UnifyKeyNames'); % use internal naming to support multiple platforms
nums = '123456789';
keynames = mat2cell(nums, 1, ones(length(nums), 1));
keynames(end + 1) = {'ESCAPE'};
RestrictKeysForKbCheck(KbName(keynames));

下面是块的一个简单示例:

response = repmat('x', 1, 10); % pre-allocate response, similar to OP example

for ii = 1:10
    [~, keycode] = KbWait(); % wait until specific key press
    keycode = KbName(keycode); % convert from key code to char
    disp(keycode);

    if strcmp(keycode, 'ESCAPE')
        break;
    else
        response(ii) = KbName(keycode);
    end
    WaitSecs(0.2); % debounce
end

RestrictKeysForKbCheck([]); % re-enable all keys