views:

872

answers:

4

GNU/Linux text console, X11 not involved, indeed not even installed. Keyboard is US layout, keymap US default. Kernel version 2.20.x or later.

An application written in C is getting keyboard input in translation mode, i.e. XLATE or UNICODE. When a key is pressed, the application receives the corresponding keystring. As an example, you press F1, the application reads "\033[[A".

Before the kernel sends the keystring to the application, it must know which key is pressed, i.e. it must know its scancode. In the F1 example above, the scancode for the key pressed is 59 or 0x3b.

That's to say even when the keyboard is in translation mode, the scancodes are held somewhere in memory. How can the application access them without switching the keyboard to RAW or MEDIUMRAW mode? A code snippet would help.

+1  A: 

Sure, the code you want to look at is in kbd-1.12.tar.bz2, which is the source bundle for the 'kbd' package. The 'kbd' package provides tools such as 'dumpkeys', 'showkeys' and 'loadkeys', which are useful for looking at the current keyboard mapping, checking what keys emit what scancodes, and loading a new mapping.

You will have to communicate with the kernel via ioctls, and it's quite complicated, so I recommend reading the source of that package to see how it's done.

Here's a link to the tarball: kbd-1.12.tar.bz2 (618K).

Jerub
A: 

At a terminal I entered "dumpkeys -f > test.txt" and there was a great deal of detailed information, including:

keycode 29 = Control
...
string F1 = "\033[[A"
string F2 = "\033[[B"
string F3 = "\033[[C"
string F4 = "\033[[D"
string F5 = "\033[[E"
string F6 = "\033[17~"
string F7 = "\033[18~"
string F8 = "\033[19~"
...
string Prior = "\033[5~"
string Next = "\033[6~"
string Macro = "\033[M"
string Pause = "\033[P"

dumpkeys was included by default with my distribution. But you should be able to find it in what jerub posted. I would start by looking kbd-1.12/src/loadkeys.y.

It looks like the kernel is responsible for holding that data, and can report to those who know how to ask.

Sqeaky
A: 

You maybe want to look at kbdev or evdev (look at your Documentation/input/input.txt file in your kernel source directory for starters.) That would work for console access.

Steve Baker
+2  A: 

Chances are that you are issuing the ioctl commands on the wrong file descriptor, check for error codes coming back from ioctl and tcsetattr.

You should be opening the console device, and then issuing your keyboard translation commands on that device. You would have to basically mimic what the X server is doing.

This is a link to the source code on codesearch.google.com: http://tinyurl.com/5yd92j

miguel.de.icaza