Last modified: Feb 15, 2023 By Alexander Williams

How to use pyautogui python library

In this article, I am going to show you how to use the library.

So, first, let us know what is pyautogui and what is it used for?.

What is pyautogui, and what is it used for

pyautogui is a library in which you can control mouse and keyboard accessible. For example, you would make a script that does something automatically on your computer.

PyAutoGUI installations

for install the library you can copy the command below

code:

 pip install pyautogui

how to use PyAutoGUI

before using PyAutoGUI, we need to import the library module

code:

 import pyautogui

1- getting current mouse

code:

 pyautogui.position()

Output:

 Point(x=875, y=519)

x means width
y means height

2.getting current screen resolutions.

Code:

 pyautogui.size()

Output:

 Size(width=1440, height=900)

3.moving mouse

before we talk about the moving mouse, we need to know this code

code:

 pyautogui.size()

When fail-safe mode is True, moving the mouse to the upper-left will raise a pyautogui.FailSafeException that can abort your program, so I recommended setting False.

In this example, we'll move the mouse to the center of the screen.

Code:

  >>> pyautogui.size() #getting current screen resolutions
Size(width=1440, height=900) # outpup / my current screen resolutions
>>> 1440/2 # here i made a division to get middle of the width
720
>>> 900/2 # here i made a division to get middle of the height.
450

pyautogui.moveTo(720, 450,) # command to move mouse 

for more informations, please visit the library document link