Marcin Wichary
2 February 2015 / 1,800 words
Originally published in Medium Engineering
One keyboard bug three decades in the making
A few weeks ago, someone reported this to us at Medium:
“I just started an article in Polish. I can type in every letter, except Ś. When I press the key for Ś, the letter just doesn’t appear. It only happens on Medium.”
This was odd. We don’t really special-case any language in any way, and even if we did… out of 32 Polish characters, why would this random one be the only one causing problems?
Turns out, it wasn’t so random. This is a story of how four incidental ingredients spanning decades (if not centuries) came together to cause the most curious of bugs, and how we fixed it.
Ingredient 1/4:
Polish language
Polish is the second most-used Slavic language, right after Russian and just before Ukrainian. In contrast with those two, however, and similar to Western European languages such as German or French, Polish uses the English/Latin alphabet with a few customizations.
This is the base English alphabet, largely the same as a classic Latin/Roman alphabet:
Original Polish words never contain Q, V or X, although we keep them for Latin and other borrowed words:
In exchange for those three, however, Polish adds nine additional diacritics using Latin characters as their base, all in relatively common use:
Starting in the early 20th century, typewriters needed to accommodate the extra 9 letters. If you compare an American typewriter with a Polish one:
…and look at the right side of the keyboard, you can see two of the diacritics – Ł and Ż – promoted to separate keys, and the rest sharing keys with digits. (Typists were encouraged to assemble uppercase versions of seven remaining letters by typing a Latin character, backspacing, and then overwriting an accent to “simulate” the proper letter. This was not uncommon during typewriter times.)
To find room for the extra letters, typewriters needed to dispense with some punctuation, most notably semicolons (comma + backspace + colon), and parentheses (replaced in common use by slashes).
Ingredient 2/4:
Communism
For someone interested in the early personal computing in the 1980s, Communism in Poland meant two things:
- not a lot of disposable income,
- forbidden commercial importing of computers from the West (individual importing was still possible, assuming you had enough foreign currency and some means of acquiring it).
I grew up in Poland. My first computer – the glorious Atari 800XL – was an original 1979 technology, repackaged in 1983. I got it, secondhand, in 1986.
This wasn’t unique. Technology was delayed on that side of the Iron Curtain; most computers were imported from the West. Prohibited commercial importing meant that for the longest time there was no commercial entity that could prepare computers for use in Poland. Foreign computers arrived with original instructions, untranslated software, and American keyboards like this one:
While France, Germany, and other countries got their early PCs with customized keyboards whose layouts mirrored closely the typewriters that came before…
…in Poland, we had to find another way of inputting the extra 9 diacritics unique to our language.
Our extra characters might look very much like Latin equivalents, and amount to only about 8% of letter distribution (you will hate them when playing Scrabble), but they are important. You can’t just swap them around. Consider these two similar phrases:
Perfectly interchangeable, right? Well, not quite:
There are more examples like that. As it happens, in those early PC days, I was even happy that my full name, Marcin Kazimierz Wichary, did not come with any diacritics that would complicate my life.
Surely, there’s something that can be done, though? Back to the keyboard:
We cannot modify it in any way since that’d require messing with hardware, but we can still try to find a clever solution. There are two modifier keys – Ctrl (where today’s Caps Lock is), and Alt. Ctrl was already used as a common shortcut key, even before CtrlC and CtrlV became typical vessels for copy and paste. But Alt was relatively uncommon. And thus, a de facto standard was born, assigning 8 of our diacritics to their Latin counterparts, and one to something nearby:
People started calling the older layout “typist’s,” and the new invention “programmer’s,” either because early PC users were mostly programmers, or because it preserved all the punctuation symbols that were often used in programming.
The new layout was an ergonomic nightmare – look at how many of those letters are very close to the solitary Alt on the left, and need to be pressed using the same hand – but it was easy to understand and did not require any expensive hardware modifications or even cheap ones (for example stickers). It stuck. A few other nearby countries – Romania, then-Czechoslovakia – came up with similar schemes.
The setup was so successful that even when, a decade later, proper typist’s keyboards started appearing, practically no one would switch to them, mirroring the ascendancy of less-than-ideal QWERTY some 80 years before.
Ingredient 3/4:
Old habits dying hard
Autosaving, common today, needed to wait for the right moment. Especially in the 1980s, and even 1990s, saving your document was lengthy (powering up that floppy drive and writing to the disk took some time), would slowly wear out whatever medium you were using, and sometimes occupy CPU so intensely it couldn’t be used for anything else.
Saving by hand was then what backing up is today: a habit you needed to learn for your own good. The unlucky ones figured it out the hard way, writing for hours on a computer that had a tendency to crash cruelly and often, only to realize they forgot to save their work.
I was one of them. We all learned to press CommandS or CtrlS whenever we paused for breath. Sometimes it was after every paragraph. Often, every sentence. Later on, even with every other word. CtrlS became a keystroke buried in people’s motor memories, a habit that was as intrinsic a part of writing as fearing the blank page.
Then that habit turned on them. If you write in any web-based editor, the default thing that happens after pressing the save key combination, is a browser window – giving you a completely useless option to save the current website’s HTML code, often announcing itself via a slow, annoying animation.
The same was true for Medium, a long time ago:
However, at some point we added a little bit of code to our editor to make the save dialog go away:
if ((e.metaKey || e.ctrlKey) && e.keyCode === goog.events.KeyCodes.S) { this._editors.save() e.preventDefault() }
It translates to: if S happens to be pressed with Command (metaKey, used by the Macintosh) or Ctrl (used by Windows or Linux PCs), first: prompt our editor to save what it’s doing (even though we’d autosave it soon anyway), and second: prevent anything else that would normally happen… in this case the annoying browser save dialog.
This felt the right thing to do. (You can still get to the save dialog from the menu, if you want.)
Now, this should ring all sorts of alarms in your head if you remember the bug report mentioned at the beginning:
“I just started an article in Polish. I can type in every letter, except Ś. When I press the key for Ś, the letter just doesn’t appear. It only happens on Medium.”
Something doesn’t compute, though. Medium is blocking CommandS and CtrlS, but you get to Ś by keying in… AltS.
For these two worlds to collide, we need just one more ingredient.
Ingredient 4/4:
Microsoft Windows
Both Windows 3.x and 95 had terrific keyboard support. The menu items and dialogs had controls that could be accessed easily by mouse… but also much quicker by pressing Alt and any underlined letter:
Most of Microsoft Windows UI could be turned into a sequence of keyboard presses, which was incredibly powerful – and something Mac could still learn from. In Poland, however, Alt was already commonly used to enter the extra diacritics. That presented a conflict. Fortunately, by then, keyboards evolved to have extra modifier keys, including a secondary Alt key on the other side of the spacebar.
So, what if we kept the Left Alt key for Windows shortcuts, but used the Right Alt key for Polish diacritics? That would solve all the problems (although the short-lived victory of better ergonomics through two modifier keys would be nullified again… this time one-handed Right AltO, Right AltL, and Right AltN putting your fingers in an uncomfortable position, literally).
But Right Alt functionality needed to be available for older keyboards with just one Alt key too, so Microsoft made it so that internally Right Alt in Windows was mapped to a rarely-used combination of Ctrl and Alt pressed together.
Which meant that, from the system perspective, all of the above shortcuts looked like this:
And there’s something oddly uncomfortable with this picture. That CtrlAltS is starting to look very familiar now, doesn’t it? In fact, incredibly similar to CtrlS that we once started blocking to improve saving.
The fix
Putting all of this together, the reason for the bug becomes obvious:
- Through historical circumstance, people in Poland use Right AltS to type in Ś.
- Because of an obscure technical decision, Right AltS is mapped internally to CtrlAltS.
- Through habit, people might occasionally want to press CtrlS to save a document.
- Medium was overriding whatever default action happened when you pressed CtrlS, thinking it’s just a browser save dialog it was canceling… and never realizing it could be something else: a letter Ś.
- And so, typing Ś became impossible.
Once I figured it out, the patch was trivial. Instead of blindly and greedily blocking CtrlS, we could block CtrlS only if Alt key was not pressed:
if ((e.metaKey || (e.ctrlKey && !e.altKey)) && e.keyCode === goog.events.KeyCodes.S) { this._editors.save() e.preventDefault() }
We fixed this last week, and it wasn’t a lot of work. After seeing the Medium bug reports filed in, and having remembered adding the feature to block CtrlS, I figured it out without much trouble.
But I happen to be Polish, and I went through all of the above – learning to touch type on my mom’s typewriter, owning early PCs with American keyboards, rewiring my motor memory for the Left Alt first, and the Right Alt second… and in the same motor memory, putting CtrlS early on, and then replacing it with CommandS as I switched to Macs.
Much has changed in Poland since the 1980s. In 1989, Communism fell. Computers are now imported legally by individuals and companies alike. Today’s Windows and Mac machines in Poland are bought new, fully support Polish, and the operating systems themselves are properly localized. If you open up system preferences, though, you will still see the programmer’s layout there, and it’s still the most commonly used of the two.
The few lines of code above might be, to me, one of the most curious in Medium’s codebase, the result of an arbitrary set of circumstances… today copiously commented so that people coming after me understand the one opaque conditional statement.
Those lines are also part of a much larger story of hegemony of American computing, where so much still evolves around conventions surrounding the relatively simple English language with its 26 unaccented letters. You can’t help but think Polish language continues fighting for scraps, allowed only to move from one xeroxed derivative of QWERTY to another – and many other languages are even less lucky, their writing systems so much further away from English.
This was a tiny change, but I’m grateful for people working diligently and spending so many more hours every day so that people speaking these languages can use software well, without encountering English-tinted assumptions, decisions, and exceptions.