Skip to content

Instantly share code, notes, and snippets.

@jahir
Created April 14, 2017 20:48
Show Gist options
  • Save jahir/a99e0e55c1a1aedb8c776e52c811212a to your computer and use it in GitHub Desktop.
Save jahir/a99e0e55c1a1aedb8c776e52c811212a to your computer and use it in GitHub Desktop.
read (midde) mouse button state every 5ms
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/input.h>
#include <stdint.h>
#define test_bit(p, n) !!(p[n>>3] & (1u << (n&7)))
int isButtonPressed(int fd, unsigned short code) {
uint8_t keys[(KEY_MAX<<3)+1];
memset(keys, 0, sizeof(keys));
ioctl(fd, EVIOCGKEY(sizeof(keys)), keys);
return test_bit(keys, code);
}
int main(int argc, char* argv[])
{
char * dev;
int fd;
if (argc < 2) {
fprintf(stderr, "Usage: %s /dev/input/eventX\n", argv[0]);
return 1;
}
dev = argv[1];
printf("using %s\n", dev);
if ((fd = open(dev, O_RDWR)) < 0) {
perror("open event device");
return 1;
}
while (1) {
write(1, isButtonPressed(fd, BTN_MIDDLE) ? "X" : ".", 1);
usleep(5000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment