Python-specific implementation of simulating mouse and keyboard movements

Source: Internet
Author: User
Tags linux mint
This article mainly introduces how to simulate the mouse and keyboard movements in python. if you need it, you can refer to the previous month and plan to develop a fun project, but it has never been 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
# Include
# Include

I am using # include And # include .

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:

The 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:

The code is as follows:


# Include
# Include
# Include
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;
/

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.