我們正在準備Likert類型的比例。必須允許主題按1-9的數字。我們知道ListenChar,但它抑制了整個鍵盤。我們如何抑制非數字鍵?抑制PsychToolBox中的特定按鍵
while(1)
ch = GetChar;
if ch == 10 %return is 10 or 13
%terminate
break
else
response=[response ch];
end
end
我們正在準備Likert類型的比例。必須允許主題按1-9的數字。我們知道ListenChar,但它抑制了整個鍵盤。我們如何抑制非數字鍵?抑制PsychToolBox中的特定按鍵
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
也許我misunderstanding-爲什麼不只聽整個鍵盤,解析它是否是一個數字,如果不是,就扔掉? –
你是我們嘗試過的東西,但它只適用於輸入密鑰nu爲數字,我會添加上面的示例代碼 –