Jerry Kirk
Jerry Kirk

Reputation: 71

How can I translate Linux keycodes from /dev/input/event* to ASCII

I'm trying to convert keyboard events read from /dev/input/event0 from the values defined in to their ASCII equivalent inside an embedded application that is not running X or a terminal.

I think this should be done via keymap functionality defined within Linux rather than just creating my own std::map<> but I can't seem to find a good place to start. Most of the examples I have found so far assume I am running with X windows or with a terminal.

Upvotes: 7

Views: 4654

Answers (2)

iamsrijon
iamsrijon

Reputation: 74

You can read the below structure from /dev/input/event0

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

For more details: https://www.kernel.org/doc/Documentation/input/input.txt

Upvotes: 0

Andy Ross
Andy Ross

Reputation: 12051

Text input (except for the very simple case of the traditional US keyboard and the 7 bit ASCII standard) is an immensely complicated field. I'd very strongly suggest you do this using an X client, where you can take advantage of all the existing input methods.

But if you must, and you're happy with one kind of keyboard and one language, you do this by interpreting the events just as a terminal would. Check the defintion in /usr/include/linux/input.h for the values. Track the position of the Shift and Ctrl keys (non-ASCII keys like Alt, Fn, etc... are up to you to interpret, of course) and emit the corresponding byte on the key up event. Maybe you'll want to implement an auto-repeat facility too if the defaults don't work for you application.

But basically: don't. This is a much (!) harder problem than you seem to realize.

Upvotes: 1

Related Questions