================================== Advanced keyboard programming ================================== Many of you reading this would probably be saying, whats advanced about programming the keyboard? Well the answer is, think about all those keys that you cannot use in your programs; SHIFT, CTRL, ALT, CAPS LOCK. When you are usually reading from the keyboard, you just use INKEY$, we cannot use INKEY$ when we need to use all of the special keys so we have to understand howeverything works. When you press a key on the keyboard, an interrupt is generated (IRQ1), when this interrupt is generated an interrupt handler is called. This interrupt handler is the keyboard handler it reads from I/O port 60h and then decides what to do with the byte after it has read it. What it does with the byte depends entirely on which key is pressed (see the chart below). |--------------------|----------------------------------------------------------| | Key | What happens | |--------------------|----------------------------------------------------------| | CTRL + ALT + DEL | memory location 0040:0072h is set to 1234h and POST | | | resumes control of the system. | | CTRL / ALT / SHIFT | Memory locations 0040:0017h and 0040:0018h are updated | | CTRL + BREAK | Interrupt 1Bh is called | | PRINT SCREEN | Interrupt 05h is called | |--------------------|----------------------------------------------------------| If we wanted to find out which key had been pressed, we just read port 60h eg. KEY = INP(&H60) and this gives us the keyboard scan code for the key that has been pressed, but know I here you say, what is a scan code? A scan code is the code that is generated every time a key is pressed. Keyboard Scan code chart |-------------|-----------------|----------------|----------------| | ESC 01 | U 16 | | or \ 2B | F6 40 | | ! or 1 02 | I 17 | Z 2C | F7 41 | | @ or 2 03 | O 18 | X 2D | F8 42 | | # or 3 04 | P 19 | C 2E | F9 43 | | $ or 4 05 | { or [ 1A | V 2F | F10 44 | | % or 5 06 | } or ] 1B | B 30 | NUMLOCK 45 | | ^ or 6 07 | ENTER 1C | N 31 | SCROLL LOCK 46 | | & or 7 08 | CTRL 1D | M 32 | HOME or 7 47 | | * or 8 09 | A 1E | < or , 33 | UP or 8 48 | | ( or 9 0A | S 1F | > or . 34 | PGUP or 9 49 | | ) or 0 0B | D 20 | ? or / 35 | - 4A | | _ or - 0C | F 21 | RIGHT SHIFT 36 | LEFT or 4 4B | | + or = 0D | G 22 | PRTSC or * 37 | 5 4C | | LEFT 0E | H 23 | ALT 38 | RIGHT or 6 4D | | TAB 0F | J 24 | SPACEBAR 39 | + 4E | | Q 10 | K 25 | CAPSLOCK 3A | END or 1 4F | | W 11 | L 26 | F1 3B | DOWN or 2 50 | | E 12 | : or ; 27 | F2 3C | PGDN or 3 51 | | R 13 | " or ' 28 | F3 3D | INS or 0 52 | | T 14 | or ` 29 | F4 3E | DEL or . 53 | | Y 15 | LEFT SHIFT 2A | F5 3F | | |-------------|-----------------|----------------|----------------| ---------------------------------------------------------------------------------- This tutorial originally appeared in the BASIX Newsletter, Issue 1 from June, 1999. It was written by Peter Johnson (a.k.a. Screech) The following note was printed in the second issue, in July, 1999: =================== 3. Keyboard note =================== Last month I neglected to mention that after you have let go of the key the scan code will be the same scan code plus 128 on top, for example if I press ESCAPE the keyboard will generate scan code 1 and when you let go it will change to 129. This is useful if you need to know this kind of thing for your programs.