Welcome to the Power Users community on Codidact!
Power Users is a Q&A site for questions about the usage of computer software and hardware. We are still a small site and would like to grow, so please consider joining our community. We are looking forward to your questions and answers; they are the building blocks of a repository of knowledge we are building together.
Post History
For context, suppose I ask (on Linux) a process to report to me about the exact bytes I send it - say, with xxd -. I press the right arrow key on my keyboard, then Enter, then Ctrl-D (to signal the...
#1: Initial revision
What's the protocol for keyboards to communicate which keys were pressed?
For context, suppose I ask (on Linux) a process to report to me about the exact bytes I send it - say, with `xxd -`. I press the right arrow key on my keyboard, then Enter, then Ctrl-D (to signal the end of input, implicitly quitting the program). And I get: ``` 00000000: 1b5b 430a .[C. ``` As I might expect from an understanding of [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code), the right-arrow keypress has resulted in the `xxd` program receiving the bytes for an ASCII "escape" character, open square bracket and capital C; the enter key has somehow resulted in a [line feed](https://en.wikipedia.org/wiki/Newline); and the Ctrl-D keypress has seemingly been handled by the terminal and not forwarded to `xxd` (it simply closes the standard input). However, I also know that the return key would produce a carriage return - line feed sequence on Windows (so two bytes) if I had an equivalent program there; and that the Escape key produces an escape character by itself (so that e.g. [TUI](https://en.wikipedia.org/wiki/Text-based_user_interface) programs need a different way to tell whether the `0x1b` byte represents the Escape key or the start of something more complex); and that the OS can implement different "keyboard layouts" for the same physical device (useful for typing text in different languages). Finally, it seems that some games can register whether a modifier key (such as the Shift key) is being held down, even if no letter key is pressed. What does the overall process look like for getting from physical key presses to a program's standard input? Does the keyboard actually send the same bytes as in the ANSI escape sequences; or does it send bytes that encode some kind of "key ID" for each press; or exactly what? Is it possible to intercept the "raw" keyboard data directly? And where in the system, exactly, does it get translated?