tags:

views:

217

answers:

3

I'm not sure where to look to find this information, but I'd like to know how to get mouse input (or any hid input) using the fewest non standard libraries in c. Basically, is there an stdio equivalent for mouse (and other input) input in c? Or is there a library that is minimal and cross compatible on multiple platforms. Just being able to print mouse coordinates to a terminal window would be enough.

A: 

Here is a 0 non-standard library approach. Runs wherever /dev/input/event# exists.

#include <linux/input.h>
#include <fcntl.h>    

int main(int argc, char **argv)
{
int fd;
if ((fd = open("/dev/input/mice", O_RDONLY)) < 0) {
    perror("evdev open");
    exit(1);
}

struct input_event ev;

while(1) {
    read(fd, &ev, sizeof(struct input_event));
    printf("value %d, type %d, code %d\n",ev.value,ev.type,ev.code);
}

return 0;
}

On Windows, you need to do something ghastly with the Win32 API and hook into the message system.

In short, no, there is no standard in C for this.

Yann Ramin
That's actually capturing keyboard signals for me :)
LukeN
this is not cross platform
klez
@klez: I don't think there really is a cross-platform way to do this.
Zan Lynx
+1  A: 

SDL will do the trick I believe.

m0s
A: 

Depends on the platform and the operating system. If you want to "track" mouse (or HID) events directly, you may have to write a driver or find one. The driver will monitor the port and listen for mouse data (messages).

On an event driven system, you will have to subscribe to mouse event messages. Some OS's will broadcast the messages to every task (such as Windows), while others will only send the events to subscribers.

This is too general of a question to receive a specific answer.

Thomas Matthews
Thanks, it's general because I'm just starting out on input programming and I have no idea where to start! To be more specific, I want as low to the driver as I can get, without having to write one.I'm interested in programming tabletPC input on windows and linux, but I've searched everywhere for a cross platform way to do it, and I've come up with nothing. So instead I want to write one (if none exist). I want it low level so it's less dependent on OS specific libraries. If you could answer that or direct me in the right direction to find out for myself that would be amazing!
TimE
m0s