You can use the PyAutoGUI library to control a keyboard using Python.
To start, here is the command to install the PyAutoGUI library:
pip install pyautogui
In this tutorial, you’ll see 4 scenarios that describe how to:
- Type characters using the typewrite() function
- Press hotkeys using the hotkey() function
- Press keyboard keys using the press() function
- Open a text file and then type text
4 Scenarios to Control a Keyboard using Python
Scenario 1: Type characters using the typewrite() function
You can use the typewrite() function to type characters.
For example, the syntax below can be used to type ‘Hello There‘:
import pyautogui pyautogui.typewrite("Hello There")
Run the code in Python, and you’ll get the following phrase:
You can take things further by adding a delay after typing each character.
For instance, let’s add 0.5 seconds delay (by adding an interval=0.5) after typing each character:
import pyautogui pyautogui.write("Hello There", interval=0.5)
Once you run the code, you’ll notice the 0.5 seconds delay after each character.
Scenario 2: Press hotkeys using the hotkey() function
You can use the hotkey() function for hotkeys or keyboard shortcuts.
For instance, if you’re using Windows, you can use ctrl + esc to open the Windows Start Menu:
import pyautogui pyautogui.hotkey("ctrl", "esc")
Once you run the code, you’ll get the Windows Start Menu.
You can check the PyAutoGUI Documentation for the KEYBOARD_KEYS that you may use.
Scenario 3: Press keyboard keys using the press() function
For this scenario, you’ll see how to use the press() function in order to press the ‘enter’ key 3 times.
Here is the complete code:
import pyautogui pyautogui.press("enter") pyautogui.press("enter") pyautogui.press("enter")
Alternatively, you can achieve the same results (of pressing the ‘enter’ key 3 times) by setting presses=3:
import pyautogui pyautogui.press("enter", presses=3)
Or by using the following code:
import pyautogui pyautogui.press(["enter", "enter", "enter"])
As before, you can check the PyAutoGUI Documentation for the KEYBOARD_KEYS that you may use.
Scenario 4: Open a text file and then type text
For the final scenario, you’ll see how to apply a combination of all the previous functions reviewed.
To start, you’ll need to create an empty text file.
For instance, let’s create a text file (called example_file) under the following path:
C:\Users\Ron\Desktop\example_file.txt
Then, apply the code below to open the text file, and type the ‘Hello There‘ phrase inside the text file (note that you’ll need to modify the path to reflect the location where the text file is stored on your computer):
import os import pyautogui import time os.startfile(r"C:\Users\Ron\Desktop\example_file.txt") time.sleep(3) pyautogui.write("Hello There")
Here is the result that you’ll get in the text file:
Let’s take things further by performing the following actions:
- Open a text file
- Wait for 3 seconds
- Type ‘Hello There‘ in the text file (with a short delay of 0.1 seconds after typing each character)
- Press the ‘enter’ key to start a new line in the text file
- Type ‘How is the Weather?‘ in a new line (with the same delay of 0.1 seconds per character)
- Close the text file using the hotkey alt+f4
- Save the file by pressing the ‘enter’ key
Here is the complete code (as before, you’ll need to modify the path to reflect the location where the text file is saved on your computer):
import os import pyautogui import time os.startfile(r"C:\Users\Ron\Desktop\example_file.txt") time.sleep(3) pyautogui.write("Hello There", interval=0.1) pyautogui.press("enter") pyautogui.write("How is the Weather?", interval=0.1) pyautogui.hotkey("alt", "f4") pyautogui.press("enter")
After you run the code, you’ll see these phrases when opening the text file:
How is the Weather?
You may also want to check the following tutorial that shows how to control a mouse using Python.
Thanks for the tutorial
You’re welcome 🙂
fine
Thanks