cs client automation control mouse and keyboard operation, python + pyautogui

PyAutoGUI is a pure Python GUI automation tool. Its purpose is to automatically control mouse and keyboard operations with programs. It supports multiple platforms (Windows, OS X, Linux).

installation

pip3 install pyautogui

pyautogui mouse operation example

Import pyautogui 

# Get the current screen resolution 
screenWidth, screenHeight = pyautogui.size () 

# Get the current mouse position 
currentMouseX, currentMouseY = pyautogui.position () 

# 2 100, 100 seconds for the mouse to move the position of the absolute coordinate movement 
# pyautogui.moveTo (100, 100,2) 
pyautogui.moveTo (X = 100, Y = 100, 2 = DURATION, Tween = pyautogui.linear) 

# mouse over the center of the screen. 
pyautogui.moveTo (screenWidth / 2, screenHeight / 2 ) 

# Left-click once 
# pyautogui.click () 
# the X- 
# the y- 
# Clicks clicks 
# spacing interval between the clicks 
#button 'left', 'middle', 'right' corresponds to the left, middle, or right of the mouse (1, 2, or 3) 
# tween gradation function 
#
 pyautogui.click (x = None, y = None, clicks = 1, interval 0.0 =, = Button ' left ' , DURATION = 0.0, Tween = pyautogui.linear) 

# mouse movement relative downward movement 
# pyautogui.moveRel (None, 10) 
pyautogui.moveRel (= None xOffset, yOffset = 10, = DURATION 0.0, Tween = pyautogui.linear) 


# current mouse location spaced Double-0 
# pyautogui.doubleClick () 
pyautogui.doubleClick (X = None, Y = None, interval The = 0.0, Button = ' left ' , DURATION = 0.0, Tween = pyautogui .linear) 

# current mouse position 3 hit 
#pyautogui.tripleClick () 
pyautogui.tripleClick (x = None, y = None, interval = 0.0, button = ' left ' , duration = 0.0, tween = pyautogui.linear) 

# right-click 
pyautogui.rightClick () 

# middle-click 
pyautogui. a middleClick () 

#   by easing / fade function allows the mouse to move after 2 seconds (500, 500) position 
#   use tweening / move to function that the easing over 2 seconds the mouse. 
pyautogui.moveTo (X = 500, Y = 500, DURATION = 2, = Tween pyautogui.easeInOutQuad) 

# mouse drag 
pyautogui.dragTo (X = 427, Y = 535, DURATION =. 3, Button = ' left ' ) 

# mouse drag opposing 
pyautogui.dragRel (xOffset = 100, yOffset = 100, duration =, button =' Left ' , mouseDownUp = False) 

# move the mouse to x = 1796, y = 778 press positions 
pyautogui.mouseDown (x = 1796, y = 778, Button = ' left ' ) 

# move the mouse to x = 2745, y = 778 release position (in combination with a mouseDown selected) 
pyautogui.mouseUp (X = 2745, Y = 778, Button = ' left ' , DURATION =. 5 ) 

# current mouse position of the roller rolling 
pyautogui.scroll ()
 # mouse horizontal scrolling (Linux ) 
pyautogui.hscroll ()
 # mouse to scroll around (Linux) 
pyautogui.vscroll ()

pyautogui keyboard operation example

# Analog input information 
pyautogui.typewrite (Message = ' the Hello World! ' , Interval The = 0.5 )
 # Click the ESC 
pyautogui.press ( ' ESC ' )
 # shift- 
pyautogui.keyDown ( ' shift ' )
 # release shift 
pyautogui .keyUp ( ' Shift ' )
 # analog hotkey combination 
pyautogui.hotkey ( ' Ctrl ' , ' C ' )

Button support

Prompt message

alert

#pyautogui.alert('This is an alert box.','Test')
pyautogui.alert(text='This is an alert box.', title='Test')

option

#pyautogui.confirm('Shall I proceed?')
pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])

password

a = pyautogui.password('Enter password (text will be hidden)')
print(a)

prompt

a = pyautogui.prompt('input  message')
print(a)

Screenshot

Take a screenshot of the entire screen and save

im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')

im2 = pyautogui.screenshot('my_screenshot2.png')

Screen to find the position of the picture and get the middle point

# Find the specified image in the current screen (picture taken by the system needs to FIG screenshot function) 
the coords = pyautogui.locateOnScreen ( ' folder.png ' )
 # Obtain to FIG intermediate point coordinates 
X, Y = pyautogui.center (the coords)
 # Right-click the coordinate point 
pyautogui.rightClick (x, y)

Security Settings

Import pyautogui 

# precautions to avoid loss of control 
pyautogui.FAILSAFE = True
 # for all PyAutoGUI function to increase the delay. The default delay time is 0.1 seconds. 
pyautogui.PAUSE = 0.5

 

Guess you like

Origin www.cnblogs.com/easy-test/p/12707637.html