pyautogui is that one can control the mouse and keyboard python library, there are similar pywin32. This article describes the python in pyautogui achieve mouse and keyboard control functions, a friend in need can refer
pyautogui is that one can control the mouse and keyboard python library, there are similar pywin32.
pyautogui installation
pip3 install python3-xlib dependencies sudo apt-get install scrot the dependencies pip3 install pyautogui python3 mounted pyautogui introduced into the library Library import pyautogui
pyautogui protection methods
In order to prevent pyautogui seized control of the mouse can not lead us to turn off the program, it provides a protective measure, that is, mouse over the top left corner, then exit the program error;
Default FAILSAFE = True, protected mode is turned on
FAILSAFE = False Close protection measures
Acquiring screen information
size () Gets the current resolution of the screen, such as (1920, 1080) tuple
Note: the upper left corner of the screen is the origin (0,0), the entire screen is equivalent to the first quadrant
position () Gets the current mouse coordinates
onScreen (x, y) is determined the point (x, y) is within the range of the screen, such as a certain negative returns False
Move the mouse
moveTo (x, y, duration = 0.25) with the mouse over a time 0.25s (x, y) position moveRel (x, y, duration = 0.25) to the mouse location as an origin, move the mouse (x, y ) at
Code below, so that the mouse 10 is rotated about a square circle designated location
import pyautogui
for i in range(10):
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.moveTo(400, 300, duration=0.25)
pyautogui.moveTo(400, 400, duration=0.25)
pyautogui.moveTo(300, 400, duration=0.25)
Code below, so that the current position of the mouse about 10 revolutions around the square
import pyautogui
for i in range(10):
pyautogui.moveRel(100, 0, duration=0.25)
pyautogui.moveRel(0, 100, duration=0.25)
pyautogui.moveRel(-100, 0, duration=0.25)
pyautogui.moveRel(0, -100, duration=0.25)
Mouse Events
Click the mouse
click(x, y, button=‘left’, click=3, interval=0.5)
button has three options: left, middle, right, without the default click the left mouse button click means to click the frequency interval represents the time interval between each click click () function is actually a mouseDown () and mouseUp () Composition , i.e., release and press;
pyautogui.doubleClick () Double-click the mouse, in fact, performed twice click () function. pyautogui.rightClick () Right-click pyautogui.middleClick () in strike
Mouse Wheel
scroll (200) to control the mouse wheel, the roll value, the negative roll
Drag the mouse
dragTo () dragRel ()
Press the mouse button and drag to the specified location, use the same moveTo (), moveRel () window interception process, to find the target button screenshot function
im = screenshot (region = (x, y, width, heigth)) taken at (x, y) is the upper left corner of the specified high and wide areas, without the parameter, interception of the entire screen by default
im.getpixel ((x, y)) of pixels acquired the specified location is a triple (note input format) of the pixel (x, y, (R, G, B)) is determined (x, y) whether at pixelMatchesColor equal RGB im.save ( 'xx.png') is saved as x.png
Looking buttons
locateOnscreen ( 'xx.png') screen and xx.png find the same icon position, is a four-tuple click (center (locateOnscreen ( 'xx.png'))) click on the center of the icon, center used to obtain the icon the coordinates of the center point
For example, the application NetEase cloud music saved as music.png, automated clicking on the implementation of the above Netease cloud icon
last to recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience, learning pen
Remember, there is a glimmer of corporate experience, and calmed down to zero on the basis of the actual project data, we can also below the message, not the
Understand proposed, we will study together progress
summary
The above is to introduce small series python in the mouse and keyboard control functions implemented pyautogui