Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 650
Jerry Kirk Avatar asked Jul 11 '12 15:07

Jerry Kirk


1 Answers

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.

like image 110
Andy Ross Avatar answered Nov 15 '22 05:11

Andy Ross