Understanding keyboard data signaling - Page 1
Forum section: electronics / general

author: SolderOcelot
author_rank: Contributor
timestamp: November 07, 2019, 03:40:15 pm
content: I have been studying how (most) keyboard work and it comes down to each key being a switch that closes a circuit with a vertical and a horizontal leg. A small onboard chip reads the relevant vertical and the horizontal leg and with this it can locate exactly which key has been pressed.But what signal gets sent over the cable to the laptop motherboard / computer usb port? A character code or a location code?Is this signal the same for every keyboard?

author: amyk
author_rank: Super Contributor
timestamp: November 08, 2019, 12:45:25 pm
content: Familiarise yourself with the specifications:https://en.wikipedia.org/wiki/USB_human_interface_device_classhttps://en.wikipedia.org/wiki/PS/2_port

author: golden_labels
author_rank: Super Contributor
timestamp: November 08, 2019, 01:21:58 pm
content: In addition to whatamykhas said: the scan codes are predefined by the protocol. They aren’t linked to the physical implementation of the keyboard(1)and the actual way the keyboard works may differ greatly between devices. Different manufacturers are having different sets of keys guaranteed to not experienceghostingand more advanced keyboards may offern-key rolloverwhich requires more complex circuitry.____(1)Which doesn’t prohibit the designers to design their keyboard controller in a way that creates a between link physical layout and scancodes. But the direction is from scancodes to layout, not the other way around.

author: tooki
author_rank: Super Contributor
timestamp: November 16, 2019, 06:12:18 pm
content: Quote from: Faloude on November 07, 2019, 03:40:15 pmWhat you're calling the "location code" is basically what's actually called a scan code. In most implementations, a scan code uniquely identifies a physical key on a keyboard, even on keys which there are more than one of. (For example, separate scan codes for the left and right Shift keys, even though the OS normally treats them as the same thing.) The OS converts the scan codes to character codes by using the keyboard layout as a sort of translation key. This is why you can select a keyboard layout in your OS settings, allowing the same physical keyboard to be used by any language, provided you aren't reliant on the key caps. (The caps are purely cosmetic.)There are actually only 3 major physical keyboard layouts: ANSI (used primarily by US English), JIS (Japanese), and ISO (pretty much every other language). These differ in the actual number of keys and the shape and location of a few (primarily in the shape of the Return key, size of the shift keys and space bar, and number of modifier keys).But there are many different scan code systems, depending on what kind of connection is used. Also, on scan code based keyboards, the keyboard doesn't sendkeystrokes, it sendskey downandkey upcommands. So a single keystroke is actually a pair of key down and up commands. This allows the OS to manage how to handle repeating letters, modifiers, delays, etc.Additionally, some very old computers did in fact use keyboards that output character codes, not scan codes. Typically, this was ASCII. But this hasn't been used for ages.

author: Yansi
author_rank: Super Contributor
timestamp: November 16, 2019, 06:56:16 pm
content: It is not that easy with the key repeat though.PS/2 keybords did the key repeat by themselves, repeating the key-down event. The repeat rate was of course programmable through the PS/2 protocol. This was done and simple.With USB HID class keyboards, it is preferred to leave key repeat to the host implementation. However, many devices do the key repeat anyway, which leads to funny working devices.QuoteThere is a fascinating book on TV Typewriters, some members sent me a while ago. It was named "TV Typewriter Cookbook" I think. Can be found on google. Keyboard and ASCII relations are mentioned there extensively.

author: tooki
author_rank: Super Contributor
timestamp: November 16, 2019, 07:12:23 pm
content: Thanks for the info, yansi! As a Mac user, I've never actually used PS/2 keyboards, and as it happens, Mac keyboards (both USB and ADB, the old standard) don't do the key repeat in the keyboard, so that's what I'm familiar with.I don't think I've ever encountered a USB keyboard that repeats the key_down events on its own. It'd seem to me that that could cause all kinds of havoc (i.e. unexpected behaviors). It seems to me a bad idea in general to leave key repeat up to the keyboard, since it hides the actual state of the keyboard from the OS, which might want to do more nuanced behaviors in many situations. (An analogous situation is how most versions of Windows handled mouse scroll wheel events, as opposed to the Mac: Windows would see the scrolling wheel as a string of "scroll up/down one line" events which would be dumped into a queue and worked through until the queue was empty, meaning that if the software couldn't keep up, the user could easily issue so many scroll events that the window would then keep scrolling up and down long after the user stopped moving the wheel. On the Mac, scroll wheels have always been handled as a sort of... velocity and direction indicator. So if you scroll fast and then stop the wheel, the window also comes to a halt. IthinkI read somewhere that Windows 8 or 10 now works like that, too, but I'm not sure, I've never used either. Anyway, converting scroll motion to discrete, queued events hides the state of the scroll wheel from the application, just as letting the keyboard do the repeat hides the keyboard state from the software.)For sure, the keyboards I have (various third-party mechanical KBs these days, aside from the built-in keyboard in the Macbook) don't do this, going by a USB HID event viewer utility.

author: tooki
author_rank: Super Contributor
timestamp: November 16, 2019, 07:13:12 pm
content: Quote from: Yansi on November 16, 2019, 06:56:16 pmThis one, right?https://archive.org/details/tvtcb_docI'll take a look, thanks!!

author: Yansi
author_rank: Super Contributor
timestamp: November 16, 2019, 07:22:25 pm
content: Yep, it is that one I guess.