Simulate the mouse in Linux using Python

Source: Internet
Author: User

Simulate the mouse in Linux using Python
14:41:22

Category: Linux

Install the python-xlib Library
Usage:
1) record the mouse track:Code:Python smartmouse. py-r <record time> <storage file>

2) replay the mouse track:Code:Python smartmouse. py-P <storage file>

The record file format is as follows:Code:6, 8, sleep: 2
6, 8, click: 1
6, 8, sleep: 3
315,4, click: 1
315,4, sleep: 2
800,662, click: 3
800,662, sleep: 2
780,662, click: 1

The first two are the X and Y coordinates of the screen, which are optional from the third. Click, release, press, and sleep are supported.
Click, release, press followed by ":", ":", followed by the number 1 indicates the left button, 2 indicates the middle button, 3 indicates the right button, and 4 indicates the Rolling Wheel, 5 is a rolling round.
The number next to sleep indicates the duration of the coordinate point, in seconds.

The unfinished part. The mouse button events cannot be recorded when the mouse track is automatically recorded. Please provide suggestions or solutions for xlib users.

Source code:Code:#! /Usr/bin/Python
#-*-Coding: UTF-8 -*-

# Name: smartmouse. py
# Useage:
#1) record the mouse track
# Python smartmouse. py-r <record time> <storage file>
#2) replay the mouse track
# Python smartmouse. py-P <storage file>
#
# Coded by xiooli <xioooli [at] yahoo.com.cn>
#2009.10.17

Import xlib. display as DS
Import xlib. X as X
Import xlib. Ext. xtest as xtest

Class mouse ():
'''Mouse class which contains couple of mouse Methods '''
Def _ init _ (Self ):
Self. Display = Ds. Display ()

Def mouse_press (self, button ):
'''Button = 1 left, 2 middle, 3 right, 4 middle up, 5 middle down '''
Xtest. fake_input (self. display, X. buttonpress, button)
Self. display. Sync ()
Def mouse_release (self, button ):
'''Button = 1 left, 2 middle, 3 right, 4 middle up, 5 middle down '''
Xtest. fake_input (self. display, X. buttonrelease, button)
Self. display. Sync ()
Def mouse_click (self, button ):
'''Button = 1 left, 2 middle, 3 right, 4 middle up, 5 middle down '''
Self. mouse_press (button)
Self. mouse_release (button)

Def goto_xy (self, x, y ):
'''Move to position X y '''
Xtest. fake_input (self. display, X. motion1_y, x = x, y = y)
Self. display. Flush ()

Def pos (Self ):
'''Get mouse position '''
Coord = self. display. Screen (). Root. query_pointer (). _ DATA
Return (coord ["root_x"], coord ["root_y"])

Def screen_size (Self ):
'''Get screen size '''
Width = self. display. Screen (). width_in_pixels
Height = self. display. Screen (). height_in_pixels
Return (width, height)

I = 0
Def elapse ():
'''Get elapse time, gap is 0.1 second '''
Global I
I + = 0.1
Return I

If _ name _ = "_ main __":

Import sys
Import time as TM

T = 0
M = mouse ()
Mouse_ev = ""
Ev = []
M = mouse ()
Evdic = {"Press": M. mouse_press,
"Release": M. mouse_release,
"Click": M. mouse_click,
"Sleep": TM. Sleep}

If SYS. argv [1] = "-R ":
Try:
Rctm = SYS. argv [2]
Except t:
SYS. Exit (1)
Try:
Logfile = SYS. argv [3]
Except t:
Logfile = "./mouse. log"

F = open (logfile, "W ")
While T <= float (rctm ):
T = elapse ()
PS = M. pos ()
F. Write (STR (PS [0]) + "," + STR (PS [1]) + "\ n ")
TM. Sleep (0.1)
F. Close ()
Elif SYS. argv [1] = "-P ":
Try:
F = open (SYS. argv [2])
Except t:
SYS. Exit (2)

Coord = f. readlines ()
F. Close ()

For pos_xy in coord:
Try:
Pos_x, pos_y = pos_xy.replace ("\ n", ""). Split (",")
Failed t valueerror:
Pos_x, pos_y, mouse_ev = pos_xy.replace ("\ n", ""). Split (",")
Ev = mouse_ev.split (":")

M. goto_xy (INT (pos_x), INT (pos_y ))

If eV and EV [1]:
If EV [0]! = "Sleep ":
Evdic [EV [0] (INT (EV [1])
Else:
Evdic [EV [0] (float (EV [1])
Ev = []

TM. Sleep (0.1)

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.