linux下如何模擬按鍵輸入和模擬鼠標_第1頁
linux下如何模擬按鍵輸入和模擬鼠標_第2頁
linux下如何模擬按鍵輸入和模擬鼠標_第3頁
linux下如何模擬按鍵輸入和模擬鼠標_第4頁
linux下如何模擬按鍵輸入和模擬鼠標_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、.linux下如何模擬按鍵輸入和模擬鼠標查看/dev/input/eventX是什么類型的事件, cat /proc/bus/input/devices設備有著自己特殊的按鍵鍵碼,我需要將一些標準的按鍵,比如09,XZ等模擬成標準按鍵,比如KEY_0,KEY-Z等,所以需要用到按鍵模擬,具體方法就是操作/dev/input/event1文件,向它寫入個input_event結(jié)構(gòu)體就可以模擬按鍵的輸入了。linux/input.h中有定義,這個文件還定義了標準按鍵的編碼等struct input_event struct timeval time; /按鍵時間_u16 type; /類型,在下面

2、有定義_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軸方向REL_X(代碼為0x00),指示鼠標的Y軸方向REL_Y(代碼為

3、0x01),指示鼠標中輪子方向REL_WHEEL(代碼為0x08).type: EV_KEY,鍵盤EV_REL,相對坐標EV_ABS,絕對坐標value:事件的值.如果事件的類型代碼是EV_KEY,當按鍵按下時值為1,松開時值為0;如果事件的類型代碼是EV_ REL,value的正數(shù)值和負數(shù)值分別代表兩個不同方向的值./* Event types*/#define EV_SYN 0x00#define EV_KEY 0x01 /按鍵#define EV_REL 0x02 /相對坐標(軌跡球)#define EV_ABS 0x03 /絕對坐標#define EV_MSC 0x04 /其他#def

4、ine 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_

5、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 errorn");return ;2。模擬鼠標輸入(軌跡球)void simulate_mouse(int fd,char b

6、uf4)int rel_x,rel_y;static struct input_event event,ev;/buf0,buf2,小于0則為左移,大于0則為右移/buf1,buf3,小于0則為下移,大于0則為上移dprintk("MOUSE TOUCH: x1=%d,y1=%d,x2=%d,y2=%dn",buf0,buf1,buf2,buf3);rel_x = (buf0 + buf2) /2;rel_y = -(buf1 + buf3) /2; /和我們的鼠標是相反的方向,所以取反event.type = EV_REL;event.code = REL_X;event

7、.value = rel_x;gettimeofday(&event.time,0);if( write(fd,&event,sizeof(event)!=sizeof(event)dprintk("rel_x error:%sn",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:

8、%sn",strerror(errno);/一定要刷新空的write(fd,&ev,sizeof(ev);鼠標和鍵盤文件打開方法:int fd_kbd; / /dev/input/event1int fd_mouse; /dev/input/mouse2fd_kbd = open("/dev/input/event1",O_RDWR);if(fd_kbd<=0)printf("error open keyboard:%sn",strerror(errno);return -1;fd_mouse = open("/dev/

9、input/event3",O_RDWR); /如果不行的話,那試試/dev/input/miceif(fd_mouse<=0)printf("error open mouse:%sn",strerror(errno);return -2;/dev/input/mice是鼠標的抽象,代表的是鼠標,也許是/dev/input/mouse,/dev/input/mouse1,或者空,這個文件一直會存在。這里你也許會問,我怎么知道/dev/input/eventX這些事件到底是什么事件阿,是鼠標還是鍵盤或者別的,eventX代表的是所有輸入設備(input核心)的

10、事件,比如按鍵按下,或者鼠標移動,或者游戲遙控器等等,在系統(tǒng)查看的方法是 cat /proc/bus/input/devices 就可以看到每個eventX是什么設備的事件了。PS: 在GTK中用的話,可以參考下gtk_main_do_event這個函數(shù)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_even

11、t_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: 記得用這個來發(fā)送事件gtk_main_do_event(event);gdk_threads_leave();gdk_event_free(event);kernel里inpu

12、t模塊input_dev結(jié)構(gòu):struct input_dev     void *private;    const char *name;    const char *phys;    const char *uniq;    struct input_id id;    /*    * 根據(jù)各種輸入信號的類型來建立類型為unsigned long 的數(shù)組,  

13、  * 數(shù)組的每1bit代表一種信號類型,    * 內(nèi)核中會對其進行置位或清位操作來表示時間的發(fā)生和被處理.    */    unsigned long evbitNBITS(EV_MAX);    unsigned long keybitNBITS(KEY_MAX);    unsigned long relbitNBITS(REL_MAX);    unsigned long absbitNBITS

14、(ABS_MAX);    unsigned long mscbitNBITS(MSC_MAX);    unsigned long ledbitNBITS(LED_MAX);    unsigned long sndbitNBITS(SND_MAX);    unsigned long ffbitNBITS(FF_MAX);    unsigned long swbitNBITS(SW_MAX);    .;/*

15、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-

16、>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);/比如按鍵,應該對哪些鍵值的按鍵進行處理(對于其它按鍵不予理睬)  

17、;      break;    case EV_REL:        _set_bit(code, dev->relbit);        break;    case EV_ABS:        _set_bit(code, dev->absbit);    

18、0;   break;    case EV_MSC:        _set_bit(code, dev->mscbit);        break;    case EV_SW:        _set_bit(code, dev->swbit);       

19、 break;    case EV_LED:        _set_bit(code, dev->ledbit);        break;    case EV_SND:        _set_bit(code, dev->sndbit);        break; 

20、;   case EV_FF:        _set_bit(code, dev->ffbit);        break;    default:        printk(KERN_ERR            "input_set_capability

21、: unknown type %u (code %u)n",            type, code);        dump_stack();        return;        _set_bit(type, dev->evbit);/感覺和前面重復了(前面一經(jīng)配置過一次了)EXPORT_SYMB

22、OL(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-&g

23、t;dev.platform_data;        struct input_dev *input = platform_get_drvdata(pdev);        for (i = 0; i < pdata->nbuttons; i+)                 struct

24、gpio_keys_button *button = &pdata->buttonsi;                int gpio = button->gpio;                if (irq = gpio_to_irq(gpio) /判斷哪個鍵被按了?&#

25、160;                       unsigned int type = button->type ?: EV_KEY;                   

26、60;    int state = (gpio_get_value(gpio) ? 1 : 0) button->active_low;/記錄按鍵狀態(tài)                        input_event(input, type, button->code, !state);/匯報輸入事件 

27、0;                      input_sync(input);/等待輸入事件處理完成                         &#

28、160;      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_inje

29、ct_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;  &#

30、160; add_input_randomness(type, code, value);    switch (type)         case EV_SYN:            switch (code)                 case SYN_CONFIG: 

31、60;                  if (dev->event)                        dev->event(dev, type, code, value);         

32、           break;                case SYN_REPORT:                    if (dev->sync)        &

33、#160;               return;                    dev->sync = 1;                    break; 

34、;                       break;        case EV_KEY:            /*            * 這里需要滿足幾個條件: 

35、           * 1: 鍵值有效(不超出定義的鍵值的有效范圍)            * 2: 鍵值為設備所能接受(屬于該設備所擁有的鍵值范圍)            * 3: 按鍵狀態(tài)改變了            */  

36、;          if (code > KEY_MAX | !test_bit(code, dev->keybit) | !test_bit(code, dev->key) = value)                return;            if (value = 2) 

37、;               break;            change_bit(code, dev->key);/改變對應按鍵的狀態(tài)            /* 如果你希望按鍵未釋放的時候不斷匯報按鍵事件的話需要以下這個(在簡單的gpio_keys驅(qū)動中不需要這個,暫時不去分析) */

38、0;           if (test_bit(EV_REP, dev->evbit) && dev->repREP_PERIOD && dev->repREP_DELAY && dev->timer.data && value)                 dev->repeat_key = c

39、ode;                mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->repREP_DELAY);                        break;.    i

40、f (type != EV_SYN)        dev->sync = 0;    if (dev->grab)        dev->grab->handler->event(dev->grab, type, code, value);    else        /*    

41、60;   * 循環(huán)調(diào)用所有處理該設備的handle(event,mouse,ts,joy等),        * 如果有進程打開了這些handle(進行讀寫),則調(diào)用其對應的event接口向氣匯報該輸入事件.        */        list_for_each_entry(handle, &dev->h_list, d_node)    &#

42、160;       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_

43、handler =         .event =        evdev_event,        .connect =      evdev_connect,        .disconnect =   evdev_disconnect,

44、        .fops =         &evdev_fops,        .minor =        EVDEV_MINOR_BASE,        .name =    

45、     "evdev",        .id_table =     evdev_ids,;Linux 有自己的 input 子系統(tǒng),可以統(tǒng)一管理鼠標和鍵盤事件?;谳斎胱酉到y(tǒng) 實現(xiàn)的 uinput 可以方便的在用戶空間模擬鼠標和鍵盤事件。當然,也可以自己造輪子, 做一個字符設備接收用戶輸入,根據(jù)輸入,投遞 input 事件。還有一種方式就是直接 往 evnent 里寫入數(shù)據(jù), 都可以達到控制鼠標鍵盤的功能。本篇文章就是演

46、示直接寫入 event 的方法。 linux/input.h中有定義,這個文件還定義了標準按鍵的編碼等struct input_event     struct timeval time;  /按鍵時間    _u16 type; /類型,在下面有定義    _u16 code; /要模擬成什么按鍵    _s32 value;/是按下還是釋放;code:事件的代碼.如果事件的類型代碼是EV_KEY,該代碼code為設備鍵盤代碼.代碼植0127為鍵盤上的按鍵代碼,

47、0x1100x116 為鼠標上按鍵代碼,其中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,當按

48、鍵按下時值為1,松開時值為0;如果事件的類型代碼是EV_ REL,value的正數(shù)值和負數(shù)值分別代表兩個不同方向的值./* * Event types */#define EV_SYN            0x00#define EV_KEY            0x01 /按鍵#define EV_REL    

49、        0x02 /相對坐標(軌跡球)#define EV_ABS            0x03 /絕對坐標#define EV_MSC            0x04 /其他#define EV_SW        &

50、#160;   0x05#define EV_LED            0x11 /LED#define EV_SND            0x12/聲音#define EV_REP            0x14/repeat#define

51、EV_FF            0x15#define EV_PWR            0x16#define EV_FF_STATUS        0x17#define EV_MAX         

52、0;  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/u

53、input.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 = k

54、val;    gettimeofday(&event.time,0);    write(fd,&event,sizeof(event) ;        event.type = EV_SYN;        event.code = SYN_REPORT;        event.value = 0;&#

55、160;       write(fd, &event, sizeof(event);           memset(&event, 0, sizeof(event);        gettimeofday(&event.time, NULL);        event.typ

56、e = EV_KEY;        event.code = kval;        event.value = 0;        write(fd, &event, sizeof(event);        event.type = EV_SYN;    

57、60;   event.code = SYN_REPORT;        event.value = 0;        write(fd, &event, sizeof(event);void simulate_mouse(int fd)    struct input_event event;        memset(&

58、amp;event, 0, sizeof(event);        gettimeofday(&event.time, NULL);        event.type = EV_REL;        event.code = REL_X;        event.value = 10; &#

59、160;      write(fd, &event, sizeof(event);        event.type = EV_REL;        event.code = REL_Y;        event.value = 10;        w

60、rite(fd, &event, sizeof(event);        event.type = EV_SYN;        event.code = SYN_REPORT;        event.value = 0;        write(fd, &event, sizeof(even

61、t);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");     &#

62、160;  return -1;        fd_mouse = open("/dev/input/event2",O_RDWR);     if(fd_mouse<=0)        printf("error open mousen");        return -2;  

63、60;     int i = 0;    for(i=0; i< 10; i+)            simulate_key(fd_kbd, KEY_A + i);        simulate_mouse(fd_mouse);        sleep(1);  

64、      close(fd_kbd);模擬了鼠標和鍵盤的輸入事件。關于這里 open 哪個 event , 可以通過 cat /proc/bus/input/devices I: Bus=0017 Vendor=0001 Product=0001 Version=0100N: Name="Macintosh mouse button emulation"P: Phys=S: Sysfs=/class/input/input0U: Uniq=H: Handlers=mouse0 event0 B: EV=7B: KEY=70000 0

65、 0 0 0 0 0 0 0B: REL=3I: Bus=0011 Vendor=0001 Product=0001 Version=ab41N: Name="AT Translated Set 2 keyboard"P: Phys=isa0060/serio0/input0S: Sysfs=/class/input/input1U: Uniq=H: Handlers=kbd event1 B: EV=120013B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffeB: MSC=10B:

66、LED=7I: Bus=0019 Vendor=0000 Product=0002 Version=0000N: Name="Power Button (FF)"P: Phys=LNXPWRBN/button/input0S: Sysfs=/class/input/input3U: Uniq=H: Handlers=kbd event3 B: EV=3B: KEY=100000 0 0 0I: Bus=0019 Vendor=0000 Product=0001 Version=0000N: Name="Power Button (CM)"P: Phys=

67、PNP0C0C/button/input0S: Sysfs=/class/input/input4U: Uniq=H: Handlers=kbd event4 B: EV=3B: KEY=100000 0 0 0I: Bus=0003 Vendor=046d Product=c018 Version=0111N: Name="Logitech USB Optical Mouse"P: Phys=usb-0000:00:1d.1-2/input0S: Sysfs=/class/input/input24U: Uniq=H: Handlers=mouse1 event2 B:

68、EV=7B: KEY=70000 0 0 0 0 0 0 0 0B: 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>#inc

69、lude <errno.h>static void show_event(struct input_event* event)        printf("%d %d %dn", event->type, event->code, event->value);        return;int main(int argc, char* argv)     

70、;   struct input_event event = 0, 0;        const char* file_name = argc = 2 ? argv1 : "/dev/input/event2"        int fd = open(file_name, O_RDWR);        if(fd > 0) &

71、#160;                      while(1)                            &#

72、160;           int ret = read(fd, &event, sizeof(event);                        if(ret = sizeof(event)     

73、;                                                   sho

74、w_event(&event);                                                el

75、se                                                  

76、60;     break;                                                        close(fd);                return 0;很多人對于 如何模擬 CTRL + SPACE 感興趣, 下面也給個例子,呵呵。void simulate_ctrl_space(int fd)    &

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論