5

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.

TimE
  • 2,800
  • 1
  • 27
  • 25

3 Answers3

4

SDL will do the trick I believe.

m0s
  • 4,250
  • 9
  • 41
  • 64
2

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
  • 32,895
  • 3
  • 59
  • 82
0

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
  • 56,849
  • 17
  • 98
  • 154
  • 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 May 08 '10 at 04:46
  • @TimE cross platform game libraries have been tracking mouse input with highest precision for long time already... some even able to access raw mouse input. If you don't want to use one, at least its a good place to look for the solution. GLUT & SDL are the right place to start looking, both are open source. Good luck. – m0s May 08 '10 at 19:15