Using /dev/inputX devices


6 posts
by PeterO » Tue Nov 06, 2012 12:31 pm
Does anyone have experience of using the /dev/inputX devices for picking up keyboard and mouse events for controlling games ?

It looks like they provide a easy to use way of picking up mouse motion and presses and releases of keyboard keys and mouse buttons.

PeterO
User avatar
Posts: 1069
Joined: Sun Jul 22, 2012 4:14 pm
by williamaadams » Tue Nov 06, 2012 7:14 pm
I have found the following articles to be use:
http://www.linuxquestions.org/questions ... ce-615178/

http://www.linuxjournal.com/article/6396
http://www.linuxjournal.com/article/6429

Also, I've been playing around with the same. If you care for the Lua language, you can take a look here:

https://github.com/Wiladams/LJIT2RPi/

In particular, you can take a look at the tests/test_input.c file, which does some stuff.

I incorporate similar, into the Keyboard.lua file.

Basically, you open up a file, and read from it. You get input_event structures, which contain the various bits and pieces of information that you need.

If you want to do things like ask the keyboard for its current state, you make ioctl calls. Similar if you want to do things like turn on/off lights on the keyboard, or start vibrating a joystick.

If you want to get more fancy and actually figure out which unicode values are being typed, because you're doing a text editor, then you'll have to get involved with keymap tables, which I don't know anything about at the moment.

Hope this helps.
Posts: 19
Joined: Thu Oct 11, 2012 4:48 pm
by williamaadams » Tue Nov 06, 2012 7:53 pm
Oh, and I forgot the blog entry I just wrote up about this:

http://williamaadams.wordpress.com/2012 ... al-design/
Posts: 19
Joined: Thu Oct 11, 2012 4:48 pm
by PeterO » Tue Nov 06, 2012 8:11 pm
Thanks.

Google had already found the Linux Journal articles for me, but the other links are useful.
I also found "evtest" which is very informative.

One thing I'm not sure about at the moment is if my code watches the keyboard input device (eg for shift key presses) what should it be doing with the characters coming in via stdin as a result of normal keypresses ?

PeterO
User avatar
Posts: 1069
Joined: Sun Jul 22, 2012 4:14 pm
by williamaadams » Wed Nov 07, 2012 1:01 am
If you're reading from stdin, you don't want to be reading /dev/input/event, the latter being low level. stdin will give you the 'characters' coming from the keyboard, already mapped. There will be a difference between 'd' and 'D'. The keymapping has already occured.

If you read from /dev/input/eventxxx, then no key mapping is happening. You're getting raw virtual keys from the keyboard, so you will get KEY_D, and KEY_LSHIFT as separate 'events' and it's up to you to keep your own state management.

Another typical game loop thing to do is simply ask the keyboard for its current state of all the keys at once, by calling ioctl. Then you do what you want from there.

So, it depends on what kind of interaction you want your game to have, but those links should show you how to read from the keyboard raw.
Posts: 19
Joined: Thu Oct 11, 2012 4:48 pm
by PeterO » Wed Nov 07, 2012 9:57 am
williamaadams wrote:If you're reading from stdin, you don't want to be reading /dev/input/event, the latter being low level. stdin will give you the 'characters' coming from the keyboard, already mapped. There will be a difference between 'd' and 'D'. The keymapping has already occured.
Yes. ... and ... ;)
If you read from /dev/input/eventxxx, then no key mapping is happening. You're getting raw virtual keys from the keyboard, so you will get KEY_D, and KEY_LSHIFT as separate 'events' and it's up to you to keep your own state management.
Which is exactly what I want so that I can track keys like Alt/Ctl/Shift/Windblows to modify the effect of mouse movements. (E.g. Alt with mouse up/down = zoom in/out Shift with mouse = Pan and TIlt Mouse alone = move forward/backward/side to side.)
Another typical game loop thing to do is simply ask the keyboard for its current state of all the keys at once, by calling ioctl. Then you do what you want from there.
I prefer to put all inputs into a poll loop (in gtk that is handled via g_io_add_watch etc)
So, it depends on what kind of interaction you want your game to have, but those links should show you how to read from the keyboard raw.

PeterO
User avatar
Posts: 1069
Joined: Sun Jul 22, 2012 4:14 pm