开发者

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

开发者 https://www.devze.com 2022-12-29 19:37 出处:网络
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 equival

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开发者_JS百科) 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.


SDL will do the trick I believe.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号