我们正在准备一个李克特式比例。必须允许受试者只按1-9的数字。我们知道ListenChar,但它抑制了整个键盘。我们如何抑制非数字键?
while(1)
ch = GetChar;
if ch == 10 %return is 10 or 13
%terminate
break
else
response=[response ch];
end
end
答案 0 :(得分:1)
如果您只想接受按键1-9:
perl -pe 'tr/01//dc' < /dev/urandom
我还添加了去抖动,这样您就不会多次意外地记录单个输入。
答案 1 :(得分:1)
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