Who invented the keyboard? Why?
We have two definitions for the word "key".
- A device whose absence prevents something from working (e.g. a lock), and while we think of the old-fashioned skeleton key prior to the Yale key, there is evidence that the Egyptians had multi-pin keys that operated on the same principle as the Yale.
- A lever or button depressed in order to activate a function of a machine.
By the nature of your question, we'll stick with definition (2).
The earliest "key" in this regard would have appeared on musical instruments, particularly brass and woodwind to operate the valves which changed the notes being played. It would seem that the first combination of keys lain out to play each individual note arose on the Harpsichord in 1397, with a "board of keys" that each plucked a harp-like string within. By re-arranging the mechanism into a hammer strike of the strings, the Piano-Forte (soft-loud) was invented where the amount of pressure on the key affected the volume of the note generated.
The "typewriter" first appeared in 1714, and the keyboard layouts were unique to each manufacturer.
When it comes to keys in electronic communication, the first would have been the Morse Key. This was a very finely tuned switch for the transmission of Morse Code, around 1830. The development of Morse Code and Telegraphy are very closely tied.
The basic Morse system was capable of sending one signal across a single pair of wires. Edison improved on this by developing a system that could transmit four signals (two in each direction) across a single pair of wires. Bell was working on a harmonic system to enable transmission of more signals across a single pair of wires but then got wind of the Italian-invented telephone and nipped into the Patent Office first. Who invented the telephone? Honoring the real inventor of the phone
As telephone communication spread across the U.S. continent, and similar throughout Europe, telegraphy was still required for long distance communication.
Around 1870 Baudot introduced a 5-key apparatus for the transmission of up to 32 characters. Although beaten by the typewriter by more than a century, the Baudot keyboard became the standard for international telegraphy. Baudot designed his code so that operators could learn smooth sequences of letters to enable higher quality transmission.
In 1873 the Sholes layout was sold to Remington and became the entrenched standard that we see today. Typewriters of that day were up-strike machines. The typist could not see the product that was being typed without scrolling the page up to view, correct, and then (hopefully) scroll back down to the right position to keep typing. While English speaking people are familiar with the Sholes layout as QWERTY, it was modified for other languages, and AZERTY is a common variant.
It was around 1900 that projects linking the Sholes keyboard to telegraphy (the teletype machine) appeared in conjunction with storing messages on punched tape. The Baudot code was completely re-arranged to be more machine compatible, with mirror codes (like 01010, 00100, 10001, etc.) to become control codes. In this way, if a tape was loaded backward or flipped over, it would not damage the machine, simply printing gibberish.
It was via this modified Baudot code, which I'll just call TTY, as it is known today, that TTY-terminals became the electro-mechanical "human interface" to computers.
During WW2, Turing's Bomb would have been interpreting German TTY code until replaced by its lesser known but fully electronic offspring, Colossus*. This machine was capable of reading punched tape at 5000 characters per second, and was very adept at utilizing the flaw in the Enigma Cypher in order to crack it. Sadly, Colossus (and its variants) was chopped up, destroyed, and buried on the orders of Winston Churchill because he didn't want the technology to fall into the wrong hands lest such an atrocity as WW2 could happen again.
*Not to be confused with Colossus: The Forbin Project.
Note that while the timing of the US-built ENIAC was almost co-incidental, there was no technology-sharing with Colossus.
Also be aware that TTY terminals are still in use with their original 5-bit code. While IBM played with variations of EBCDIC which were mutually incompatible with each other, it was also a major player in the development of ASCII which has long been the "standard" before Unicode was developed.
Ultimately, Sholes did not invent the keyboard. He simply developed the layout which has been maintained ever since by user-inertia.