linux下如何模擬按鍵輸入和模擬鼠標【轉】

轉自:http://www.cnblogs.com/leaven/archive/2010/11/30/1891947.htmlhtml

查看/dev/input/eventX是什麼類型的事件, cat /proc/bus/input/devices 設備有着本身特殊的按鍵鍵碼,我須要將一些標準的按鍵,好比0-9,X-Z等模擬成標準按鍵,好比KEY_0,KEY-Z等,因此須要用到按鍵模擬,具體方法就是操做/dev/input/event1文件,向它寫入個input_event結構體就能夠模擬按鍵的輸入了。 linux/input.h中有定義,這個文件還定義了標準按鍵的編碼等 struct input_event { struct timeval time; //按鍵時間
__u16 type; //類型,在下面有定義
__u16 code; //要模擬成什麼按鍵
__s32 value;//是按下仍是釋放
}; 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)
1。模擬按鍵輸入 //其中0表示釋放,1按鍵按下,2表示一直按下 //0 for EV_KEY for release, 1 for 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(&event.time,0); if(write(fd,&event,sizeof(event)) < 0){ dprintk("simulate key error~~~\n"); return ; } } 2。模擬鼠標輸入(軌跡球) void simulate_mouse(int fd,char buf[4]) { int rel_x,rel_y; static struct input_event event,ev; //buf[0],buf[2],小於0則爲左移,大於0則爲右移 //buf[1],buf[3],小於0則爲下移,大於0則爲上移
dprintk("MOUSE TOUCH: x1=%d,y1=%d,x2=%d,y2=%d\n",buf[0],buf[1],buf[2],buf[3]); rel_x = (buf[0] + buf[2]) /2; rel_y = -(buf[1] + buf[3]) /2; //和咱們的鼠標是相反的方向,因此取反
event.type = EV_REL; event.code = REL_X; event.value = rel_x; gettimeofday(&event.time,0); if( write(fd,&event,sizeof(event))!=sizeof(event)) dprintk("rel_x error~~~:%s\n",strerror(errno)); event.code = REL_Y; event.value = rel_y; gettimeofday(&event.time,0); if( write(fd,&event,sizeof(event))!=sizeof(event)) dprintk("rel_y error~~~:%s\n",strerror(errno)); //必定要刷新空的
write(fd,&ev,sizeof(ev)); } 鼠標和鍵盤文件打開方法: int fd_kbd; // /dev/input/event1
int fd_mouse; //dev/input/mouse2
fd_kbd = open("/dev/input/event1",O_RDWR); if(fd_kbd<=0){ printf("error open keyboard:%s\n",strerror(errno)); return -1; } fd_mouse = open("/dev/input/event3",O_RDWR); //若是不行的話,那試試/dev/input/mice
if(fd_mouse<=0){ printf("error open mouse:%s\n",strerror(errno)); return -2; } } /dev/input/mice是鼠標的抽象,表明的是鼠標,也許是/dev/input/mouse,/dev/input/mouse1,或者空, 這個文件一直會存在。 這裏你也許會問,我怎麼知道/dev/input/eventX這些事件究竟是什麼事件阿,是鼠標仍是鍵盤或者別的, eventX表明的是全部輸入設備(input核心)的事件,好比按鍵按下,或者鼠標移動,或者遊戲遙控器等等, 在系統查看的方法是 cat /proc/bus/input/devices 就能夠看到每一個eventX是什麼設備的事件了。 PS: 在GTK中用的話,能夠參考下gtk_main_do_event這個函數 static void simulate_key(GtkWidget *window,int keyval,int press) { GdkEvent *event; GdkEventType type; if(press) type = GDK_KEY_PRESS; else type = GDK_KEY_RELEASE; event = gdk_event_new(type); //event->key.send_event = TRUE;
event->key.window = window->window; //必定要設置爲主窗口
event->key.keyval = keyval; //FIXME:必定要加上這個,要否則容易出錯
g_object_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 的數組, * 數組的每1bit表明一種信號類型, * 內核中會對其進行置位或清位操做來表示時間的發生和被處理. */ unsigned long evbit[NBITS(EV_MAX)]; unsigned long keybit[NBITS(KEY_MAX)]; unsigned long relbit[NBITS(REL_MAX)]; unsigned long absbit[NBITS(ABS_MAX)]; unsigned long mscbit[NBITS(MSC_MAX)]; unsigned long ledbit[NBITS(LED_MAX)]; unsigned long sndbit[NBITS(SND_MAX)]; unsigned long ffbit[NBITS(FF_MAX)]; unsigned long swbit[NBITS(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 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(code, 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: printk(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_data *pdata = pdev->dev.platform_data; struct input_dev *input = platform_get_drvdata(pdev); for (i = 0; i < pdata->nbuttons; i++) { struct gpio_keys_button *button = &pdata->buttons[i]; 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) ^ button->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 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, code, 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) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { dev->repeat_key = code; mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); } break; ........................................................ if (type != EV_SYN) dev->sync = 0; if (dev->grab) dev->grab->handler->event(dev->grab, type, code, value); else
        /* * 循環調用全部處理該設備的handle(event,mouse,ts,joy等), * 若是有進程打開了這些handle(進行讀寫),則調用其對應的event接口向氣彙報該輸入事件. */ list_for_each_entry(handle, &dev->h_list, d_node) if (handle->open) handle->handler->event(handle, type, code, value); } EXPORT_SYMBOL(input_event); event層對於input層報告的這個鍵盤輸入事件的處理: drivers/input/evdev.c: static struct input_handler evdev_handler = { .event = evdev_event, .connect = evdev_connect, .disconnect = evdev_disconnect, .fops =         &evdev_fops, .minor = EVDEV_MINOR_BASE, .name =         "evdev", .id_table = evdev_ids, }; Linux 有本身的 input 子系統,能夠統一管理鼠標和鍵盤事件。 基於輸入子系統 實現的 uinput 能夠方便的在用戶空間模擬鼠標和鍵盤事件。 固然,也能夠本身造輪子, 作一個字符設備接收用戶輸入,根據輸入,投遞 input 事件。 還有一種方式就是直接 往 evnent 裏寫入數據, 均可以達到控制鼠標鍵盤的功能。 本篇文章就是演示直接寫入 event 的方法。 linux/input.h中有定義,這個文件還定義了標準按鍵的編碼等 struct input_event { struct timeval time;  //按鍵時間
    __u16 type; //類型,在下面有定義
    __u16 code; //要模擬成什麼按鍵
    __s32 value;//是按下仍是釋放
}; 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) 下面是一個模擬鼠標和鍵盤輸入的例子: #include <string.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <linux/input.h> #include <linux/uinput.h> #include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h>

void simulate_key(int fd,int kval) { struct input_event event; event.type = EV_KEY; event.value = 1; event.code = kval; gettimeofday(&event.time,0); write(fd,&event,sizeof(event)) ; event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); memset(&event, 0, sizeof(event)); gettimeofday(&event.time, NULL); event.type = EV_KEY; event.code = kval; event.value = 0; write(fd, &event, sizeof(event)); event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); } void simulate_mouse(int fd) { struct input_event event; memset(&event, 0, sizeof(event)); gettimeofday(&event.time, NULL); event.type = EV_REL; event.code = REL_X; event.value = 10; write(fd, &event, sizeof(event)); event.type = EV_REL; event.code = REL_Y; event.value = 10; write(fd, &event, sizeof(event)); event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); } int main() { int fd_kbd; int fd_mouse; fd_kbd = open("/dev/input/event1",O_RDWR); if(fd_kbd<=0){ printf("error open keyboard:\n"); return -1; } fd_mouse = open("/dev/input/event2",O_RDWR); if(fd_mouse<=0){ printf("error open mouse\n"); return -2; } int i = 0; for(i=0; i< 10; i++) { simulate_key(fd_kbd, KEY_A + i); simulate_mouse(fd_mouse); sleep(1); } close(fd_kbd); } 模擬了鼠標和鍵盤的輸入事件。 關於這裏 open 哪一個 event , 能夠經過 cat /proc/bus/input/devices I: Bus=0017 Vendor=0001 Product=0001 Version=0100 N: Name="Macintosh mouse button emulation" P: Phys= S: Sysfs=/class/input/input0 U: Uniq= H: Handlers=mouse0 event0 B: EV=7 B: KEY=70000 0 0 0 0 0 0 0 0 B: REL=3 I: Bus=0011 Vendor=0001 Product=0001 Version=ab41 N: Name="AT Translated Set 2 keyboard" P: Phys=isa0060/serio0/input0 S: Sysfs=/class/input/input1 U: Uniq= H: Handlers=kbd event1 B: EV=120013 B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffe B: MSC=10 B: LED=7 I: Bus=0019 Vendor=0000 Product=0002 Version=0000 N: Name="Power Button (FF)" P: Phys=LNXPWRBN/button/input0 S: Sysfs=/class/input/input3 U: Uniq= H: Handlers=kbd event3 B: EV=3 B: KEY=100000 0 0 0 I: Bus=0019 Vendor=0000 Product=0001 Version=0000 N: Name="Power Button (CM)" P: Phys=PNP0C0C/button/input0 S: Sysfs=/class/input/input4 U: Uniq= H: Handlers=kbd event4 B: EV=3 B: KEY=100000 0 0 0 I: Bus=0003 Vendor=046d Product=c018 Version=0111 N: Name="Logitech USB Optical Mouse" P: Phys=usb-0000:00:1d.1-2/input0 S: Sysfs=/class/input/input24 U: Uniq= H: Handlers=mouse1 event2 B: EV=7 B: KEY=70000 0 0 0 0 0 0 0 0 B: REL=103 個人鼠標是 羅技 的 Logitech USB Optical Mouse, 因此 鼠標是 event2 下面是一個讀取 鼠標和鍵盤事件的例子: #include <stdio.h> #include <stdlib.h> #include <linux/input.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <errno.h>

static void show_event(struct input_event* event) { printf("%d %d %d\n", event->type, event->code, event->value); return; } int main(int argc, char* argv[]) { struct input_event event = {{0}, 0}; const char* file_name = argc == 2 ? argv[1] : "/dev/input/event2"; int fd = open(file_name, O_RDWR); if(fd > 0) { while(1) { int ret = read(fd, &event, sizeof(event)); if(ret == sizeof(event)) { show_event(&event); } else { break; } } close(fd); } return 0; } 不少人對於 如何模擬 CTRL + SPACE 感興趣, 下面也給個例子,呵呵。 void simulate_ctrl_space(int fd) { struct input_event event; //先發送一個 CTRL 按下去的事件。
        event.type = EV_KEY; event.value = 1; event.code = KEY_LEFTCTRL; gettimeofday(&event.time,0); write(fd,&event,sizeof(event)) ; event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); //先發送一個 SPACE 按下去的事件。
        event.type = EV_KEY; event.value = 1; event.code = KEY_SPACE; gettimeofday(&event.time,0); write(fd,&event,sizeof(event)) ; //發送一個 釋放 SPACE 的事件
        memset(&event, 0, sizeof(event)); gettimeofday(&event.time, NULL); event.type = EV_KEY; event.code = KEY_SPACE; event.value = 0; write(fd, &event, sizeof(event)); event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); //發送一個 釋放 CTRL 的事件
        memset(&event, 0, sizeof(event)); gettimeofday(&event.time, NULL); event.type = EV_KEY; event.code = KEY_LEFTCTRL; event.value = 0; write(fd, &event, sizeof(event)); event.type = EV_SYN; event.code = SYN_REPORT; event.value = 0; write(fd, &event, sizeof(event)); }
相關文章
相關標籤/搜索