Scanning The IBM Keyboard
I have an IBM PC and have encountered a dilemma in a BASICA program. My program sends one character at a time from the keyboard to a letter-quality printer (that is, it makes the PC and printer act like a typewriter). The program works fine, but I would like to display on the screen a constant monitoring of the Caps Lock key. So far, I have been unable to determine how to scan the keyboard for that key. Can you help?
Thomas Bigos
Location &H17 at segment &H40 on the IBM PC and PCjr contains the status of the Caps Lock key and other important keys. Each of the eight bits in this location corresponds to one of eight special keys as follows:
Bit# | Value | |
Right Shift | 0 | 1 |
Left Shift | 1 | 2 |
Ctrl | 2 | 4 |
Alt | 3 | 8 |
Scroll Lock | 4 | 16 |
Num Lock | 5 | 32 |
Caps Lock | 6 | 64 |
Ins Lock | 7 | 128 |
If a bit is on, the key is active. The following program prints the contents of this location:
10 DEF SEG = & H40 20 PRINT PEEK(&H17) 30 GOTO 20
Run the program and press one of the special keys. The corresponding value is printed. Notice that Ctrl, Alt, and the Shift keys are active only while they are pressed. But the Lock keys act as a toggle; pressing them once activates them, and pressing them again turns them off. If you press more than one key, their values are added. For example, holding down Ctrl and Alt displays 4 + 8 = 12.
To check for the Caps Lock key, you need to read bit 6. To test a particular bit, you must AND with the bit's value. Bit 6 can be checked by ANDing with 64. Change line 20 as follows:
20 PRINT PEEK(&H17) AND 64
Now the program will check only for Caps Lock. A 64 is displayed when Caps Lock is pressed, and a 0 is displayed when it's not, regardless of the status of the other keys.