How to Get the Current Position of the Mouse in Python with the pyautogui Module



Python


In this article, we show how to get the current position of the mouse in Python with the pyautogui module.

pyautogui is a module that can do many different dynamic things, including sending virtual keypresses and mouse clicks to Windows.

With it, we can get the current position of the mouse.

To determine the mouse's current position, we use the statement, pyautogui.position().

This function returns a tuple of the position of the mouse's cursor. The first value is the x-coordinate of where the mouse cursor is. And the second value is the y-coordinate of where the mouse cursor is.

So the full code to execute this is shown below.



So the first thing that has to be done is you have to import the pyautogui module.

After this, we run the line, pyautogui.position()

This gives a 2-value tuple of the position of the mouse cursor.

If you move the mouse cursor, this value will change.

And this is all that is required to get the current position of the mouse in Python using the pyautogui module.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...