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

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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

2、有定義_u16 code; /要模擬成什么按鍵_s32 value;/是按下還是釋放;code:事件的代碼.如果事件的類型代碼是EV_KEY,該代碼code為設(shè)備鍵盤(pán)代碼.代碼植0127為鍵盤(pán)上的按鍵代碼,0x1100x116 為鼠標(biāo)上按鍵代碼,其中0x110(BTN_ LEFT)為鼠標(biāo)左鍵,0x111(BTN_RIGHT)為鼠標(biāo)右鍵,0x112(BTN_ MIDDLE)為鼠標(biāo)中鍵.其它代碼含義請(qǐng)參看include/linux/input.h文件. 如果事件的類型代碼是EV_REL,code值表示軌跡的類型.如指示鼠標(biāo)的X軸方向REL_X(代碼為0x00),指示鼠標(biāo)的Y軸方向REL_Y(代碼為

3、0x01),指示鼠標(biāo)中輪子方向REL_WHEEL(代碼為0x08).type: EV_KEY,鍵盤(pán)EV_REL,相對(duì)坐標(biāo)EV_ABS,絕對(duì)坐標(biāo)value:事件的值.如果事件的類型代碼是EV_KEY,當(dāng)按鍵按下時(shí)值為1,松開(kāi)時(shí)值為0;如果事件的類型代碼是EV_ REL,value的正數(shù)值和負(fù)數(shù)值分別代表兩個(gè)不同方向的值./* Event types*/#define EV_SYN 0x00#define EV_KEY 0x01 /按鍵#define EV_REL 0x02 /相對(duì)坐標(biāo)(軌跡球)#define EV_ABS 0x03 /絕對(duì)坐標(biāo)#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;/是按下還是釋放按鍵或者重復(fù)gettimeofday(&event.time,0);if(write(fd,&event,sizeof(event) < 0)dprintk("simulate key errorn");return ;2。模擬鼠標(biāo)輸入(軌跡球)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; /和我們的鼠標(biāo)是相反的方向,所以取反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);鼠標(biāo)和鍵盤(pán)文件打開(kāi)方法: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是鼠標(biāo)的抽象,代表的是鼠標(biāo),也許是/dev/input/mouse,/dev/input/mouse1,或者空,這個(gè)文件一直會(huì)存在。這里你也許會(huì)問(wèn),我怎么知道/dev/input/eventX這些事件到底是什么事件阿,是鼠標(biāo)還是鍵盤(pán)或者別的,eventX代表的是所有輸入設(shè)備(input核心)的

10、事件,比如按鍵按下,或者鼠標(biāo)移動(dòng),或者游戲遙控器等等,在系統(tǒng)查看的方法是 cat /proc/bus/input/devices 就可以看到每個(gè)eventX是什么設(shè)備的事件了。PS: 在GTK中用的話,可以參考下gtk_main_do_event這個(gè)函數(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; /一定要設(shè)置為主窗口event->key.keyval = keyval;/FIXME:一定要加上這個(gè),要不然容易出錯(cuò)g_object_ref(event->key.window);gdk_threads_enter();/FIXME: 記得用這個(gè)來(lái)發(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ù)各種輸入信號(hào)的類型來(lái)建立類型為unsigned long 的數(shù)組,  

13、  * 數(shù)組的每1bit代表一種信號(hào)類型,    * 內(nèi)核中會(huì)對(duì)其進(jìn)行置位或清位操作來(lái)表示時(shí)間的發(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.*/* 記錄本設(shè)備對(duì)于哪些事件感興趣(對(duì)其進(jìn)行處理)*/void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)    switch (type)     case EV_KEY:        _set_bit(code, dev->keybit);/比如按鍵,應(yīng)該對(duì)哪些鍵值的按鍵進(jìn)行處理(對(duì)于其它按鍵不予理睬)  

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);/感覺(jué)和前面重復(fù)了(前面一經(jīng)配置過(guò)一次了)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) /判斷哪個(gè)鍵被按了?&#

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);/匯報(bào)輸入事件 

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)/首先判斷該事件類型是否有效且為該設(shè)備所接受        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:            /*            * 這里需要滿足幾個(gè)條件: 

35、           * 1: 鍵值有效(不超出定義的鍵值的有效范圍)            * 2: 鍵值為設(shè)備所能接受(屬于該設(shè)備所擁有的鍵值范圍)            * 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);/改變對(duì)應(yīng)按鍵的狀態(tài)            /* 如果你希望按鍵未釋放的時(shí)候不斷匯報(bào)按鍵事件的話需要以下這個(gè)(在簡(jiǎn)單的gpio_keys驅(qū)動(dòng)中不需要這個(gè),暫時(shí)不去分析) */

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)用所有處理該設(shè)備的handle(event,mouse,ts,joy等),        * 如果有進(jìn)程打開(kāi)了這些handle(進(jìn)行讀寫(xiě)),則調(diào)用其對(duì)應(yīng)的event接口向氣匯報(bào)該輸入事件.        */        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層對(duì)于input層報(bào)告的這個(gè)鍵盤(pán)輸入事件的處理: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)一管理鼠標(biāo)和鍵盤(pán)事件?;谳斎胱酉到y(tǒng) 實(shí)現(xiàn)的 uinput 可以方便的在用戶空間模擬鼠標(biāo)和鍵盤(pán)事件。當(dāng)然,也可以自己造輪子, 做一個(gè)字符設(shè)備接收用戶輸入,根據(jù)輸入,投遞 input 事件。還有一種方式就是直接 往 evnent 里寫(xiě)入數(shù)據(jù), 都可以達(dá)到控制鼠標(biāo)鍵盤(pán)的功能。本篇文章就是演

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

47、0x1100x116 為鼠標(biāo)上按鍵代碼,其中0x110(BTN_ LEFT)為鼠標(biāo)左鍵,0x111(BTN_RIGHT)為鼠標(biāo)右鍵,0x112(BTN_ MIDDLE)為鼠標(biāo)中鍵.其它代碼含義請(qǐng)參看include/linux /input.h文件. 如果事件的類型代碼是EV_REL,code值表示軌跡的類型.如指示鼠標(biāo)的X軸方向 REL_X (代碼為0x00),指示鼠標(biāo)的Y軸方向REL_Y(代碼為0x01),指示鼠標(biāo)中輪子方向REL_WHEEL(代碼為0x08).type:EV_KEY,鍵盤(pán)EV_REL,相對(duì)坐標(biāo)EV_ABS,絕對(duì)坐標(biāo)value:事件的值.如果事件的類型代碼是EV_KEY,當(dāng)按

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

49、        0x02 /相對(duì)坐標(biāo)(軌跡球)#define EV_ABS            0x03 /絕對(duì)坐標(biāo)#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)下面是一個(gè)模擬鼠標(biāo)和鍵盤(pán)輸入的例子:#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);模擬了鼠標(biāo)和鍵盤(pán)的輸入事件。關(guān)于這里 open 哪個(gè) event , 可以通過(guò) 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我的鼠標(biāo)是 羅技 的 Logitech USB Optical Mouse, 所以 鼠標(biāo)是 event2下面是一個(gè)讀取 鼠標(biāo)和鍵盤(pán)事件的例子:#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;很多人對(duì)于 如何模擬 CTRL + SPACE 感興趣, 下面也給個(gè)例子,呵呵。void simulate_ctrl_space(int fd)    &

溫馨提示

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

評(píng)論

0/150

提交評(píng)論