Simulating keyboard input in Python
Python can be used for almost anything. Using Python, we can develop backends for web applications, backends for mobile applications, and APIs using free and open source frameworks such as Django
and Flask
.
In addition, Python programs also use powerful libraries such as Keras
, NumPy
, Tensorflow
and PyTorch
to create efficient machine learning models, which use Matplotlib
to draw various graphs and more.
In this article, we will see such use cases of Python. We will learn how to simulate or control the keyboard using Python.
We'll discuss two open source Python libraries, keyboard
and PyAutoGUI
, that let us control the keyboard using a Python script.
keyboard
Use the library to simulate keyboard
in Python
keyboard
The library is an open source library for controlling your keyboard.
This library can listen to and send keyboard events, use hotkeys, support internationalization, and mouse
provide mouse support with the help of the library, which can be downloaded using pip install mouse
or pip3 install mouse
.
To install keyboard
the library, use pip
one of the following two commands.
pip install keyboard
pip3 install keyboard
Let us understand how to use this library to control the keyboard. See the following Python code for a simple example of typing some text.
import keyboard
keyboard.write("Python is an amazing programming language.")
keyboard.press_and_release("enter")
keyboard.press_and_release("shift+p")
keyboard.press_and_release("y")
keyboard.press_and_release("t")
keyboard.press_and_release("h")
keyboard.press_and_release("o")
keyboard.press_and_release("n")
Output:
Python is an amazing programming language.
Python
Before running the above code, take note of your text cursor or caret. The above text in the output box will be automatically entered there.
write()
The function takes input as any string passed as an argument to this function. This function sends artificial keyboard events to the operating system followed by further typing at the caret.
If no character is available on the keyboard, an explicit Unicode character is typed. press_and_release()
The function sends an operating system event to execute the hotkey and type the character passed as a parameter.
PyAutoGUI
Use the library to simulate keyboard
in Python
PyAutoGUI
The library allows us to write Python scripts to control the keyboard and mouse.
The library can move the mouse cursor and click on windows and applications, send key events to type characters and execute hotkeys, take screenshots, move, resize, minimize, maximize and position applications on the screen, show alert messages, and more.
To install this library, use either of the following commands.
pip install pyautogui
pip3 install pyautogui
We can use PyAutoGUI
the library for our use case. See the following code.
import pyautogui
pyautogui.write("Python is an amazing programming language.")
Output:
Python is an amazing programming language.
As we can see, write()
the function passes the character type of the string as a parameter at the caret. This function only works on single character keys like letters and numbers.
This means we cannot press Shift, Ctrl, Command, Alt,Option< 等键/kbd>、F1 和 F3。我们可以使用 keyDown()
和 keyUp()
方法来按下这些键。
keyDown()
Method presses a key and keeps it held down. keyUp()
Method releases a held key.
See the following Python code for an example. Don't forget to note the position of the text cursor or caret.
import pyautogui
pyautogui.keyDown("shift")
pyautogui.press("a")
pyautogui.press("b")
pyautogui.press("c")
pyautogui.keyUp("shift")
pyautogui.press("x")
pyautogui.press("y")
pyautogui.press("z")
pyautogui.keyDown("shift")
pyautogui.press("a")
pyautogui.keyUp("shift")
pyautogui.keyDown("shift")
pyautogui.press("b")
pyautogui.keyUp("shift")
pyautogui.keyDown("shift")
pyautogui.press("c")
pyautogui.keyUp("shift")
Output:
ABCxyzABC
To press keys like Shift+ , we can also use the method. This function will press whatever key is passed as a string.Fpress()
Behind the scenes, this function is just a wrapper around keyDown()
the and keyUp()
methods.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Writing logs to a file in Python
Publish Date:2025/05/06 Views:132 Category:Python
-
This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file
Comparing two dates in Python
Publish Date:2025/05/06 Views:97 Category:Python
-
This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods. Comparing two dates in Python usin
Reload or unimport modules in Python
Publish Date:2025/05/06 Views:58 Category:Python
-
Modules allow us to store definitions of different functions and classes in Python files, which can then be used in other files. pandas , NumPy , scipy , Matplotlib are the most widely used modules in Python. We can also create our own modu
Pausing program execution in Python
Publish Date:2025/05/06 Views:156 Category:Python
-
This tutorial will demonstrate various ways to pause a program in Python. Pausing the execution of a program or application is used in different scenarios, such as when a program requires user input. We may also need to pause the program fo
Importing modules from a subdirectory in Python
Publish Date:2025/05/06 Views:190 Category:Python
-
This tutorial will explain various ways to import modules from subdirectories in Python. Suppose we have a file in a subdirectory of our project directory and we want to import this file and use its methods in our code. We can import files
Sleeping for a number of milliseconds in Python
Publish Date:2025/05/06 Views:124 Category:Python
-
In this tutorial, we will look at various ways to pause or suspend the execution of a program in Python for a given amount of time. Let's say we want to pause the execution of a program for a few seconds to let the user read instructions ab
Python Numpy.pad Function
Publish Date:2025/05/06 Views:104 Category:Python
-
In Python, we have NumPy the array module to create and use arrays. Arrays can have different sizes and dimensions. Padding is a useful method that can be used to compensate for the size of an array. We can mutate an array and add some padd
Generating Random Colors in Python
Publish Date:2025/05/06 Views:135 Category:Python
-
In the digital world, colors are represented in different formats. RGB, Hexadecimal format are just some of the commonly used formats. In this tutorial, we will learn how to generate random colors in Python. When we talk about generating ra
Getting the name of a class in Python
Publish Date:2025/05/06 Views:83 Category:Python
-
Just like object constructors, classes can be defined as user-defined prototypes for creating objects. class Classes can be created using the keyword . A class is a data structure that can contain both data members and member methods. This