What is a Scan Code?

In the keyboard are little switches. When you press a key one of the switches is activated and when you release that key the switch is activated again. The keyboard makes note of these happenings and stores them in a small buffer (memory area) in the keyboard while it notifies the computer that something has happened at the keyboard (an interrupt). The computer, once notified of keyboard activity reads the buffer and takes the necessary action.

Each key on the keyboard has its own code that it sends when pressed and when released; this is called its scan code. When listing scan codes here we’ll list the “press” scan code. The “release” scan code is that number plus 128 (80 hex).

While the original scan code specification allowed for a single number, newer keyboards with the movement keys repeated in the center of the keyboard forced a change and those keys carry a two-number scan code with the first number always being hex E0 (so programs reading scan codes first test for the E0 character; if not found process the code directly, if found, process the next code as one of the center movement keys).

At first blush the release code may seem redundant but when you think about how often you might press and hold the shift, control, or alt keys down while typing something else it becomes clear why it’s needed.

That said, here are the various scan codes originally defined by IBM (you can see from the layout these were defined for the very first keyboard)…

  • hex 01 = Escape key
  • hex 02 = 1 or ! key
  • hex 03 = 2 or @ key
  • hex 04 = 3 or # key
  • hex 05 = 4 or $ key
  • hex 06 = 5 or % key
  • hex 07 = 6 or ^ key
  • hex 08 = 7 or & key
  • hex 09 = 8 or * key
  • hex 0A = 9 or ( key
  • hex 0B = 0 or ) key
  • hex 0C = – or _ key
  • hex 0D = = or + key
  • hex 0E = Backspace key
  • hex 0F = Tab key
  • hex 10 = q or Q key
  • hex 11 = w or W key
  • hex 12 = e or E key
  • hex 13 = r or R key
  • hex 14 = t or T key
  • hex 15 = y or Y key
  • hex 16 = u or U key
  • hex 17 = i or I key
  • hex 18 = o or O key
  • hex 19 = p or P key
  • hex 1A = [ or { key
  • hex 1B = ] or } key
  • hex 1C = Enter key
  • hex 1D = Control key (Left if two)
  • hex 1E = a or A key
  • hex 1F = s or S key
  • hex 20 = d or D key
  • hex 21 = f or F key
  • hex 22 = g or G key
  • hex 23 = h or H key
  • hex 24 = j or J key
  • hex 25 = k or K key
  • hex 26 = l or L key
  • hex 27 = ; or : key
  • hex 28 = ‘ or ” key
  • hex 29 = ` or ~ key
  • hex 2A = Left shift key
  • hex 2B = \ or | key
  • hex 2C = z or Z key
  • hex 2D = x or X key
  • hex 2E = c or C key
  • hex 2F = v or V key
  • hex 30 = b or B key
  • hex 31 = n or N key
  • hex 32 = m or M key
  • hex 33 = , or < key
  • hex 34 = . or > key
  • hex 35 = / or ? key
  • hex 36 = Right shift key
  • hex 37 = * or PrtScr key
  • hex 38 = Alt key (Left one if two)
  • hex 39 = Space bar
  • hex 3A = Caps Lock key
  • hex 3B = F1 key
  • hex 3C = F2 key
  • hex 3D = F3 key
  • hex 3E = F4 key
  • hex 3F = F5 key
  • hex 40 = F6 key
  • hex 41 = F7 key
  • hex 42 = F8 key
  • hex 43 = F9 key
  • hex 44 = F10 key
  • hex 45 = Num Lock key on numeric keypad
  • hex 46 = Scroll Lock key on numeric keypad
  • hex 47 = 7 or Home key on numeric keypad
  • hex 48 = 8 or Cursor Up key on numeric keypad
  • hex 49 = 9 or Pg Up key on numeric keypad
  • hex 4A = – key on numeric keypad
  • hex 4B = 4 or Cursor Left key on numeric keypad
  • hex 4C = 5 key on numeric keypad
  • hex 4D = 6 or Cursor Right key on numeric keypad
  • hex 4E = + key on numeric keypad
  • hex 4F = 1 or End key on numeric keypad
  • hex 50 = 2 or Cursor Down kay on numeric keypad
  • hex 51 = 3 or Pg Dn key on numeric keypad
  • hex 52 = 0 or Insert key on numeric keypad
  • hex 53 = . or Delete key on numeric keypad
  • hex 54 = Sys Req key (on 84-key keyboard)
  • hex 57 = F11
  • hex 58 = F12
  • hex E1 = Pause key (on 101-key keyboard)

The following scan codes are preceeded by hex E0…

  • hex 1C = Enter key on numeric keypad
  • hex 1D = Control (Right if two)
  • hex 35 = / key on numeric keypad
  • hex 38 = Alt (Right if two)
  • hex 47 = Home
  • hex 48 = Up arrow
  • hex 49 = Pg Up
  • hex 4B = Left arrow
  • hex 4D = Right arrow
  • hex 4F = End
  • hex 50 = Down arrow
  • hex 51 = Pg Dn
  • hex 52 = Insert
  • hex 53 = Delete

While these are generally assigned scan codes, be aware that keyboards come in a wide variety of shapes and sizes and the scan codes from those may differ somewhat from the above.

[Originally published 11/22/2009.] Two of the comments and response are below…

1) What do I do if wanna read an ‘Enter’ key from keyboard in a ‘C’ program.
I want to write a code for the program in which user select some options through arrow keys and then S/he hit the ‘Enter’, then how can I scan the ‘Enter’ key.

2) what is the program to read the arrow keys ? when searched in net i saw a program which using ‘i.h.ah’ &’o.h.ah’ what does they mean and how it works?

Answer: How you read a key depends on the language being used. In C, for example, the function getkey() is typically used. After you get the key then your program has to first determine if the key was a regular keystroke or one of the extended codes that have two parts.

A sample C program to do this is in the answer here…

http://answers.yahoo.com/question/index?qid=20080202062127AAdkCrz

The i.h.ah referenced is just a variable name that seems to appear in many Google search results including the one at Yahoo! Answers. But that one at least has a useful answer.