Learn about the technologies behind the Internet with The TCP/IP Guide!
NOTE: Using robot software to mass-download the site degrades the server and is prohibited. See here for more.
Find The PC Guide helpful? Please consider a donation to The PC Guide Tip Jar. Visa/MC/Paypal accepted.
View over 750 of my fine art photos any time for free at DesktopScenes.com!

[ The PC Guide | Systems and Components Reference Guide | Keyboards | Keyboard Construction and Operation | Keyboard Operation ]

Keystroke Detection and Scan Code Generation

The keyswitches are the devices that are used to actually detect "keystrokes", that is, the finger motions that mean a key has been pressed. The keyswitches respond to mechanical motion by creating an electrical signal that tells the keyboard's internal controller that "a key was pressed". From there, the internal circuitry must take over and determine which key, and figure out how to communicate this fact to the system.

The first issue that the circuitry must deal with is figuring out which key was actually pressed. This would seem fairly simple: there's one keyswitch for each key, and that keyswitch could be connected to the keyboard controller; it would just send it a signal when that key was pressed. The problem is that on a keyboard with over 100 keys, having each keyswitch wired directly to the keyboard controller would be cumbersome and difficult to implement. A controller chip with over 100 pins (for all these different signals) would also be expensive to manufacture.

Instead, the keyswitch sensors are arranged in a matrix of rows and columns, not unlike a spreadsheet. (Of course, the keys on a keyboard are not arranged in a perfect rectangular matrix like a spreadsheet is, but it works the same way internally.) When a specific key is pressed, it generates a signal for the row and column assigned to that key. The controller is programmed to know that, for example, the letter "T" is at row # 3 and column #8, and so on, for each key. This design means that instead of, say, 104 signal lines being needed for a 104-key keyboard, 9 row lines and about 12 column lines will suffice, for example. (The number of rows and columns doesn't necessarily correspond to the number of rows and columns of keys on the keyboard, since the wiring of different keys can be changed by the design of the circuit board in the keyboard.)

The next problem is that, especially with contact keyswitches, the transition from "this key has not been pressed" to "this key has been pressed" is not a simple one. In fact, the mechanical elements tend to "bounce" as they are pushed together, because they have some elasticity. (Demonstration: take a ruler, hold it at one end with the other a few inches over the surface of a table. Try to bring the other end down to touch the table at about the speed a keypress would go; you'll see and hear the vibration that results.)

With a keyswitch this "bouncing" is barely noticeable to the human senses, but electronics are much more sensitive. Without correction, the sense circuits would see a rapid sequence of "on-off-on-off-..." as each key "bounced" as it was being pressed. To get around this problem, special "debounce" circuitry is designed into the keyboard's wiring to ensure that a single keypress will not result in multiple keystrokes being registered.

After the keystroke has been registered, the controller within the keyboard generates a special code to indicate what has been pressed. This is called a scan code. In fact, each key on the keyboard has two scan codes: the first is called the make code, and is sent when the key is pushed down; the second is called the break code, and is sent when the key is released. The reason for having two codes is that it allows for much more flexibility than just a single code. Using two codes, the PC system can tell not only what keys were pressed, but for how long, and also which keys have been held down for a period of time. This dual-code system also facilitates the multiple-key combinations, such as "<Ctrl>+A", or "<Alt>+<Tab>", that we are so used to using on the PC.

Each key on the keyboard has its own scan code make-break pair; this is true even if there are two keys that are the same. For example, the PC can tell the difference between the left and right <Shift> keys (often used to control the flippers in pinball games! :^) ). It can also differentiate between the main <Enter> key and the one on the numeric keypad.

In fact, there are three different entire sets of make-break pairs for each key on the keyboard. Only one is generally used; the others exist for compatibility with older hardware. If you use a much older keyboard type you must tell your system that you are doing so, or it will misinterpret the scan codes from the keyboard (you also may have signaling issues to deal with). Foreign-language keyboards also have some of their keys different from American keyboards, and thus generate some different scan codes, which the system must be told to look for.

I was going to include a table here that showed the make and break scan codes for each key on a typical U.S. keyboard. However, the table would have been monstrous and hard to really deal with, since there are over 100 keys in a standard keyboard. Instead of putting all the code pairs in one huge page, I have broken it into multiple smaller tables that can be found in the various pages in the section discussing key groupings.

Note: Remember that scan codes are used only for internal operations within the PC. You won't have to deal with these numbers yourself in day-to-day operation of your PC.

Next: Keyboard Interfacing

Home  -  Search  -  Topics  -  Up

The PC Guide (http://www.PCGuide.com)
Site Version: 2.2.0 - Version Date: April 17, 2001
Copyright 1997-2004 Charles M. Kozierok. All Rights Reserved.

Not responsible for any loss resulting from the use of this site.
Please read the Site Guide before using this material.
Custom Search