Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4261140
  • 博文数量: 776
  • 博客积分: 13014
  • 博客等级: 上将
  • 技术积分: 10391
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-22 17:00
文章分类

全部博文(776)

文章存档

2015年(55)

2014年(43)

2013年(147)

2012年(20)

2011年(82)

2010年(429)

分类: LINUX

2013-09-04 09:02:18

概述:
查看/dev/input/eventX是什么类型的事件, cat /proc/bus/input/devices


设备有着自己特殊的按键键码,我需要将一些标准的按键,比如0-9,X-Z等模拟成标准按键,比如KEY_0,KEY-Z等,所以需要用到按键模拟,具体方法就是操作/dev/input/event1文件,向它写入个input_event结构体就可以模拟按键的输入了。

input_event 说明:
linux/input.h中有定义,这个文件还定义了标准按键的编码等

点击(此处)折叠或打开

  1. struct input_event {

  2. struct timeval time; //按键时间

  3. __u16 type; //类型,在下面有定义

  4. __u16 code; //要模拟成什么按键

  5. __s32 value;//是按下还是释放

  6. };

code
事件的代码.如果事件的类型代码是EV_KEY,该代码code为设备键盘代码.代码植0~127为键盘上的按键代码,0x110~0x116 为鼠标上按键代码,其中0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看include/linux/input.h文件. 如果事件的类型代码是EV_REL,code值表示轨迹的类型.如指示鼠标的X轴方向REL_X(代码为0x00),指示鼠标的Y轴方向REL_Y(代码为0x01),指示鼠标中轮子方向REL_WHEEL(代码为0x08).

type:
EV_KEY,键盘
EV_REL,相对坐标
EV_ABS,绝对坐标

value
事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0; 如果事件的类型代码是EV_ REL,value的正数值和负数值分别代表两个不同方向的值.

/*
* Event types
*/
#define EV_SYN 0x00
#define EV_KEY 0x01 //按键
#define EV_REL 0x02 //相对坐标(轨迹球)
#define EV_ABS 0x03 //绝对坐标
#define EV_MSC 0x04 //其他
#define EV_SW 0x05
#define EV_LED 0x11 //LED
#define EV_SND 0x12//声音
#define EV_REP 0x14//repeat
#define EV_FF 0x15
#define EV_PWR 0x16
#define EV_FF_STATUS 0x17
#define EV_MAX 0x1f
#define EV_CNT (EV_MAX+1)

kernel里input模

点击(此处)折叠或打开

  1. //input_dev结构:
  2. struct input_dev
  3. {
  4.     void *private;

  5.     const char *name;
  6.     const char *phys;
  7.     const char *uniq;
  8.     struct input_id id;

  9.     /*
  10.     * 根据各种输入信号的类型来建立类型为unsigned long 的数组,
  11.     * 数组的每1bit代表一种信号类型,
  12.     * 内核中会对其进行置位或清位操作来表示时间的发生和被处理.
  13.     */

  14.     unsigned long evbit[NBITS(EV_MAX)];
  15.     unsigned long keybit[NBITS(KEY_MAX)];
  16.     unsigned long relbit[NBITS(REL_MAX)];
  17.     unsigned long absbit[NBITS(ABS_MAX)];
  18.     unsigned long mscbit[NBITS(MSC_MAX)];
  19.     unsigned long ledbit[NBITS(LED_MAX)];
  20.     unsigned long sndbit[NBITS(SND_MAX)];
  21.     unsigned long ffbit[NBITS(FF_MAX)];
  22.     unsigned long swbit[NBITS(SW_MAX)];

  23.     .........................................
  24. };

  25. /**
  26. * input_set_capability - mark device as capable of a certain event
  27. * @dev: device that is capable of emitting or accepting event
  28. * @type: type of the event (EV_KEY, EV_REL, etc...)
  29. * @code: event code
  30. *
  31. * In addition to setting up corresponding bit in appropriate capability
  32. * bitmap the function also adjusts dev->evbit.
  33. */

  34. /* 记录本设备对于哪些事件感兴趣(对其进行处理)*/
  35. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  36. {
  37.     switch (type)
  38.     {
  39.     case EV_KEY:
  40.         __set_bit(code, dev->keybit);//比如按键,应该对哪些键值的按键进行处理(对于其它按键不予理睬)
  41.         break;

  42.     case EV_REL:
  43.         __set_bit(code, dev->relbit);
  44.         break;

  45.     case EV_ABS:
  46.         __set_bit(code, dev->absbit);
  47.         break;

  48.     case EV_MSC:
  49.         __set_bit(code, dev->mscbit);
  50.         break;

  51.     case EV_SW:
  52.         __set_bit(code, dev->swbit);
  53.         break;

  54.     case EV_LED:
  55.         __set_bit(code, dev->ledbit);
  56.         break;

  57.     case EV_SND:
  58.         __set_bit(code, dev->sndbit);
  59.         break;

  60.     case EV_FF:
  61.         __set_bit(code, dev->ffbit);
  62.         break;

  63.     default:
  64.         printk(KERN_ERR
  65.                "input_set_capability: unknown type %u (code %u)\n",
  66.                type, code);
  67.         dump_stack();
  68.         return;
  69.     }

  70.     __set_bit(type, dev->evbit);//感觉和前面重复了(前面一经配置过一次了)
  71. }
  72. EXPORT_SYMBOL(input_set_capability);

  73. static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
  74. {
  75.     int i;
  76.     struct platform_device *pdev = dev_id;
  77.     struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  78.     struct input_dev *input = platform_get_drvdata(pdev);

  79.     for (= 0; i < pdata->nbuttons; i++)
  80.     {
  81.         struct gpio_keys_button *button = &pdata->buttons[i];
  82.         int gpio = button->gpio;

  83.         if (irq == gpio_to_irq(gpio)) //判断哪个键被按了?
  84.         {
  85.             unsigned int type = button->type ? : EV_KEY;
  86.             int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;//记录按键状态

  87.             input_event(input, type, button->code, !!state);//汇报输入事件
  88.             input_sync(input);//等待输入事件处理完成
  89.         }
  90.     }

  91.     return IRQ_HANDLED;
  92. }


  93. /*
  94. * input_event() - report new input event
  95. * @dev: device that generated the event
  96. * @type: type of the event
  97. * @code: event code
  98. * @value: value of the event
  99. *
  100. * This function should be used by drivers implementing various input devices
  101. * See also input_inject_event()
  102. */
  103. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  104. {
  105.     struct input_handle *handle;

  106.     if (type > EV_MAX || !test_bit(type, dev->evbit))//首先判断该事件类型是否有效且为该设备所接受
  107.         return;

  108.     add_input_randomness(type, code, value);

  109.     switch (type)
  110.     {

  111.     case EV_SYN:
  112.         switch (code)
  113.         {
  114.         case SYN_CONFIG:
  115.             if (dev->event)
  116.                 dev->event(dev, type, code, value);
  117.             break;

  118.         case SYN_REPORT:
  119.             if (dev->sync)
  120.                 return;
  121.             dev->sync = 1;
  122.             break;
  123.         }
  124.         break;

  125.     case EV_KEY:
  126.         /*
  127.         * 这里需要满足几个条件:
  128.         * 1: 键值有效(不超出定义的键值的有效范围)
  129.         * 2: 键值为设备所能接受(属于该设备所拥有的键值范围)
  130.         * 3: 按键状态改变了
  131.         */

  132.         if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  133.             return;

  134.         if (value == 2)
  135.             break;

  136.         change_bit(code, dev->key);//改变对应按键的状态

  137.         /* 如果你希望按键未释放的时候不断汇报按键事件的话需要以下这个(在简单的gpio_keys驱动中不需要这个,暂时不去分析) */
  138.         if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value)
  139.         {
  140.             dev->repeat_key = code;
  141.             mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  142.         }

  143.         break;
  144.         ........................................................

  145.         if (type != EV_SYN)
  146.             dev->sync = 0;

  147.         if (dev->grab)
  148.             dev->grab->handler->event(dev->grab, type, code, value);
  149.         else
  150.             /*
  151.             * 循环调用所有处理该设备的handle(event,mouse,ts,joy等),
  152.             * 如果有进程打开了这些handle(进行读写),则调用其对应的event接口向气汇报该输入事件.
  153.             */
  154.             list_for_each_entry(handle, &dev->h_list, d_node)
  155.             if (handle->open)
  156.                 handle->handler->event(handle, type, code, value);
  157.     }
  158. }
  159. EXPORT_SYMBOL(input_event);


  160. //event层对于input层报告的这个键盘输入事件的处理:
  161. //drivers / input / evdev.c:

  162. static struct input_handler evdev_handler =
  163. {
  164.     .event = evdev_event,
  165.     .connect = evdev_connect,
  166.     .disconnect = evdev_disconnect,
  167.     .fops = &evdev_fops,
  168.     .minor = EVDEV_MINOR_BASE,
  169.     .name = "evdev",
  170.     .id_table = evdev_ids,
  171. };


Linux 有自己的 input 子系统,可以统一管理鼠标和键盘事件。基于输入子系统 实现的 uinput 可以方便的在用户空间模拟鼠标和键盘事件。
当然,也可以自己造轮子, 做一个字符设备接收用户输入,根据输入,投递 input 事件。
还有一种方式就是直接 往 evnent 里写入数据, 都可以达到控制鼠标键盘的功能。

本篇文章就是演示直接写入 event 的方法。 


1。模拟按键输

点击(此处)折叠或打开

  1. //其中0表示释放,1按键按下,2表示一直按下
  2. //0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

  3. void simulate_key(int fd, int value)
  4. {
  5.     struct input_event event;
  6.     event.type = EV_KEY;
  7. //event.code = KEY_0;//要模拟成什么按键
  8.     event.value = value;//是按下还是释放按键或者重复
  9.     gettimeofday(&event.time, 0);
  10.     if(write(fd, &event, sizeof(event)) < 0)
  11.     {
  12.         dprintk("simulate key error~~~\n");
  13.         return ;
  14.     }
  15. }

2。模拟鼠标输入(轨迹球

点击(此处)折叠或打开

  1. void simulate_mouse(int fd, char buf[4])
  2. {
  3.     int rel_x, rel_y;
  4.     static struct input_event event, ev;

  5.     //buf[0],buf[2],小于0则为左移,大于0则为右移
  6.     //buf[1],buf[3],小于0则为下移,大于0则为上移
  7.     
  8.     dprintk("MOUSE TOUCH: x1=%d,y1=%d,x2=%d,y2=%d\n", buf[0], buf[1], buf[2], buf[3]);
  9.     rel_x = (buf[0] + buf[2]) / 2;
  10.     rel_y = -(buf[1] + buf[3]) / 2; //和我们的鼠标是相反的方向,所以取反
  11.     event.type = EV_REL;
  12.     event.code = REL_X;
  13.     event.value = rel_x;
  14.     gettimeofday(&event.time, 0);
  15.     if( write(fd, &event, sizeof(event)) != sizeof(event))
  16.         dprintk("rel_x error~~~:%s\n", strerror(errno));
  17.     event.code = REL_Y;
  18.     event.value = rel_y;
  19.     gettimeofday(&event.time, 0);
  20.     if( write(fd, &event, sizeof(event)) != sizeof(event))
  21.         dprintk("rel_y error~~~:%s\n", strerror(errno));

  22.     //一定要刷新空的
  23.     write(fd, &ev, sizeof(ev));
  24. }

鼠标和键盘文件打开方法:

点击(此处)折叠或打开

  1. int fd_kbd; // /dev/input/event1
  2. int fd_mouse; //dev/input/mouse2

  3. fd_kbd = open("/dev/input/event1", O_RDWR);

  4. if(fd_kbd <= 0)
  5. {
  6.     printf("error open keyboard:%s\n", strerror(errno));
  7.     return -1;
  8. }

  9. fd_mouse = open("/dev/input/event3", O_RDWR); //如果不行的话,那试试/dev/input/mice
  10. if(fd_mouse <= 0)
  11. {
  12.     printf("error open mouse:%s\n", strerror(errno));
  13.     return -2;
  14. }

/dev/input/mice是鼠标的抽象,代表的是鼠标,也许是/dev/input/mouse,/dev/input/mouse1,或者空,这个文件一直会存在。这里你也许会问,我怎么知道/dev/input/eventX这些事件到底是什么事件阿,是鼠标还是键盘或者别的,eventX代表的是所有输入设备(input核心)的事件,比如按键按下,或者鼠标移动,或者游戏遥控器等等,在系统查看的方法是 cat /proc/bus/input/devices 就可以看到每个eventX是什么设备的事件了。

下面是一个模拟鼠标和键盘输入的例子
关于这里 open 哪个 event , 可以通过 cat /proc/bus/input/device


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <linux/input.h>
  7. #include <linux/uinput.h>
  8. #include <stdio.h>
  9. #include <sys/time.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>

  12. void simulate_key(int fd, int kval)
  13. {
  14.     struct input_event event;
  15.     event.type = EV_KEY;
  16.     event.value = 1;
  17.     event.code = kval;
  18.     gettimeofday(&event.time, 0);
  19.     write(fd, &event, sizeof(event)) ;
  20.     event.type = EV_SYN;
  21.     event.code = SYN_REPORT;
  22.     event.value = 0;
  23.     write(fd, &event, sizeof(event));

  24.     memset(&event, 0, sizeof(event));
  25.     gettimeofday(&event.time, NULL);
  26.     event.type = EV_KEY;
  27.     event.code = kval;
  28.     event.value = 0;
  29.     write(fd, &event, sizeof(event));
  30.     event.type = EV_SYN;
  31.     event.code = SYN_REPORT;
  32.     event.value = 0;
  33.     write(fd, &event, sizeof(event));
  34. }

  35. void simulate_mouse(int fd)
  36. {
  37.     struct input_event event;
  38.     memset(&event, 0, sizeof(event));
  39.     gettimeofday(&event.time, NULL);
  40.     event.type = EV_REL;
  41.     event.code = REL_X;
  42.     event.value = 10;
  43.     write(fd, &event, sizeof(event));
  44.     event.type = EV_REL;
  45.     event.code = REL_Y;
  46.     event.value = 10;
  47.     write(fd, &event, sizeof(event));
  48.     event.type = EV_SYN;
  49.     event.code = SYN_REPORT;
  50.     event.value = 0;
  51.     write(fd, &event, sizeof(event));
  52. }

  53. int main()
  54. {
  55.     int fd_kbd;
  56.     int fd_mouse;
  57.     fd_kbd = open("/dev/input/event1", O_RDWR);
  58.     if(fd_kbd <= 0)
  59.     {
  60.         printf("error open keyboard:\n");
  61.         return -1;
  62.     }
  63.     fd_mouse = open("/dev/input/event2", O_RDWR);
  64.     if(fd_mouse <= 0)
  65.     {
  66.         printf("error open mouse\n");
  67.         return -2;
  68.     }
  69.     int i = 0;
  70.     for(i = 0; i < 10; i++)
  71.     {
  72.         simulate_key(fd_kbd, KEY_A + i);
  73.         simulate_mouse(fd_mouse);
  74.         sleep(1);
  75.     }
  76.     close(fd_kbd);
  77. }


下面是一个读取 鼠标和键盘事件的例子

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <linux/input.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <errno.h>

  9. static void show_event(struct input_event* event)
  10. {
  11.     printf("%d %d %d\n", event->type, event->code, event->value);
  12.     return;
  13. }

  14. int main(int argc, char* argv[])
  15. {
  16.     struct input_event event = {{0}, 0};
  17.     const char* file_name = argc == 2 ? argv[1] : "/dev/input/event2";
  18.     int fd = open(file_name, O_RDWR);

  19.     if(fd > 0)
  20.     {
  21.         while(1)
  22.         {
  23.             int ret = read(fd, &event, sizeof(event));
  24.             if(ret == sizeof(event))
  25.             {
  26.                 show_event(&event);
  27.             }
  28.             else
  29.             {
  30.                 break;
  31.             }
  32.         }
  33.         close(fd);
  34.     }
  35.     return 0;
  36. }



很多人对于 如何模拟 CTRL + SPACE 感兴趣, 下面也给个例子,呵呵

点击(此处)折叠或打开

  1. void simulate_ctrl_space(int fd)
  2. {
  3.     struct input_event event;
  4.     //先发送一个 CTRL 按下去的事件。
  5.     event.type = EV_KEY;
  6.     event.value = 1;
  7.     event.code = KEY_LEFTCTRL;
  8.     gettimeofday(&event.time, 0);
  9.     write(fd, &event, sizeof(event)) ;
  10.     event.type = EV_SYN;
  11.     event.code = SYN_REPORT;
  12.     event.value = 0;
  13.     write(fd, &event, sizeof(event));
  14.     //先发送一个 SPACE 按下去的事件。
  15.     event.type = EV_KEY;
  16.     event.value = 1;
  17.     event.code = KEY_SPACE;
  18.     gettimeofday(&event.time, 0);
  19.     write(fd, &event, sizeof(event)) ;
  20.     //发送一个 释放 SPACE 的事件
  21.     memset(&event, 0, sizeof(event));
  22.     gettimeofday(&event.time, NULL);
  23.     event.type = EV_KEY;
  24.     event.code = KEY_SPACE;
  25.     event.value = 0;
  26.     write(fd, &event, sizeof(event));
  27.     event.type = EV_SYN;
  28.     event.code = SYN_REPORT;
  29.     event.value = 0;
  30.     write(fd, &event, sizeof(event));

  31.     //发送一个 释放 CTRL 的事件
  32.     memset(&event, 0, sizeof(event));
  33.     gettimeofday(&event.time, NULL);
  34.     event.type = EV_KEY;
  35.     event.code = KEY_LEFTCTRL;
  36.     event.value = 0;
  37.     write(fd, &event, sizeof(event));

  38.     event.type = EV_SYN;
  39.     event.code = SYN_REPORT;
  40.     event.value = 0;
  41.     write(fd, &event, sizeof(event));
  42. } 



浅析linux中鼠标数据读取

点击(此处)折叠或打开

  1. //我们就是去读/dev/input/mice设备节点,源码如下:
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <sys/select.h>
  6. #include <string.h>

  7. /* Mouse button bits*/
  8. #define WHEEL_UP 0x10
  9. #define WHEEL_DOWN 0x08

  10. #define BUTTON_L 0x04
  11. #define BUTTON_M 0x02
  12. #define BUTTON_R 0x01
  13. #define SCALE 3 /* default scaling factor for acceleration */
  14. #define THRESH 5 /* default threshhold for acceleration */

  15. static int xpos; /* current x position of mouse */
  16. static int ypos; /* current y position of mouse */
  17. static int minx; /* minimum allowed x position */
  18. static int maxx; /* maximum allowed x position */
  19. static int miny; /* minimum allowed y position */
  20. static int maxy; /* maximum allowed y position */
  21. static int buttons; /* current state of buttons */
  22. static int scale = SCALE; /* acceleration scale factor */
  23. static int thresh = THRESH;/* acceleration threshhold */

  24. static int mouse_update(int dx, int dy, int dz);
  25. static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp);
  26. static void mouse_setposition (int newx, int newy);
  27. static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy);

  28. int mouse_fd;

  29. int main(void)
  30. {
  31.     int dx, dy, dz;
  32.     static unsigned char imps2_param [] = {243, 200, 243, 100, 243, 80}; //,242};
  33.     // 来自vnc4的xc/programs/Xserver/hw/xfree86/input/mouse/mouse.c==>PROT_IMPS2
  34.     const char *mdev = "/dev/input/mice";

  35.     mouse_fd = open (mdev, O_RDWR); // | O_NONBLOCK);
  36.     if (mouse_fd < 0)
  37.     {
  38.         printf("[luther.gliethttp]: RW error [please use root user]: %s\n", mdev);
  39.         mouse_fd = open (mdev, O_RDONLY); // | O_NONBLOCK);
  40.         if (mouse_fd < 0)
  41.             return -1;
  42.     }
  43.     else
  44.     {
  45.         write (mouse_fd, imps2_param, sizeof (imps2_param)); // 初始化序列, 这样可以读取4个字节数据
  46.         // 0x80用来表示滚轮向上还是向下滚动
  47.         // 0xa0表示滚轮向上滚动的同时中键按下
  48.         printf("[luther.gliethttp]: imps2_param ok!\n");
  49.     }

  50.     mouse_setrange(0, 0, 1024, 768);

  51.     for (;;)
  52.     {
  53.         IMPS2_Read(&dx, &dy, &dz, &buttons);
  54.         mouse_update(dx, dy, dz);
  55.         mouse_setposition(xpos, ypos);
  56.         printf("[%04d,%04d,0x%04x]\n", xpos, ypos, buttons);
  57.     }

  58.     return 0;
  59. }

  60. static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp)
  61. {
  62.     static unsigned char buf[5];
  63.     static int buttons[7] = { 0, 1, 3, 0, 2, 0, 0}; // 1左键,2中键,3右键
  64.     static int nbytes;
  65.     int n;

  66.     while ((n = read (mouse_fd, &buf [nbytes], 4 - nbytes)))
  67.     {
  68.         if (n < 0)
  69.         {
  70.             if (errno == EINTR)
  71.                 continue;
  72.             else
  73.                 return -1;
  74.         }

  75.         nbytes += n;

  76.         if (nbytes == 4)
  77.         {
  78.             int wheel;
  79.             // printf("[luther.gliethttp]: %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
  80.             if ((buf[0] & 0xc0) != 0)
  81.             {
  82.                 buf[0] = buf[1];
  83.                 buf[1] = buf[2];
  84.                 buf[2] = buf[3];
  85.                 nbytes = 3;
  86.                 return -1;
  87.             }

  88.             /* FORM XFree86 4.0.1 */
  89.             *bp = buttons[(buf[0] & 0x07)];
  90.             *dx = (buf[0] & 0x10) ? buf[1] - 256 : buf[1];
  91.             *dy = (buf[0] & 0x20) ? -(buf[2] - 256) : -buf[2];

  92.             /* Is a wheel event? */
  93.             if ((wheel = buf[3]) != 0)
  94.             {
  95.                 if(wheel > 0x7f)
  96.                 {
  97.                     *bp |= WHEEL_UP;
  98.                 }
  99.                 else
  100.                 {
  101.                     *bp |= WHEEL_DOWN;
  102.                 }
  103.             }

  104.             *dz = 0;
  105.             nbytes = 0;
  106.             return 1;
  107.         }
  108.     }
  109.     return 0;
  110. }

  111. static int mouse_update(int dx, int dy, int dz)
  112. {
  113.     int r;
  114.     int sign;

  115.     sign = 1;
  116.     if (dx < 0)
  117.     {
  118.         sign = -1;
  119.         dx = -dx;
  120.     }
  121.     if (dx > thresh)
  122.         dx = thresh + (dx - thresh) * scale;
  123.     dx *= sign;
  124.     xpos += dx;
  125.     if( xpos < minx )
  126.         xpos = minx;
  127.     if( xpos > maxx )
  128.         xpos = maxx;

  129.     sign = 1;
  130.     if (dy < 0)
  131.     {
  132.         sign = -1;
  133.         dy = -dy;
  134.     }
  135.     if (dy > thresh)
  136.         dy = thresh + (dy - thresh) * scale;
  137.     dy *= sign;
  138.     ypos += dy;
  139.     if ( ypos < miny )
  140.         ypos = miny;
  141.     if ( ypos > maxy )
  142.         ypos = maxy;

  143.     return 1;
  144. }

  145. static void mouse_setposition (int newx, int newy)
  146. {
  147.     if (newx < minx)
  148.         newx = minx;
  149.     if (newx > maxx)
  150.         newx = maxx;
  151.     if (newy < miny)
  152.         newy = miny;
  153.     if (newy > maxy)
  154.         newy = maxy;
  155.     if (newx == xpos && newy == ypos)
  156.         return;
  157.     xpos = newx;
  158.     ypos = newy;
  159. }

  160. static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy)
  161. {
  162.     minx = newminx;
  163.     miny = newminy;
  164.     maxx = newmaxx;
  165.     maxy = newmaxy;
  166.     mouse_setposition ((newminx + newmaxx) / 2, (newminy + newmaxy) / 2);
  167. }

  168. static int mouse_getbutton (void)
  169. {
  170.     return buttons;
  171. }

  172. static void mouse_getxy (int* x, int* y)
  173. {
  174.     *x = xpos;
  175.     *y = ypos;
  176. }

 

阅读(11376) | 评论(2) | 转发(4) |
给主人留下些什么吧!~~

yioadgjn2017-12-30 16:38:29

请教下,我是通过xshell连接虚拟机的(Linux ubuntu16.04) ,通过你代码(下面是一个模拟鼠标和键盘输入的例子:)模拟键盘输入,屏幕没有信息,有没有办法输出啊,我想要的效果是这个程序可以后台运行,屏幕自动输入键盘或回车等,循环这样的操作

tjyuanxi2015-12-15 12:41:41

当前我是一个播放盒子,linux上的键盘对应的设备支持的按键很少,不满足需求,请问能够添加吗?怎么添加?