2

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

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

2 回答 2

0

如果您只想接受按键 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

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

于 2016-12-16T22:22:25.307 回答
0

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
于 2016-12-18T20:12:18.960 回答