Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

71%
+3 −0
Q&A 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...

1 answer  ·  posted 22h ago by Karl Knechtel‭  ·  last activity 20h ago by Olin Lathrop‭

Question keyboard-input hardware protocols
#1: Initial revision by user avatar Karl Knechtel‭ · 2025-05-19T19:03:19Z (about 22 hours ago)
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?