last month is going to develop a moderately fun project, but haven't got the time. This article is a part of the project used,

to deal with this part basic return bad communication, etc. First to simulate mouse and keyboard press release of the action, I use X11

this library, so to understand the X11 programming; Second, itself in c or c + + can be realized, but because of I am py

powder, so the total want to move in python code, so I will realize the python module, this with ctypes, will

the attachment of python module c extensions.

1.

X11 programming

      First to introduce a simple X11, there are introduction, I will not repeat. We know the server and client X is

the way of providing services, we want to use its function, we need to communicate with the server. Using the

Display * XOpenDisplay (char * display_name) to obtain a handle to the Display type pointer.

display_name can be DISPLAY environment variable, with the echo $DISPLAY output is: 0 (this is my Linux mint lose

a). If display_name is NULL interfaces using environment variable to hold the values by default. X11 programming the commonly used several head

file:

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

I used was #include <X11/Xlib. H> And #include <X11/extensions/XTest. H> .

XTest. H is what we need simulate mouse and keyboard interface XTestFakeButtonEvent, XTestFakeMotionEvent and

XTestFakeKeyEvent. Want to know more information you just need to man on the terminal function name.

like XTestFakeMotionEvent interface:


code is as follows:


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

Display * Display;  //this value is obtained from XOpenDisplay
int screen_number;//let it to 1 can be said that the current screen
int x, y;                  //screen position
unsigned long delay;//delay in milliseconds, for their CurrentTime said not delay

the end we must close the handle to the Display: XCloseDisplay * Display (Display).

interface implementation is as follows:


code is as follows:


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

      The Display * DSP=XOpenDisplay (NULL);
      If (! DSP) {
              Printf (" open display failed \ n ");
              Return null;
      }
      Return DSP;

} int presskey (Display * DSP, int s) { //keyboard press
      If (DSP==NULL)
              The return - 1;
//      KeySym KeySym=XStringToKeysym (s);
      KeyCode key=XKeysymToKeycode (DSP, s);
      If (key==NoSymbol)
              The return - 1;
      XTestFakeKeyEvent (DSP, key, 1, CurrentTime);
      XFlush (DSP);
      Return 0;

} int move (Display * DSP, int x, int y)//mouse
{
      If (0==XTestFakeMotionEvent (DSP, 1, x, y, CurrentTime))
      {
              Printf (" always move! \ n ");
              The return - 1;
      }
      Return 0;

} int buttonpress (DSP Display * and an int type)//mouse press, click type=1, 3 is right, 2 is the key in the
{
      If (0==XTestFakeButtonEvent (DSP, type, 1, CurrentTime))
      {
              Printf (" press failed \ n ");
              The return - 1;
      }
      Return 0;

} int buttonrelease (DSP Display * and an int type)//mouse release
{
      If (0==XTestFakeButtonEvent (DSP, type, 0, CurrentTime))
      {
              Printf (" release failed \ n ");
              The return - 1;
      }
      Return 0;

} int releasekey (Display * DSP, int s) {//keyboard release
      If (DSP==NULL)
              The return - 1;
//      KeySym KeySym=XStringToKeysym (s);
      KeyCode key=XKeysymToKeycode (DSP, s);
      If (key==NoSymbol)
              The return - 1;
      XTestFakeKeyEvent (DSP, key, 0, CurrentTime);
      XFlush (DSP);
      Return 0;

} void dspclose (DSP) Display * {
      If (DSP!=NULL) {
              XCloseDisplay (DSP);

      }

}//int main () {       //test will be at the end of the program, the output before the cursor c
//      The Display * DSP=dspopen ();
//      Presskey (DSP, 'c');
//      Releasekey (DSP, 'c');
//      Dspclose (DSP);
//      Return 0;
//}

the above comment out of the main function can be used as a test, well, we keep the above code to display. C

compiled into a Shared library, need X11 and Xtst library.


code is as follows:


GCC -fpic -shared -o libdisplay. So the display. The c - lX11 - lXtst

the compiled generates libdisplay. So. Now we ctypes module USES the dynamic Shared libraries.

        2. Ctypes simple introduction and use of the

      We know that in python type and c type is different, it should be said that is not the same, take int think,

python also see it as a PyObject types to handle. So we need to use the ctype provided interfaces do type

transformation. http://docs.python.org/2/library/ctypes.html#fundamental-data-types

this link has a picture show type conversion of the corresponding interface in detail. The following demonstrates the specific operation.

we through CDLL () interface library:


code is as follows:


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

then you can use the library provided interfaces, but the above dspopen (), the return value is the Display type of the interface pointer,

so we need to use c_void_p () :


code is as follows:


d=c_void_p (lc) dspopen ())

can do processing with d, after the code is as follows:


code is as follows:


the from ctypes import *
the 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)

the code above will be in after 5 seconds the mouse pointer to open the right-click menu.

use ctypes use library written in c, speak so much. Will be part of the c code written in python c extensions of the share.

using the simulation above the keyboard and mouse interface can do some interesting things...


This concludes the body part