TimE
TimE

Reputation: 2887

How can you track mouse input using the fewest libraries in c

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.

Upvotes: 6

Views: 6966

Answers (3)

Thomas Matthews
Thomas Matthews

Reputation: 57729

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.

Upvotes: 0

m0s
m0s

Reputation: 4280

SDL will do the trick I believe.

Upvotes: 4

Yann Ramin
Yann Ramin

Reputation: 33197

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.

Upvotes: 3

Related Questions