How to Control a Keyboard using Python

Looking to control a keyboard using Python?

If so, you can accomplish this goal using the PyAutoGUI library.

You can install the PyAutoGUI library using the command below (under Windows):

pip install pyautogui

In this tutorial, you’ll see 4 scenarios that describe how to:

  1. Type characters using the write() function
  2. Press hotkeys using the hotkey() function
  3. Press keyboard keys using the press() function
  4. Open a text file and then type text

4 Scenarios to Control a Keyboard using Python

Scenario 1: Type characters using the write() function

You can use the write() function to type characters.

For example, the syntax below can be used to type ‘Hello There’:

import pyautogui

pyautogui.write('Hello There')

Run the code in Python, and you’ll get the ‘Hello There’ phrase:

How to Control a Keyboard using Python

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 can 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 ‘pagedown’ key 3 times.

Once you run the code, click on your web browser and you’ll see how the web page scrolls down.

A time delay of 3 seconds was added to allow you enough time to click on your web browser.

Here is the complete code:

import pyautogui
import time

time.sleep(3)
pyautogui.press('pagedown')
pyautogui.press('pagedown')
pyautogui.press('pagedown')

After you run the code in Python, and then immediately click on your web page, you’ll notice that the web page would move down.

Alternatively, you can achieve the same results (of pressing the ‘pagedown’ key 3 times) by setting presses=3:

import pyautogui
import time

time.sleep(3)
pyautogui.press('pagedown', presses=3)

Or by using the following code:

import pyautogui
import time

time.sleep(3)
pyautogui.press(['pagedown', 'pagedown','pagedown'])

As before, you can check the PyAutoGUI Documentation for the KEYBOARD_KEYS that you can 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, I created a text file (called Example_File) on my desktop:

C:\Users\Ron\Desktop\Example_File.txt

I then applied the code below to open the text file (using the os library), and then 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:

Open a text file and type text

Let’s take things further by performing the following actions:

  • Opening a text file
  • Waiting for 3 seconds
  • Typing the ‘Hello There‘ phrase in the text file (with a short delay of 0.1 seconds after typing each character)
  • Pressing the ‘enter’ key to start a new line in the text file
  • Typing ‘What is the Weather?‘ on the new line (with the same delay of 0.1 seconds per character)
  • Closing the text file using the hotkey alt+f4
  • Saving 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('What 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 to Control a Keyboard using Python

You may want to check the following tutorial if you’d like to learn how to control a mouse using Python.