%exampleKb2.m % Displays the key number when the user presses a key. % a simplified version of KbDemo by IF 4/2007 WaitSecs(0.5); disp('1 of 4. Testing KbCheck and KbName: press a key to see its number'); disp('Press the escape key to exit.'); escapeKey = KbName('ESCAPE'); while KbCheck; end % Wait until all keys are released. while 1 % Check the state of the keyboard. [ keyIsDown, seconds, keyCode ] = KbCheck; % If the user is pressing a key, % then display its code number and name. if keyIsDown % Note that we use find(keyCode) because keyCode is an array. str=['You pressed key ', find(keyCode), ... ' which is ', KbName(keyCode)]; disp(str) if keyCode(escapeKey) break; end % If the user holds down a key, % KbCheck will report multiple events. % To condense multiple 'keyDown' events into a single event, % once a key has been pressed % we wait until all keys have been released % before going through the loop again while KbCheck; end end end