linux下如何模拟按键输入和模拟鼠标

上传人:jiups****uk12 文档编号:39420834 上传时间:2018-05-15 格式:DOC 页数:17 大小:64.50KB
返回 下载 相关 举报
linux下如何模拟按键输入和模拟鼠标_第1页
第1页 / 共17页
linux下如何模拟按键输入和模拟鼠标_第2页
第2页 / 共17页
linux下如何模拟按键输入和模拟鼠标_第3页
第3页 / 共17页
linux下如何模拟按键输入和模拟鼠标_第4页
第4页 / 共17页
linux下如何模拟按键输入和模拟鼠标_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《linux下如何模拟按键输入和模拟鼠标》由会员分享,可在线阅读,更多相关《linux下如何模拟按键输入和模拟鼠标(17页珍藏版)》请在金锄头文库上搜索。

1、linux 下如何模拟按键输入和模拟鼠标下如何模拟按键输入和模拟鼠标查看/dev/input/eventX 是什么类型的事件, cat /proc/bus/input/devices设备有着自己特殊的按键键码,我需要将一些标准的按键,比如 09,XZ 等模拟成标准按键,比如KEY_0,KEY-Z 等,所以需要用到按键模拟,具体方法就是操作/dev/input/event1 文件,向它写入个input_event 结构体就可以模拟按键的输入了。linux/input.h 中有定义,这个文件还定义了标准按键的编码等struct input_event struct timeval time; /按

2、键时间_u16 type; /类型,在下面有定义_u16 code; /要模拟成什么按键_s32 value;/是按下还是释放;code:事件的代码.如果事件的类型代码是 EV_KEY,该代码 code 为设备键盘代码.代码植 0127 为键盘上的按键代码,0x1100x116 为鼠标上按键代码,其中 0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看include/linux/input.h 文件. 如果事件的类型代码是 EV_REL,code 值表示轨迹的类型.如指示鼠标的 X轴方向 R

3、EL_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

4、_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)1。模拟按键输入/其中 0 表示释放,1 按键按下,2 表示一直按下/0 for EV_KEY for release, 1 fo

5、r keypress and 2 for autorepeat.void simulate_key(int fd,int value)struct input_event event;event.type = EV_KEY;/event.code = KEY_0;/要模拟成什么按键event.value = value;/是按下还是释放按键或者重复gettimeofday(if(write(fd,event-key.window = window-window; /一定要设置为主窗口event-key.keyval = keyval;/FIXME:一定要加上这个,要不然容易出错g_object

6、_ref(event-key.window);gdk_threads_enter();/FIXME: 记得用这个来发送事件gtk_main_do_event(event);gdk_threads_leave();gdk_event_free(event);kernel 里 input 模块input_dev 结构:struct input_dev void *private;const char *name;const char *phys;const char *uniq;struct input_id id;/* 根据各种输入信号的类型来建立类型为 unsigned long 的数组,*

7、数组的每 1bit 代表一种信号类型,* 内核中会对其进行置位或清位操作来表示时间的发生和被处理.*/unsigned long evbitNBITS(EV_MAX);unsigned long keybitNBITS(KEY_MAX);unsigned long relbitNBITS(REL_MAX);unsigned long absbitNBITS(ABS_MAX);unsigned long mscbitNBITS(MSC_MAX);unsigned long ledbitNBITS(LED_MAX);unsigned long sndbitNBITS(SND_MAX);unsign

8、ed long ffbitNBITS(FF_MAX);unsigned long swbitNBITS(SW_MAX);.;/* input_set_capability - mark device as capable of a certain event* dev: device that is capable of emitting or accepting event* type: type of the event (EV_KEY, EV_REL, etc.)* code: event code* In addition to setting up corresponding bit

9、 in appropriate capability* bitmap the function also adjusts dev-evbit.*/* 记录本设备对于哪些事件感兴趣(对其进行处理)*/void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)switch (type) case EV_KEY:_set_bit(code, dev-keybit);/比如按键,应该对哪些键值的按键进行处理(对于其它按键不予理睬)break;case EV_REL:_set_bit(cod

10、e, dev-relbit);break;case EV_ABS:_set_bit(code, dev-absbit);break;case EV_MSC:_set_bit(code, dev-mscbit);break;case EV_SW:_set_bit(code, dev-swbit);break;case EV_LED:_set_bit(code, dev-ledbit);break;case EV_SND:_set_bit(code, dev-sndbit);break;case EV_FF:_set_bit(code, dev-ffbit);break;default:print

11、k(KERN_ERR“input_set_capability: unknown type %u (code %u)n“,type, code);dump_stack();return;_set_bit(type, dev-evbit);/感觉和前面重复了(前面一经配置过一次了)EXPORT_SYMBOL(input_set_capability);static irqreturn_t gpio_keys_isr(int irq, void *dev_id)int i;struct platform_device *pdev = dev_id;struct gpio_keys_platform

12、_data *pdata = pdev-dev.platform_data;struct input_dev *input = platform_get_drvdata(pdev);for (i = 0; i nbuttons; i+) struct gpio_keys_button *button = int gpio = button-gpio;if (irq = gpio_to_irq(gpio) /判断哪个键被按了?unsigned int type = button-type ?: EV_KEY;int state = (gpio_get_value(gpio) ? 1 : 0) b

13、utton-active_low;/记录按键状态input_event(input, type, button-code, !state);/汇报输入事件input_sync(input);/等待输入事件处理完成return IRQ_HANDLED;/* input_event() - report new input event* dev: device that generated the event* type: type of the event* code: event code* value: value of the event* This function should be

14、used by drivers implementing various input devices* See also input_inject_event()*/void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)struct input_handle *handle;if (type EV_MAX | !test_bit(type, dev-evbit)/首先判断该事件类型是否有效且为该设备所接受return;add_input_randomness(type, c

15、ode, value);switch (type) case EV_SYN:switch (code) case SYN_CONFIG:if (dev-event)dev-event(dev, type, code, value);break;case SYN_REPORT:if (dev-sync)return;dev-sync = 1;break;break;case EV_KEY:/* 这里需要满足几个条件:* 1: 键值有效(不超出定义的键值的有效范围)* 2: 键值为设备所能接受(属于该设备所拥有的键值范围)* 3: 按键状态改变了*/if (code KEY_MAX | !test_bit(code, dev-keybit) | !test_bit(code, dev-key) = value)return;if (value = 2)break;change_bit(code, dev-key);/改变对应按键的状态/* 如果你希望按键未释放的时候不断汇报按键事件的话需要以下这个(在简单的 gpio_keys 驱动中不需要这个,暂时不去分析) */if (test_bit(EV_REP, dev-evbit) mod_timer(break;.if (type != EV_SYN)dev

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号