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

66%
+2 −0
Q&A What's the protocol for keyboards to communicate which keys were pressed?

Keyboards usually natively send "key codes". These have to do with the physical location of the key on the keyboard and how the keys may be multiplexed. These key codes are received by a driver t...

posted 22h ago by Olin Lathrop‭

Answer
#1: Initial revision by user avatar Olin Lathrop‭ · 2025-05-19T20:42:19Z (about 22 hours ago)
Keyboards usually natively send "key codes".  These have to do with the physical location of the key on the keyboard and how the keys may be multiplexed.  These key codes are received by a driver that knows about the specific keyboard.  What happens after that is specific to the operating system.

Some operating system define their own key codes for device-independent internal use.  The keyboard driver then has to translate the codes from the keyboard to the official internal generic key codes.

Depending on the interface the driver must support on the OS side, the driver or a low level OS layer then creates "events" for any changes of the keyboard state.

What happens after that can vary greatly.  The raw generic keyboard events are usually available to the user in some way.  Apps can generally capture these events, and can call generic OS routines to map keys to particular functions (like letters, numbers, Enter, modifier keys, etc).  There is also usually a more cooked layer where apps can get the interpreted key presses.

When keyboard input is routed to something like a terminal window, the key actions are interpreted into a series of ASCII (or other encodings) bytes and stuffed into the STDIN FIFO, which apps can read the other end of.

How events are processed and made available to apps is also very dependent on the OS.  In Windows for example, apps have to provide a special subroutine that gets called from the system on events.  Other systems have an event queue.  It's a FIFO that the OS writes events to, and the app gets the next event (or indication there is no pending event) when it asks.

Nowadays, most keyboards use the same set of low level key codes.  They were originally arbitrary, and each keyboard needed a custom driver.  However, very early on new keyboards were made to be compatible with existing popular keyboards or whatever driver was the default or assumed to come with the system.  As a result, most keyboards now use the same key codes.  Custom drivers to support a particular model keyboard are still possible, but for obvious reasons vendors want to be compatible with the "standard" keyboard driver when possible.