Created
May 20, 2016 03:13
-
-
Save matthewaveryusa/a721aad80ae89a5c69f7c964fa20fec1 to your computer and use it in GitHub Desktop.
linux grab io from keyboard and mouse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <linux/input.h> | |
#include <time.h> | |
#include <stdint.h> | |
int main(int argc, char* argv[]){ | |
sleep(1); | |
int rcode = 0; | |
char keyboard_name[256] = "Unknown"; | |
int keyboard_fd = open("/dev/input/event1", O_RDONLY | O_NONBLOCK); | |
if ( keyboard_fd == -1 ) { | |
printf("Failed to open keyboard.\n"); | |
exit(1); | |
} | |
rcode = ioctl(keyboard_fd, EVIOCGNAME(sizeof(keyboard_name)), keyboard_name); | |
printf("Reading From : %s \n", keyboard_name); | |
printf("Getting exclusive access: "); | |
rcode = ioctl(keyboard_fd, EVIOCGRAB, 1); | |
printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE"); | |
struct input_event keyboard_event; | |
char mouse_name[256] = "Unknown"; | |
int mouse_fd = open("/dev/input/event2", O_RDONLY | O_NONBLOCK); | |
if ( mouse_fd == -1 ) { | |
printf("Failed to open mouse.\n"); | |
exit(1); | |
} | |
rcode = ioctl(mouse_fd, EVIOCGNAME(sizeof(mouse_name)), mouse_name); | |
printf("Reading From : %s \n", mouse_name); | |
printf("Getting exclusive access: "); | |
rcode = ioctl(mouse_fd, EVIOCGRAB, 1); | |
printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE"); | |
struct input_event mouse_event; | |
int end = time(NULL) + 10; | |
while ( time(NULL) < end ) { | |
if ( read(keyboard_fd, &keyboard_event, sizeof(keyboard_event)) != -1 ) { | |
printf("keyboard event: type %d code %d value %d \n", keyboard_event.type, keyboard_event.code, keyboard_event.value); | |
} | |
if ( int sz = read(mouse_fd, &mouse_event, sizeof(mouse_event)) ) { | |
if(sz != -1) { | |
printf("mouse event: type %d code %d value %d \n", mouse_event.type, mouse_event.code, mouse_event.value); | |
} | |
} | |
} | |
printf("Exiting.\n"); | |
rcode = ioctl(keyboard_fd, EVIOCGRAB, 1); | |
close(keyboard_fd); | |
rcode = ioctl(mouse_fd, EVIOCGRAB, 1); | |
close(mouse_fd); | |
return 0; | |
} |
Thanks a lot!
Thanks.
Question: shouldn't we be sending 0
to ungrab, towards the end?
printf("Exiting.\n");
rcode = ioctl(keyboard_fd, EVIOCGRAB, 0);
close(keyboard_fd);
rcode = ioctl(mouse_fd, EVIOCGRAB, 0);
close(mouse_fd);
return 0;
Also, might want to add a usleep(100000);
before you grab the device - see https://stackoverflow.com/a/41997935/9488865 for more on that.
https://stackoverflow.com/questions/41995349/why-does-ioctlfd-eviocgrab-1-cause-key-spam-sometimes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came for
ioctl(mouse_fd, EVIOCGRAB, 1);
, got more than expected :)