williamaadams
Posts: 19
Joined: Thu Oct 11, 2012 4:48 pm

Doing simple keyboard/mouse event loops

Thu Oct 11, 2012 4:55 pm

I am currently putting together some graphics frameworks using bcm_host/opengl. The one piece I'm lacking right now is my basic mouse and keyboard handling.

Are there already existant methods for asynchronous, or at least non-blocking, ways of reading mouse and keyboard data? I don't want to use X or any other framework, I just want to get raw device data streams.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: Doing simple keyboard/mouse event loops

Thu Oct 11, 2012 6:10 pm

You can read the raw input for mouse using the basic code in the GLES2 demo from the pi source directory

static int fd = -1;
fd = open("/dev/input/mouse0",O_RDONLY|O_NONBLOCK);

then

struct {char buttons, dx, dy; } m;
int bytes = read(fd, &m, sizeof m);

However I usually find it's best to use another library (personally I use SDL and create a 0,0 window this allows you to setup your graphics using EGL then use the full SDL event processing for mouse and keyboard).

If you want to program it yourself I would start with these articles

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

but It is quite a complex task

williamaadams
Posts: 19
Joined: Thu Oct 11, 2012 4:48 pm

Re: Doing simple keyboard/mouse event loops

Thu Oct 11, 2012 6:19 pm

Thanks for the pointers. I will probably go this low level route as I want to keep my dependencies low, and in the future I might be able to pick up more devices.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: Doing simple keyboard/mouse event loops

Thu Oct 11, 2012 6:29 pm

May be worth looking at this http://www.glfw.org/ it does keyboard and mouse input so the source may have some pointers (but I think it uses the X11 libraries).

May be worth having a look at how libcurses does things as well as this is the lowest level input lib I can think of

http://www.gnu.org/software/ncurses/

Return to “General programming discussion”