抑制PsychToolbox中的特定密钥按下



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

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

相关内容

  • 没有找到相关文章

最新更新