Mouse and Keyboard Automation:

In the article, we will see how to automate the movement of mouse and keyboard by using pyautogui module in Python for Data Science. This is not there already and we have to install it using the following code.

Mouse and Keyboard Automation in Python for Data Science - PST

Controlling the movement of Mouse:

In order to track and control the mouse, Python uses coordinate system of the screen. Let us assume our screen resolution is 1920 X 1080, in such case, it will look like this.

automation in python for data science

Some of the functions in this module:

  • size() – This function returns the screen resolution.

We should save the file we import by using the extension .py before the program is run.

  • moveTo() – This function is there to move the mouse in pyautogui module.

The code takes x and y coordinates and an optional duration as an argument. The function is there to move the mouse pointer from current location to the mention x and y coordinates in the argument mention. The process will take 1 sec as it is mention in the duration part.

  • moveRel() function:

    This function moves the mouse pointer relative to its current position.

  • position() – This function provides the current position of the pointer.

  • click() – This function helps in clicking and dragging the pointer.

The above code will result in a mouse click at the coordinate (100,100).

Similar to move, we have two operations related to drag, i.e., dragTo and dragRel. The function is similar to moveTo and moveRel, but the only difference is that it holds the right mouse button while moving resulting in a drag.

python data science

Before executing the program, open MS-Paint in the background and select pencil tools. After that run the code and switch to MS Paint before 10 seconds which is the sleep time provide in the code. We will see a box being drawn at the top left edge at 1000,1000 and edge length 100 pixels.

  • scroll() – This function takes the number of pixels as argument and then scrolls up the screen to a given number of pixels.

This will result in scrolling the screen up to 200 pixels.

  • typewrite() – We can automate the typing of string using typewrite() function. The string we want to type is passed as argument.
  • Passing key names: Key names can be passed separately using typewrite() function.
  • Pressing hot key combinations: We can use hotkey() function in order to press combination keys like ctrl+a, ctrl+c, etc.Mouse and Keyboard Automation in Python for Data Science - PST

To see more about automation in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.