pyautogui module
Introduction#
pyautogui is a module used to control mouse and keyboard. This module is basically used to automate mouse click and keyboard press tasks. For the mouse, the coordinates of the screen (0,0) start from the top-left corner. If you are out of control, then quickly move the mouse cursor to top-left, it will take the control of mouse and keyboard from the Python and give it back to you.
Mouse Functions
These are some of useful mouse functions to control the mouse.
size() #gave you the size of the screen
position() #return current position of mouse
moveTo(200,0,duration=1.5) #move the cursor to (200,0) position with 1.5 second delay
moveRel() #move the cursor relative to your current position.
click(337,46) #it will click on the position mention there
dragRel() #it will drag the mouse relative to position
pyautogui.displayMousePosition() #gave you the current mouse position but should be done on terminal.
Keyboard Functions
These are some of useful keyboard functions to automate the key pressing.
typewrite('') #this will type the string on the screen where current window has focused.
typewrite(['a','b','left','left','X','Y'])
pyautogui.KEYBOARD_KEYS #get the list of all the keyboard_keys.
pyautogui.hotkey('ctrl','o') #for the combination of keys to enter.
ScreenShot And Image Recognition
These function will help you to take the screenshot and also match the image with the part of the screen.
.screenshot('c:\\path') #get the screenshot.
.locateOnScreen('c:\\path') #search that image on screen and get the coordinates for you.
locateCenterOnScreen('c:\\path') #get the coordinate for the image on screen.