Read the keyboard directly under Linux

xiaoxiao2021-03-06  50

Direct control of the keyboard under Linux, you need to figure out some concepts, such as Terminal, Console, Scancode, KeyCode, etc. I understand that the basic process of keyboard processing under Linux should be like this:

Keyboard -> KBD Controller --------> Keyboard Driver ------> TTY Processing --- button generates scanning code according to keyboard mode conversion ---- TTY processing ------- > console ---------> Application Processing Canonical Mode and other processing ESC sequences, etc.

About Keyboard, Console's basic processes can refer to 2 and 3 in the Linux Keyboard and Console Howto. About TTY can refer to 11 Chapter 10 Terminal I / O in Advanced Programming In Unix Environment.

Understand this process, you have to get the keyboard input directly.

First, you need to set the TTY mode to noncanonical, refer to the example in Advanced Programming.

Second, it is necessary to set the keyboard conversion mode. In the usual mode, it is converted to the ASCII code, or it can be converted to Scancode, Keycode, and Unicode. About ScanCode definitions are more troublesome, one key may generate 1, 2, 4 or even 5 Scancode, which can refer to Microsoft's document "Keyboard Scan Code Specification". About Keycode, there is a program called showkey, you can display a key corresponding to the code, because a button only corresponds to one code, is relatively simple. Specific settings Use the parameters to call the IOCTL call to KdskBMode, refer to the Man Document Console_iocTl.

Finally, set the console to kd_graphics mode (IOCTL call with parameter kdsetMode), console enters graphics mode, not displaying text cursors, and the output of SDTOUT will only display after returning text mode.

It is good to do it clearly, and the rest is to open the device and read data. The general stdin is a TTY, of course, can also open / dev / console device or / dev / tty, / dev / tty1, / dev / tty2, etc. What is the difference between / dev / console and TTY, I can't figure it out. It can be used at least after CONSOLE and TTY. STDIN is generally the current TTY, if there is no redirection.

Of course, it is important to note that once the keyboard conversion mode is switched, all keyboard code to the application will change, and it may be not recognized by Ctrl-C, so I must remember to switch back, debugging should be careful, otherwise it may To restart :-(

Summarize the reference: "The Linux Keyboard and Console Howto" "Advanced Programming in Unix Environment" "Keyboard Scan Code Specification" "MAN Document: Console_iocTl (4)"

转载请注明原文地址:https://www.9cbs.com/read-40806.html

New Post(0)