# 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:

 01Esc 021 032 043 054 065 076 087 098 0A9 0B0 0C− 0D= 0E← 0F⇥ 10Q 11W 12E 13R 14T 15Y 16U 17I 18O 19P 1A[ 1B] 1C↲ 1DCtrl 1EA 1FS 20D 21F 22G 23H 24J 25K 26L 27; 28' 29` 2A⇧ 2B\ 2CZ 2DX 2EC 2FV 30B 31N 32M 33, 34. 35/ 36⇧ 37* 38Alt 39Space 3ACaps

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.

Tags

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. 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. 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. :-)