- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <linux/input.h>
- #include <linux/uinput.h>
- void usage(void)
- {
- printf(" Usage : keyboard [key number...]\n");
- printf(" keyboard 29 56 32 /* CTRL + ALT +D */\n");
- printf(" keyboard 125 32 /* WIN +D */\n");
- printf("/* Types of key number are defined in /usr/include/linux/input.h */\n");
- return;
- }
- /* Setup the uinput device */
- int setup_uinput_device(int fd)
- {
- struct uinput_user_dev uidev; // uInput device structure
- memset(&uidev, 0, sizeof(struct uinput_user_dev)); // Intialize the uInput device to NULL
- snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-keyboard");
- uidev.id.bustype = BUS_USB;
- uidev.id.vendor = 0x1;
- uidev.id.product = 0x1;
- uidev.id.version = 1;
- // Setup the driver I/O channels
- ioctl(fd, UI_SET_EVBIT, EV_KEY);
- ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
- for (int i = 0; i < 256; i++) {
- ioctl(fd, UI_SET_KEYBIT, i);
- }
- ioctl(fd, UI_SET_EVBIT, EV_REL);
- ioctl(fd, UI_SET_RELBIT, REL_X);
- ioctl(fd, UI_SET_RELBIT, REL_Y);
- /* Create input device into input sub-system */
- write(fd, &uidev, sizeof(struct uinput_user_dev));
- if (ioctl(fd, UI_DEV_CREATE) < 0) {
- printf("error: create uinput device\n");
- return -1;
- }
- return 0;
- }
- void uinput_key(int fd, int keycode, int keyvalue)
- {
- struct input_event ev; // Input device structure
- memset(&ev, 0, sizeof(struct input_event));
- gettimeofday(&ev.time, NULL);
- ev.type = EV_KEY;
- ev.code = keycode;
- ev.value = keyvalue;
- if (write(fd, &ev, sizeof(struct input_event)) < 0) {
- printf("uinput key error\n");
- return;
- } else {
- printf("uinput key %d, %d\n", keycode, keyvalue);
- }
- return;
- }
- void uinput_key_end(int fd)
- {
- struct input_event ev; // Input device structure
- memset(&ev, 0, sizeof(struct input_event));
- ev.type = EV_SYN;
- ev.code = 0;
- ev.value = 0;
- write(fd, &ev, sizeof(struct input_event));
- return;
- }
- //check the key is control key
- int check_valid_control_key(int key)
- {
- int ret = 0;
- int control_key[] = {29, 42, 54, 56, 97, 100, 125, 126};
- int len = sizeof(control_key) / sizeof(control_key[0]);
- for (int i = 0; i < len; i++) {
- if (control_key[i] == key) {
- ret = 1;
- break;
- }
- }
- return ret;
- }
- int main(int argc, char *argv[])
- {
- int fd = -1;
- int key;
- int i;
-
- if (argc <= 1) {
- usage();
- return -1;
- }
- fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); // Open the input device
- if (fd < 0) {
- printf("error: open /dev/uinput\n");
- return -1;
- }
- setup_uinput_device(fd);
- sleep(1);
- for (i = 1; i < argc; i++) {
- key = atoi(argv[i]);
- uinput_key(fd, key, 1);
- if (check_valid_control_key(key) == 0) {
- uinput_key(fd,key, 0);
- }
- }
- for (i = 1; i < argc; i++) {
- key = atoi(argv[i]);
- if (check_valid_control_key(key) == 1) {
- uinput_key(fd,key, 0);
- }
- }
- uinput_key_end(fd);
- if (ioctl(fd, UI_DEV_DESTROY) < 0) {
- printf("error: ioctl");
- return -1;
- }
- close(fd);
- fd = -1;
- return 0;
- }
阅读(1481) | 评论(0) | 转发(0) |