第七章按键反应的记录、保存 和数据分析 Key Presses and Data analysis
Key Presses and Data analysis
单个实验试次Tria的流程 01 呈现刺激 Psychtoolbox(PTB)中 的Screen函数 ??? 02 记录(按键)反应
Psychtoolbox(PTB)中 的Screen函数 呈 现 刺 激 ??? 记 录 ( 按 键 ) 反 应
7.1与PTB兼容的按键相关命令 ·新建一个m文件KbPractice ·功能:持续记录键盘上的按键,并在命令窗口显示对应的 按键数字码以及按键时间;按SPACE键结束记录。 1 号bPractice,m 2 Displays the number of seconds that have elapsed when the user presses a key. sa simplified version of KbDemo modified by YW,2022 >KbPractice %在命令窗口运行KbPractice Testing KbCheck and KbName:press a key to see its number Press the 'SPACE'key to exit. You pressed key 87 which is w typed at time 2.4043seconds You pressed key 69 which is e typed at time 3.5383seconds You pressed key 65 which is a typed at time 5.286seconds You pressed key 82 which is r typed at time 5.7581seconds You pressed key 70 which is f typed at time 6.828seconds You pressed key 49 which is 1!typed at time 12.7795seconds You pressed key 50 which is 23 typed at time 13.3814seconds You pressed key 51 which is 3 typed at time 13.8795seconds You pressed key 97 which is 1 typed at time 15.4673seconds You pressed key 98 which is 2 typed at time 15.7394seconds You pressed key 99 which is 3 typed at time 16.1774seconds You pressed key 32 which is space typed at time 18.0832seconds
l 新建一个m文件KbPractice • 功能:持续记录键盘上的按键,并在命令窗口显示对应的 按键数字码以及按键时间;按SPACE键结束记录。 %在命令窗口运行KbPractice
6 bName ('UnifyKevNames); WaitSecs(1.0); 8 disp('Testing KbCheck and KbName:preas a key to see ita number') 9 disp('Preas the 'SPACE''key to exit.'); 10 escapeKey KbName ('SPACE'); ●KbName %在命令窗口测试KbName ·实现键盘按键数字码和所表示的按键名称 之间的转换。 >KbName('a') .KbName('UnifyKeyNames') ans ·可以使程序使用在Windows.系统和Mac系 65 统下通用的按键 >KbName(65) .WaitSecs ans ·等待给定的时间,单位为秒。 a ●disp ·在命令窗口显示字符串
l KbName • 实现键盘按键数字码和所表示的按键名称 之间的转换。 l KbName(‘UnifyKeyNames’) • 可以使程序使用在Windows系统和Mac系 统下通用的按键 l WaitSecs • 等待给定的时间,单位为秒。 l disp • 在命令窗口显示字符串 %在命令窗口测试KbName
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 15s program doesn't continue until all keys have been released. ●ListenChar(2):监听除了Octave/MATLAB命令窗口以 外的按键行为 。m文件运行过程中记录到的按键不会显示在命令窗口 。ListenChar(O):恢复MATLAB命令窗口的监听状态,从 而可以执行命令窗口输入的命令
l ListenChar(2):监听除了Octave/MATLAB命令窗口以 外的按键行为 • m文件运行过程中记录到的按键不会显示在命令窗口 l ListenChar(0):恢复MATLAB命令窗口的监听状态,从 而可以执行命令窗口输入的命令
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获取当前电脑系统时间 %.是字符跨行连接符
7.2与PTB兼容的按键相关函数1 。新建一个函数m文件waitTilltime.m ·在给定时间内记录并存储按键内容及时间。 %这是函数wait Tilltime,两个输出变量,两个输入变量(第2个输入变量不是必须) 月Function[keys,RT】=wa1tT111time(waitTime,startTime】 3 [keys,RT]waitTill(waitTime,[startTime]) Returns a vector of keys pressed and the timing of the presses during an sinterval of 'waitTime'seconds.By default,the clock starts within the > sfunction,but if startTime is provided then the function will return swaitTime seconds after startrime was defined with 'GetSecs'. 10 An empty variable is returned if no key was pressed during the interval. 11 12 3/24/09 Written by G.M.Boynton at the University of Washington >[keys,RT]waitTilltime(8) >>[keys,RT]waitTilltime(5) keys keys ::%在命令窗口运行函数waitTilltime 1.31=3 RT= 2.2399 3.56174.2635 RT= 1.41351.59361,76351,96952.2353.2.4215
l 新建一个函数m文件waitTilltime.m • 在给定时间内记录并存储按键内容及时间。 %这是函数waitTilltime,两个输出变量,两个输入变量(第2个输入变量不是必须) %在命令窗口运行函数waitTilltime
14 sInitialize the output variables 15 keys =() 16 RT=[]: 17 18 Read the clock if no clock time was provided 19if ~exist ('startTime') 20 startTime GetSecs; 21 end 22 23 sGive a warning if the waiting interval is zero or less 24if GetSecs-startTime waitTime 25 disp('Warning!waitTill:waiting interval is less than zero') 26 end 27 8 sTurn off the output to the command window 29 ListenChar(2); Q:如何设置必须要按键才行? 3 nKeys 0; sloop until waitTime seconds has passed since startTime was defined 33 while GetSecs-startTime waitTime %如果还处在等待按键反应的时间内 34 ssee if a key is pressed 35 【keyIsDown,timeSecs,keyCode】=bCheck;.%检查按键行为 36白 if keyIsDown sa key is down:record the key and time pressed 37 nKeys-nKeys+i;%计算总的按键次数 38 RT(ney3)=timeSeca3-3 cartTime:%计算当次按键反应时间(当前时间-起始时间) 39 keys(nKeys)=KbName(keyCode) 40 sclear the keyboard buffer %查看按键内容 41 while KbCheck:end 42 end 43 end sTurn on the output to the command window 45 ListenChar(0); 46
%如果还处在等待按键反应的时间内 %检查按键行为 %计算总的按键次数 %计算当次按键反应时间(当前时间-起始时间) %查看按键内容 Q:如何设置必须要按键才行?