11 Listenchar (2): 12 while KbCheck;end Wait until all keys are released. 13This is a loop that flips around doing anything as long as KbCheck 14 sreports back that a key is pressed on the keyboard.Means that the 15sprogram doesn't continue until all keys have been released. 。KbCheck ●实时检查是否有按键行为,确保在开始实验前键盘的按键处于空闲 状态。 试-试:help KbCheck %这是函数KbCheck,三个输出变量,没有输入变量 [keylsDown,secs,keyCode]KbCheck; ·keylsDown:如果有按键被按到,keylsDown=1,KbCheck=1;如果没有按键, keylsDown=0,KbCheck=0 ●Secs:按键的电脑系统时刻点 ●keyCode: ●keyCode在Windows中是256*1的矩阵,每一个元素代表的是一个按键 ●如果某个按键(例如,第65个)被按到,那keyCode矩阵对应的元素(例如, 第65个元素)就为1,其他元素都为0 ●可通过find(keyCode)查看是哪个数字编码的按键 ●可通过KbName(keyCode)查看按键对应的字符是什么
l KbCheck l实时检查是否有按键行为,确保在开始实验前键盘的按键处于空闲 状态。 试一试:help KbCheck l [keyIsDown,secs,keyCode] = KbCheck; l keyIsDown:如果有按键被按到, keyIsDown=1, KbCheck=1;如果没有按键, keyIsDown=0, KbCheck=0 ; l Secs:按键的电脑系统时刻点 l keyCode: l keyCode在Windows中是256*1的矩阵,每一个元素代表的是一个按键 l 如果某个按键(例如,第65个)被按到,那keyCode矩阵对应的元素(例如, 第65个元素)就为1,其他元素都为0 l 可通过find(keyCode)查看是哪个数字编码的按键 l 可通过KbName(keyCode)查看按键对应的字符是什么 %这是函数KbCheck,三个输出变量,没有输入变量
11 ListenChar(2); 12 while KbCheck;end Wait until all keys are released. 13 &This is a loop that flips around doing anything as long as KbCheck 14 reports back that a key is pressed on the keyboard.Means that the 15sprogram doesn't continue until all keys have been released. ●while KbCheck;end ·只要KbCheck=1(检查到有按键被按住),就一直保持按键检查 状态,直到没有任何一个按键被按住,即所有按键都处于空闲状态, 为随后“正式”开始检查按键行为做好准备
l while KbCheck; end • 只要KbCheck=1(检查到有按键被按住),就一直保持按键检查 状态,直到没有任何一个按键被按住,即所有按键都处于空闲状态, 为随后“正式”开始检查按键行为做好准备
17 18曰whi1e1 %GetSecs获取当前电脑系统时间 19 swhile 1 is always true,so this loop will continue indefinitely. 20 Line 28 forceably breaks us out of the loop if the escape key is 21 号pressed 22 23 Check the state of the keyboard. 24 See if a key is currently pressed on the keyboard.If not,we skip 25 s the next for loop from lines 20-38,and basically check again almost 26 &immediately. 27 28 【keyIsDown,seconds,keyCode】=KbCheck; %KbCheck检查是否有按键行为 29 30 If the user is pressing a key, then display its code number and name. 32白 if keyIsDown %如果检测到按键行为,显示按键内容及时间 33 34 Note that we use find(keyCode)because keyCode is an array. 35 str=['You pressed key 'num2str(find(keyCode)),'which is',KbName(keyCode),'typed at time ',. num2str(seconds startsecs),'seconds']: 37 %是字符跨行连接符 8 disp(str); 39 Display which key has been pressed. 40 41 If the key that has been pressed is the escape key break out of all loops including the indefinite while loop. 43白 if keyCode(escapekey) 44 break; %如果检测到的按键行为是结束键,则break, 45 end 96 跳出while7end'死循环 while KbCheck;end If the user holds down a key for more than a microsecond, 49 KbCheck will report multiple events,since computers are faster 50 than people's fingers 51 To condense multiple 'keyDown'events into a single event, 52 tonce a key has been pressed 53 we wait until all keys have been released 54 sbefore going through the loop again 56 end -end 58 Listenchar(0);
%KbCheck检查是否有按键行为 %如果检测到按键行为,显示按键内容及时间 %如果检测到的按键行为是结束键,则break, 跳出while 1 end‘死循环’ %GetSecs获取当前电脑系统时间 %.是字符跨行连接符