Matlab 抑制PsychToolBox中的特定按键

Matlab 抑制PsychToolBox中的特定按键,matlab,psychtoolbox,Matlab,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 = GetCh

我们正在准备利克特式量表。必须允许受试者只按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
while(1)
ch=GetChar;
如果ch==10%,则回报率为10或13
%终止
打破

elseif(ch>48)和(chPsychtoolbox包括通过
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

也许我误解了-为什么不听听整个键盘,如果它是一个数字,就解析它,如果它不是,就扔掉它呢?你很有可能我们用getchar尝试了一些东西,但它只适用于数字的enter键nu,我将添加上面的示例代码