Python simulation mouse Keyboard action to achieve the specific _python

Source: Internet
Author: User
Tags linux mint in python

I was planning to develop a fun project last month, but there was no time. This is part of the project,

Deal with this part of the basic still poor communication. First analog mouse keyboard press release action, I use X11

This library, so to understand X11 programming, and secondly, itself with C or C + + can be achieved, but because I am py

Powder, so always want to move the code into Python, so I will implement Python module, this article used cTYPES, will

Attach the Python C extension module.

1.X11 programming

First of all, a simple introduction to the X11 bar, the Internet has introduced, I will not repeat. We know that X is based on server and client

Way to provide services, we want to use its functionality, we need to communicate with the server. Use

Display *xopendisplay (char *display_name) gets a handle pointer to the display type.

Display_name can be display environment variables, with echo $DISPLAY output is: 0 (this is my Linux mint lose

Out of). If Display_name is a null interface, the value saved by the environment variable is used by default. X11 programming commonly used in several heads

File:

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

I use the #include <X11/Xlib.h> and #include <x11/extensions/xtest.h>

XTest.h has the interface we need to simulate mouse and keyboard xtestfakebuttonevent, xtestfakemotionevent and

Xtestfakekeyevent. To find out more information, you only need to add a letter name to the end of the man to obtain.

such as Xtestfakemotionevent interface:

Copy Code code as follows:

int xtestfakemotionevent (display, Screen_number, X, Y,delay);

Display *display; This value is obtained from the Xopendisplay
int screen_number; Let it be-1 to represent the current screen
int x, y; Screen location
unsigned long delay; Delay milliseconds so that it is currenttime without delay

Finally we want to close the display handle: Xclosedisplay (display *display).

Interface implementations are as follows:

Copy Code code as follows:

#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) {//keyboard press
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)//mouse movement
{
if (0==xtestfakemotionevent (dsp,-1,x,y,currenttime))
{
printf ("Cannot move!\n");
return-1;
}
return 0;
}
int ButtonPress (Display *dsp,int type)//mouse press, type=1 for left ARROW, 3 is right, 2 is middle key
{
if (0==xtestfakebuttonevent (dsp,type,1,currenttime))
{
printf ("Press failed\n");
return-1;
}
return 0;
}
int buttonrelease (Display *dsp,int type)//mouse release
{
if (0==xtestfakebuttonevent (dsp,type,0,currenttime))
{
printf ("Release failed\n");
return-1;
}
return 0;
}
int Releasekey (Display *dsp,int s) {//keyboard 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 () {//test will output C before the cursor at the end of the program
Display *dsp=dspopen ();
Presskey (DSP, ' C ');
Releasekey (DSP, ' C ');
Dspclose (DSP);
return 0;
//}

The main function commented out above can be used as a test, OK, let's save the above code as DISPLAY.C

Compiled into a shared library, you need X11 and XTST libraries.

Copy Code code as follows:

Gcc-fpic-shared-o libdisplay.so DISPLAY.C-LX11-LXTST

Libdisplay.so is generated after compilation. Now we ctypes the module using this dynamic shared library.

2.ctypes Simple Introduction and use

We know that the type in Python is not the same as the type in C, it should be said that there is no the same, take int to say,

Python also deals with it as a pyobject type. Then we need to use the interface provided by CType to do the type

Transformation. See: Http://docs.python.org/2/library/ctypes.html#fundamental-data-types

This link has Zhang Tu detailed presentation type conversion corresponding to the interface. Here's how to do it.

We load the storage through the Cdll () interface:

Copy Code code as follows:

Lc=cdll ("./libdisplay.so")

You can then use the interfaces provided in the library, but the return value of the Dspopen () interface above is a pointer to the display type.

So we need to use c_void_p () to convert:

Copy Code code as follows:

D=c_void_p (Lc.dspopen ())

You can then use D to do the processing, the code is as follows:

Copy Code code as follows:

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)

The above code opens the right-click menu at the mouse pointer after 5 seconds.

Use cTYPES to write a library that uses C to talk so much. The C code will later be written as a Python C extension to share it.

Using the above analog keyboard and mouse interface can do some interesting things ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.