Accessing the Keyboard with the PsychToolbox

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

PsychToolbox Key Codes

But KBWAIT only tells you when the key was pressed. To actually know which key was pressed you have to call KBCHECK. It returns three parameters: a flag whether a key is currently pressed (KEYISDOWN), the timestamp (SECS) and a variable called KEYCODE. The later one is an bit array that encodes for each key on the keyboard wether it was pressed on the time when KBCHECK was called. Hence, it is very easy to see whether one, two or some more keys are pressed at the same time (e.g. shift + a + return), but it is not obvious to guess the letters that are printed on these keys. If you just want to know whether a particular key was pressed then you can obtain the key code with the KBNAME function. KBNAME('return') for instance gives 13, the key code for the return key. If you want to know the names of the keys one can call KBNAME without parameters

> kbname

ans =

'return'

> kbname('return')

ans =

13

The information whether the return key was pressed is therefore encoded in the 13th field of the KEYCODE variable. Try

> [keyisdown, secs, keycode] = kbcheck;

> keyisdown

ans =

1

> keycode(13)

ans =

1

Waiting for a Key, The Fancy Version

When writing program you should always use the KBNAME function to get the key code since the codes might be different on different systems (e.g. Mac or Linux). Before waiting for a key make sure that all keys are already released, then call KBWAIT and check the KEYCODE with KBCHECK. Try holding the holding the RETURN key after you typed the following line

> while kbcheck; end;

>

>

>

Now, get the key code with KBNAME, wait for the key and get its key code

> 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.


Last edited on Friday, June 6, 2003 7:12:56 am.

Valid XHTML 1.0! Valid CSS!
Page Execution took 6.204 seconds