Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard device in Unix

How to capture all the user input, reading one byte at a time?

I did this coding, but it's not working

if ((fd = open("/dev/char", O_RDONLY) != 1) {
    tty = open("/dev/tty", O_RDONLY);
    tcsetattr(0, TCSANOW, &orig_info);
    read (fd, &buf, sizeof(char));
}
close(fd);

I expected the terminal to wait for input, but it didn't.

like image 270
Bunny Bunny Avatar asked Apr 07 '13 22:04

Bunny Bunny


People also ask

Is keyboard a character device?

Character devices, or char devices, are accessed as a stream of sequential data, one byte after another. Example character devices are serial ports and keyboards.

How is keyboard input handled by Linux?

The keyboard sends a scancode to the computer. The Linux kernel maps the scancode to a keycode, see Map scancodes to keycodes. The keyboard layout maps the keycode to a symbol or keysym, depending on what modifier keys are pressed.

What are keyboard and mouse known as in Linux?

Linux (at the operating system kernel level) supports four interfaces to a HID device - keyboard, mouse, joystick and a generic interface, known as the event interface.

Are there Linux keyboards?

Manufacturers don't make keyboards just for Linux, but many PC keyboards are Linux-compatible. Unlike Max and Windows (PC), Linux has multiple distributions, so it doesn't have a universal keyboard layout.


2 Answers

The keyboard device itself is one of the entries in /dev/input. You can locate keyboards and other input devices by their connection type (e.g. PS/2, USB, …) in /dev/input/by-path. Obviously, you'll need to run as root to access the hardware directly, and you'll need to provide your own translation from raw bytes coming from the keyboard into things like key presses and key releases. This is probably not what you want.

If you're running a GUI application, the low-level method is to call XNextEvent and other functions in the same family. Decoding input events isn't completely trivial, as it's up to applications to apply modifiers. A GUI framework (Motif, Gtk, Qt, …) would help you.

If you're running a terminal application, read from standard input or from /dev/tty (/dev/tty is always the terminal that your program is running on, even if standard input has been redirected). You'll want to put the terminal in raw mode. You'll get decoded character keys, and function keys mostly as escape sequences. Here, too, a library helps; the de facto standard is ncurses.

like image 157
Gilles 'SO- stop being evil' Avatar answered Nov 02 '22 08:11

Gilles 'SO- stop being evil'


What exactly do you want to do?

If what you are looking for is unbuffered input in terminal, tcsetattr() is what you are looking for - you need to put the terminal into the non-canonical mode, i.e. set the terminal flags not to contain ICANON - see e.g. this code snippet. See man termios (or the function name, it usually links to the same man page on Linux).

If you want to have exclusive access to the keyboard, it is much more complicated.

like image 30
peterph Avatar answered Nov 02 '22 08:11

peterph