At the first glance, it does not seem to be an excercise for your computer if you hit a key on your keyboard. Instantely, it displays the appropriate charachter at the current cursor position. However, in the time between you started pressing the key and the time the character is displayed a lot of electrical and computational magic is done. Although you may not notice the tiny delay, it may be significant for some psychological experiments.
First, make yourself clear that the computer can not see what the letter the manufacturer has printed on each key. As yourself when you touch-type the computer can only rely the keys are always at the same position. This is true if you use e.g. an english type keyboard, but you must notify the computer (and adapt yourself) if you suddently use a keyboard with another layout. Internally, the keyboard assigns a "key code" to each key and reports that key code to your computer if a key is hit. When programming psychological experiments you are generally interested in the 'key' the subject is pressing, rather than what character is printed on it. Thus, the PsychToolbox offers commands to access to key codes the computer receives from the keyboard. KBCHECK tests whether any key is currently pressed and KBWAIT would wait if there is no key pressed.
When you type KBWAIT in the command window it would return instantly. On a fast computer you simply can not relase the return key you need to press after typeing KBWAIT. The results of the KBWAIT command is a timestamp of the key press. You can get a precise timestamp anytime with the GETSECS command. To compute the elapsed time between two commands in seconds take the difference of two timestamps.
> t0 = getsecs t0 =
5.8905e+005
> t1 = kbwait
ans =
5.8906e+005
> t1 - t0
ans =
4.4691
> kbname
ans =
'return'
> kbname('return')
ans =
13
> [keyisdown, secs, keycode] = kbcheck;
> keyisdown
ans =
1
> keycode(13)
ans =
1
> while kbcheck; end;
>
>
>
> endkey = kbname('end');
> while kbcheck; end; kbwait; [keyisdown, secs, keycode] = kbcheck;
> keycode(endkey)
ans =
1
This seems to be quite awkward but is the only correct way to obtain 'key' presses. The other option would be to design experiments were secondary how fast the subjects presses the key ( and often these are the better experiments ;) ). Note, that the timing is still quite imprecise and is usually in the range of the time resolution of your operating system (e.g. 11 ms).
If you need a more accurate response time look at the ActiveWire components that are supported by the PsychToolbox.
Page Execution took 6.204 seconds |