Python-specific implementation of simulating mouse and keyboard movements

Source: Internet
Author: User
Tags linux mint

I was planning to develop a fun project last month, but I have never had time. This is part of the project,

This part is basically poor in communication. First, simulate the mouse and keyboard press the release action, I use X11

This library, so you need to understand X11 programming; secondly, you can use c or c ++ to implement it, but since I am a py

So I always want to move the code into python, so I need to implement the python module. The ctypes used in this article will

Attaches the c extension module of python.

1. X11 Programming

First of all, let's give a brief introduction to X11. I will not repeat the introduction on the Internet. We know that X is based on the server and client

To provide services, we need to communicate with the server if we want to use its functions. Use

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

Display_name can be the DISPLAY environment variable, and echo $ DISPLAY output is: 0 (this is my linux mint input)

). If display_name is NULL, the environment variable is saved by default. Common X11 programming Headers

File:

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

I use # include <X11/Xlib. h> and # include <X11/extensions/XTest. h>.

XTest. h has the interfaces XTestFakeButtonEvent, XTestFakeMotionEvent, and

XTestFakeKeyEvent. For more information, you only need to add a function name to man on the terminal.

For example, the XTestFakeMotionEvent interface:

Copy codeThe Code is as follows:
Int XTestFakeMotionEvent (display, screen_number, x, y, delay );

Display * display; // The value is obtained from XOpenDisplay.
Int screen_number; // set it to-1 to indicate the current screen.
Int x, y; // screen position
Unsigned long delay; // delay in milliseconds. Set it to CurrentTime to indicate no delay.

Finally, we need to disable the Display handle: XCloseDisplay (Display * display ).

The interface implementation is as follows:

Copy codeThe Code is 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) {// press the keyboard
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) // move the mouse
{
If (0 = XTestFakeMotionEvent (dsp,-1, x, y, CurrentTime ))
{
Printf ("Cannot move! \ N ");
Return-1;
}
Return 0;
}
Int buttonpress (Display * dsp, int type) // press the mouse, type = 1 to indicate the left button, 3 to context, 2 to context
{
If (0 = XTestFakeButtonEvent (dsp, type, 1, CurrentTime ))
{
Printf ("press failed \ n ");
Return-1;
}
Return 0;
}
Int buttonrelease (Display * dsp, int type) // release the mouse
{
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 () {// the testing result will output c before the cursor after the program ends.
// Display * dsp = dspopen ();
// Presskey (dsp, 'C ');
// Releasekey (dsp, 'C ');
// Dspclose (dsp );
// Return 0;
//}

The main function commented out above can be used for testing. Well, we save the above Code as display. c.

To compile a shared library, the X11 and Xtst libraries are required.

Copy codeThe Code is as follows:
Gcc-fPIC-shared-o libdisplay. so display. c-lX11-lXtst

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

2. Brief introduction and use of ctypes

We know that the types in python are different from those in c. It should be said that there are not the same types. Let's take int for example,

Python also treats it as a PyObject type. Then we need to use the interface provided by ctype for the Type

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

This link provides a detailed interface for converting the display type. The following describes the specific operations.

We load data into the database through the CDLL () interface:

Copy codeThe Code is as follows:
Lc = CDLL ("./libdisplay. so ")

Then you can use the interface provided in the library, but the returned value of the dspopen () interface above is a Display pointer,

So we need to convert it with c_void_p:

Copy codeThe Code is as follows:
D = c_void_p (lc. dspopen ())

Then you can use d for processing. The Code is as follows:

Copy codeThe Code is 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 will open the right-click menu in 5 seconds.

So many libraries written in c using ctypes. In the future, I will write part of the c code as the c extension of python.

You can use the above interface to simulate the keyboard and mouse to 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.