Automating Keyboard
Automatically typing with keyboard:
First, we would learn how to simulate typing something using pyAutoGUI. Here in this example, we would type something in a notepad file using the .typewrite() function. In this code, we first import the time and pyAutoGUI module and then use time.sleep() function to pause the execution of the program for some specified seconds. Then we use the pyautogui.typewrite() function to simulate typing of alphanumeric keys. The phrase inside the quotes would be typed. Implementation is as follows:
Syntax: pyautogui.typewrite()
Parameters: The function has only one parameter which is the string that needs to be typed.
Return Type: The functions don’t return anything but perform the jobs of simulating the typing of a string that is passed inside it.
Below is the implementation:
Python3
# used to access time related functions import time import pyautogui # pauses the execution of the program # for 5 sec time.sleep( 5 ) # types the string passed inside the # function pyautogui.typewrite( "Geeks For Geeks!" ) |
Output:
Pressing specific keys and simulating hotkey:
Next, we would explore two functions, the first one is .press() and the second one is .hotkey(), first one helps you to press a key generally used to press non-alphanumeric keys and the .hotkeys() functions helps us to press hotkeys like ctrl+shift+esc, etc. Here also we start the code by importing two modules, time and pyAutoGUI. Then we pause the execution of the program for 5 seconds using the sleep function. we type the string using the typewrite function. Then we use the .press() function to simulate a keypress and finally the .hotkey() function to simulate the pressing of hotkeys. In our example, we used hotkey ctrl+a that selects all the text. The implementation is as follows:
Syntax: pyautogui.press() and pyautogui.hotkey()
Parameters: The .press() function has only one parameter which is the key that needs to be pressed and the .hotkey() function has a number of parameters depending upon the number of keys to simulate the hotkey action.
Return Type: The functions don’t return anything but perform the job of simulating of pressing the enter key and simulates pressing the hotkey ctrl+a.
Below is the implementation:
Python3
# used to access time related functions import time import pyautogui # pauses the execution of the program # for 5 sec time.sleep( 5 ) # types the string passed inside the # function pyautogui.typewrite( "Geeks For Geeks!" ) # simulates pressing the enter key pyautogui.press( "enter" ) # simulates pressing the hotkey ctrl+a pyautogui.hotkey( "ctrl" , "a" ) |
Output:
GUI Automation using Python
In this article, we will explore how we can do GUI automation using Python. There are many modules that can do these things, but in this article, we will use a module named PyAutoGUI to perform GUI and desktop automation using python.
We would explore two sections –
- How to automatically use the mouse pointer to perform certain tasks like moving the cursor, clicking on a certain point on the screen, etc
- Also, we would explore how we can automate the keyboard keystrokes.