Does anybody here have a decent and thorough tutorial on how to gain direct access to the PC keyboard? I have already read the 20th chapter of the AoA book but i seem to be having problems understanding some of the things that have been written there. It'd be really nice if any of you could share your knowledge on this.

Thanks in advance.
Posted on 2006-09-08 11:36:27 by XCHG
You may want to run my IOport 60h monitor OPIN Keys, press and release some keys and watch what is the keyboard actually sending.
Posted on 2006-09-08 12:22:09 by vit$oft
One of the more thorough, yet to-the-point, explanations I have found is at, being part of a set of OS Development literature.

To summarize a few things for you
Both I/O Ports 60h and 61h are collective called the Programmable Peripheral Interface (PPI) ports, dating back to the IBM/PC XT.

Keyboards return what are called "scancodes" through I/O port 60h (PPI Port A) on the IBM PC Architecture. I/O port 61h (PPI Port B) is simply to acknowledge the reception of the scancode (i.e. "OK, I got it... continue accepting keystrokes").

I/O port 64h is an extension for the IBM/PC AT, which is defined as having the Intel 8042 Keyboard Microcontroller. Pretty much anything that you can access more than 1MB of RAM on (requiring the A20 Gate to be enabled through the 8042), you can use I/O port 64h on. For all intents and purposes, you want to use utilize this I/O Port, and fall back on the PPI ports when required.

Here is a quick example in using the PPI Ports to obtain a scancode...

;Read in the scancode from the keyboard buffer and reset the keyboard
in al,0x60 ;Obtain scancode form Keyboart I/O Port
mov cl,al ;Store the scancode in CL for now
in al,0x61 ;Parse the Keyboard Command Port
mov ah,al ;Store command code in AH for now
or al,0x80 ;Set AL to disable command code
out 0x61,al ;Output disable command to Keyboard Command Port
mov al,ah ;Set AL to the original command code
out 0x61,al ;Output enable command to Keyboard Command Port
mov al,cl ;Restore the scancode to AL

Once you have received the scancode in such a manner, you must relate it to an ASCII/UNICODE character. This all depends on if you are using a QWERTY, DVORAK or any other keyboard. The common thing is to "know" which keyboard is being used and develop a look-up table accordingly (i.e. scancode 01h = <ESC> key on QWERTY).

Another thing you have to keep track of is Extension Scancodes, those starting with E0h or E1h. A standard key-press scancode falls below 80h and comprises of only one byte, with its accompanying key-depress scancode above 80h (high-bit toggle indication). Extension Scancodes comprise of two or more bytes. Port 60h still only allows you to receive one byte (scancode) at a time, so this is where the complexity exists in keeping track of Extension Scancodes.

With all this in mind, go ahead and read the link I posted above (there are two more good text files on that site as well) and use vit$oft's "OPIN Keys" program to help visualize this process :)
Posted on 2006-09-09 01:56:27 by SpooK
I wish i had asked this question earlier. These links are just what i needed, thanks to both of you. Spook, if i could make love to a link i would cause that place is virtually a heaven. Appreciations.
Posted on 2006-09-09 08:11:14 by XCHG