views:

219

answers:

1

Hello. I have been thinking alot over keyboard handling. How does it work? I cant seem to google me to a good explaining. I know that a keyboard interrupt is made every time a key is pressed. The processor halts whatever it is processing and load the keyboard data from the keyboard buffer, storing it in a system level buffer. But what happens next? let take a practical example. What happens when I run the following piece of code

...
std::string s;
std::cin >> s;
....

does the cin read from a user level representation of the system level keyboard buffer? That makes perfect sense in my head because then 2 or more processes can read from the same buffer, and by that way I dont loose any key presses. But does it work this way? I know Im talking in very general terms. The OS Im using is OS X. Hope that someone can give an answer to this general question about a specific subject.

+9  A: 

Except in rare situations, your keyboard and display are managed by a Window Manager: X11, Gnome, KDE, Carbon, Cocoa or Windows.

It works like this.

The keyboard driver is part of the OS.

The window manager is a privileged process, which acquires the device during startup. The window manager "owns" the device. Exclusively.

  1. The interrupts go to OS.

  2. The OS responds the interrupt by queueing. Eventually -- when there's nothing of a higher priority to do -- it captures the keyboard input from the interrupt and buffers it.

  3. The owning process (the window manager) is reading this buffer. From this, it creates keyboard events.

Your application works through the window manager.

Example 1 -- You're running a command-line application. In a terminal window. When terminal window is front-most, the window manager directs events at the terminal window. Keyboard events become the stdin stream.

Example 2 -- you're running GUI application. In your own application's window. When your application's window is front-most, the window manager direct events at your application window. Keyboard events are available for your various GUI controls to process. Some keyboard events may cycle among the controls or active buttons.

S.Lott
So the WM owns the keyboard. The OS handles the interrupts and copy the buffer from kernel level to the userlevel where the WM handles the buffer. The WM handles the events to the different applications making sure that the things I write on my keyboard is shown in the text app?
mslot
Well, more correctly, the window server (X11, Cocoa, or Win32) passes messages to the focused window that carry the keystroke information. The input buffer is implemented in part by the input driver and simply read by the window server. All the app sees is window messages.
greyfade
Ah okay. I was just wondering how different processses read from the keyboard buffer, but I can see now that the window server does it. Does one of you have some links to papers describing this mechanism?
mslot
Read the source for X11, KDE or Gnome. Seriously.
S.Lott
Okay. Thanks for helping.
mslot
http://www.gtk.org/documentation.html, http://library.gnome.org/devel/references
S.Lott