Why are the keyboard scan codes for digits off by one?


In Off by one what, exactly?, my colleague Michael Kaplan wrote

And this decision long ago that caused the scan codes to not line up for these digits when they could have...

The word that struck me there was "decision".

Because it wasn't a "decision" to make the scan codes almost-but-not-quite line up with digits. It was just a coincidence.

If you look at the scan code table from Michael's article

you can see stretches of consecutive scan codes, broken up by weird places where the consecutive pattern is violated. The weirdness makes more sense when you look at the original IBM PC XT keyboard:

01
Esc
02
1
03
2
04
3
05
4
06
5
07
6
08
7
09
8
0A
9
0B
0
0C
0D
=
0E
0F
10
Q
11
W
12
E
13
R
14
T
15
Y
16
U
17
I
18
O
19
P
1A
[
1B
]
1C
1D
Ctrl
1E
A
1F
S
20
D
21
F
22
G
23
H
24
J
25
K
26
L
27
;
28
'
29
`
2A
2B
\
2C
Z
2D
X
2E
C
2F
V
30
B
31
N
32
M
33
,
34
.
35
/
36
37
*
38
Alt
39
Space
3A
Caps

With this presentation, it becomes clearer how scan codes were assigned: They simply started at 01 and continued through the keyboard in English reading order. (Scan code 00 is an error code indicating keyboard buffer overflow.) The reason for the keyboard scan code being off-by-one from the digits is merely due to the fact that there was one key to the left of the digits. If there were two keys to the left of the digits, they would have been off by two.

Of course, if the original keyboard designers had started counting from the lower left corner, like all right-thinking mathematically-inclined people, then this sort-of-coincidence would never have happened. The scan codes for the digits would have been 2E through 37, and nobody would have thought anything of it.

It's a testament to the human brain's desire to find patterns and determine a reason for them that what is really just a coincidence gets interpreted as some sort of conspiracy.

Comments (17)
  1. John Elliott says:

    In IBM's later keyboards (starting with the 3270PC, and including the AT and PS/2 models) they did start in the bottom left-hand corner, and went up first, then across (more or less; there are zigzags). So the scancodes that come down the wire from the number keys are 16 1E 26 25 2E 36 3D 3E 46 45. It's the PC motherboard chipset that translates them back to 02 03 04 05 and so on.

  2. Maurits says:

    Now that I look at it…

    Why is 0 next to 9? Surely it should be next to 1. On the numpad 0 is next to 1. (On the other hand, on my phone, it's next to 9 again.)

  3. Niels says:

    Maurits:

    You'll probably have to ask someone at IBM that question. They had been making typewriters for years before they went into computers, and I believe their typewriters had numerical rows before the time of the PC.

  4. James Schend says:

    @Maurits

    Probably has something to do with people being significantly more likely to type a 1 than a 0. Not only do we omit leading zeros in numbers, but also see Benford's Law which states that in real-world data, the majority of numbers will start with the digit 1.

  5. joewoodbury says:

    Not to age myself too much, but many manual typewriters had no number 1 and/or 0. The small 'L' was used for '1' and the large 'O' for zero.

  6. Barbie says:

    @James Schend: allow me to nitpick: 1's are 30% of numbers. Not quite majority, but way higher than the intuitive 10%.

    Yeah, uniform distributions don't exist on R!

    en.wikipedia.org/…/Benford%27s_law

  7. h.attila says:

    Maurits:

    It is on certain non-English keyboard layouts.

    By the way, does anyone know what was the point of switching Y and Z on some layouts (but keeping every other letter in place)?

  8. waleri says:

    @h.atilla

    AFAIK, switched Y and Z is a german layout

  9. Dusty says:

    @Barbie:

    Not mentioned in the Wikipedia article, but the occurrence of 1's also goes way up in binary, which computers use internally.  At the same time, the rate of the other digits significantly decreases, except for 0.  Which is probably why their on the ends of the set of number keys.

  10. Cherry says:

    @h.atilla: waleri is right. The "Y" is seldom used in the German language, where "Z" is for more often used than in the English language.

    English: Y – 1,9%, Z – 0,07%

    German: Y – 0,1%, Z – 1%

  11. Maurits says:

    Looks like the 1234567890 (as opposed to 0123456789) convention goes back to the days of printing telegraphs:

    en.wikipedia.org/…/Printing_telegraph (note the picture)

    And even before, looking at Morse code charts:

    en.wikipedia.org/…/Morse_code (note the order of the digits on the chart)

    "When you read, you begin with 'A, B, C'; / When you sing, you begin with 'Do, Re, Mi'"… I suppose we're supposed to conclude that "When you count, you begin with '1, 2, 3'" since "0, 1, 2" wouldn't rhyme.

  12. Bob says:

    @Maurits:  one of us smug PLATO ex-users must point out that not ALL computers used 1234567890

             en.wikipedia.org/…/PLATO_%28computer_system%29   (note the picture)

  13. Ricardo C says:

    In the natural order of the numbers the 0 follows after 9. The first number is always 1

    Historically zero is a late invention not well understood by many civilizations.

    The main purpose of 0 is to increase the count to the next series. A placeholder. Zero having no value is just consequential, the aftermath ;)

  14. Dean Harding says:

    @Maurits: if you look on that Morse Code chart, the 0 could conceivable go either before the 1 or after the 9 (the pattern would fit either way). It's just that particular diagram that put it after the 9.

  15. Malcolm says:

    On old typewriters there was no "0" key, you were expected to use the O character instead.  Similarly, 1 was often omitted as a lower case L was an adequate substitute.  

    This may be entirely irrelevant of course.

  16. 640k says:

    Scan code 00 is an error code indicating keyboard buffer overflow

    aka fail

  17. prunoki says:

    Cherry: Attila asks this because he is Hungarian. :-)

Comments are closed.