python之類比滑鼠鍵盤動作具體實現_python

來源:互聯網
上載者:User

上個月就打算開發個還算好玩的項目,但是一直沒時間。這篇是此項目用到的一部分,

處理好此部分基本還差通訊等方面的了。首先類比滑鼠鍵盤按下釋放的動作,本人利用X11

這個庫,所以要瞭解X11編程;其次,本身用c或者c++就可以實現了,但是由於本人是py

粉,所以總想把代碼搬進python,所以本人就要實現python模組,本篇用的ctypes,以後會

把python的c擴充模組附上來的。

  1.X11編程

    首先簡單的介紹一下X11吧,網上有介紹,本人就不重複了。我們知道X是以server與client

的方式提供服務的,我們想要使用其功能,我們就需要與server通訊。使用

Display *XOpenDisplay(char *display_name)獲得一個Display類型的控制代碼指標就可以了。

display_name可以是DISPLAY環境變數,用echo $DISPLAY輸出是:0(這是本人linux mint輸

出的)。如果display_name為NULL介面預設使用環境變數儲存的值。X11編程常用的幾個頭

檔案:

  #include <X11/Xlib.h>
  #include <X11/Xutil.h>
  #include <X11/Xos.h>

本人用到的是#include <X11/Xlib.h>和 #include <X11/extensions/XTest.h>.

XTest.h有我們類比滑鼠和鍵盤需要的介面XTestFakeButtonEvent、 XTestFakeMotionEvent和

XTestFakeKeyEvent。想瞭解更多資訊只需要在終端上man加函數名即可獲得。

比如XTestFakeMotionEvent介面:

複製代碼 代碼如下:

int XTestFakeMotionEvent(display, screen_number, x, y,delay);

Display *display;  //此值就是從XOpenDisplay獲得
int screen_number; //讓其為-1即可表示當前的螢幕
int x, y;          //螢幕位置
unsigned long delay; //延遲毫秒,讓其為CurrentTime表示不延遲

最後我們要關閉Display控制代碼:XCloseDisplay(Display *display)。

介面實現如下:

複製代碼 代碼如下:

#include <stdio.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlib.h>
Display *dspopen(){  

    Display *dsp = XOpenDisplay(NULL);
    if(!dsp) {
        printf("open display failed\n");
        return NULL;
    }
    return dsp;
}
int presskey(Display *dsp,int s){  //鍵盤按
    if(dsp==NULL)
        return -1;
//    KeySym keysym=XStringToKeysym(s);
    KeyCode key=XKeysymToKeycode(dsp,s);
    if(key==NoSymbol)
        return -1;
    XTestFakeKeyEvent(dsp,key,1,CurrentTime);
    XFlush(dsp);
    return 0;
}
int move(Display *dsp,int x,int y) //滑鼠移動
{
    if(0==XTestFakeMotionEvent(dsp,-1,x,y,CurrentTime))
    {
        printf("Cannot move!\n");
        return -1;
    }
    return 0;
}
int buttonpress(Display *dsp,int type) //滑鼠按,type=1表示左鍵,3是右鍵,2是中鍵
{
    if(0==XTestFakeButtonEvent(dsp,type,1,CurrentTime))
    {
        printf("press failed\n");
        return -1;
    }
    return 0;
}
int buttonrelease(Display *dsp,int type) //滑鼠釋放
{
    if(0==XTestFakeButtonEvent(dsp,type,0,CurrentTime))
    {
        printf("release failed\n");
        return -1;
    }
    return 0;
}
int releasekey(Display *dsp,int s){ //鍵盤release
    if(dsp==NULL)
        return -1;
//    KeySym keysym=XStringToKeysym(s);
    KeyCode key=XKeysymToKeycode(dsp,s);
    if(key==NoSymbol)
        return -1;
    XTestFakeKeyEvent(dsp,key,0,CurrentTime);
    XFlush(dsp);
    return 0;
}
void dspclose( Display *dsp ){
    if(dsp!=NULL){
        XCloseDisplay(dsp);

    }
}
//int main(){     //測試用的會在程式結束後,在游標前輸出c
//    Display *dsp=dspopen();
//    presskey(dsp,'c');
//    releasekey(dsp,'c');
//    dspclose(dsp);
//    return 0;
//}

上面注釋掉的main函數可以作為測試用的,好了,我們把上面的代碼儲存為display.c

編譯成一個共用庫,需要X11和Xtst庫。

複製代碼 代碼如下:

gcc -fPIC -shared -o libdisplay.so display.c -lX11 -lXtst

編譯後會產生libdisplay.so 。現在我們ctypes模組使用這個動態共用程式庫。

     2.ctypes簡單介紹和使用

    我們知道python中的類型與c中類型是不一樣的,應該說沒有一樣的,就拿int想來說,

python也是把它看作PyObject類型來處理的。那麼我們需要使用ctype提供的介面做類型的

轉換。見:http://docs.python.org/2/library/ctypes.html#fundamental-data-types

此連結有張圖詳細的展現類型轉換對應的介面。下面示範具體的操作吧。

我們通過CDLL()介面載入庫:

複製代碼 代碼如下:

lc=CDLL("./libdisplay.so")

然後就可以使用庫中提供的介面了,但是上面dspopen()介面的傳回值是Display類型的指標,

所以我們需要用c_void_p()轉換一下:

複製代碼 代碼如下:

d=c_void_p(lc.dspopen())

之後就可以用d做處理了,代碼如下:

複製代碼 代碼如下:

from ctypes import *
import time
class MOUSE:
    LEFT=1
    MiDDLE=2
    RIGHT=3
lc=CDLL("./libdisplay.so")
d=c_void_p(lc.dspopen())
time.sleep(5);
lc.buttonpress(d,c_int(MOUSE.RIGHT))
lc.buttonrelease(d,c_int(MOUSE.RIGHT))
lc.dspclose(d)

上面的代碼會在5秒後在滑鼠指標處開啟右鍵菜單。

利用ctypes使用c編寫的庫就講這麼多吧。以後會把c代碼的部分寫成python的c擴充再分享吧。

利用上面的類比鍵盤和滑鼠的介面可以做一些有意思的事情....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.