Javatpoint Logo
Javatpoint Logo

Keyboard Module in Python

Whenever we work on a computer or laptop or even smartphones, there are certain input devices that we use commonly to give input commands to the system. These input devices are programmed so that they take command from the user and tell the software of the system what action should be performed next. After that, the system performs the required action according to the input command provided by us, and the result of the action or task will be displayed through the output devices.

Let's understand this concept through the following example:

Suppose we are working on a laptop or computer and start typing in a word or excel file. The key which we enter or press on the computer's keyboard will give a command to the computer's system. And after that, the system will perform the required action, and the result will be displayed on the computer screen (An output device) as a result of the command. Let's say we pressed 'A' on the keyboard, which means that we are giving a command to the system that A should be written in the word or excel file. When the system processes this command, we will see an 'A' written in the text or word file on which we are currently working.

That's how commands are given through input devices, and the result is displayed through an output device in a computer or any other system. Now, to give commands to the system to perform a particular action, we need input devices that will deliver commands to the system. We use many common input devices in our daily lives, such as keyboard, mouse, scanner, and many others. Among all these input devices, the keyboard & mouse are the two input devices most commonly used by us to give input commands to the system on which we are working. The keyboard is an important input device that gives us access to giving all kinds of commands to the system to perform a particular task. Therefore, getting full control of the keyboard will be really helpful for us when we are working on a computer, laptop, or any other device. Getting full control of the keyboard will not only give us the option to provide all kinds of input commands to the system, but it will also help us to take full control over the system we are working on. That's why good programmers or developers always know how they can get full access over the keyboard they are working on. Apart from this, the keyboard is an integral part of every programmer or developer, and no one can imagine that a programmer is coding without using a keyboard. Therefore, the keyboard is one of the essential parts of the coding, and getting full control over it, is beneficial for us.

In today's time, many programming languages offer us the options or methods that we can use to get full control over the keyboard of the device on which we are working. Many programming languages even provide us with multiple packages or modules that we can use to get full control over the keyboard of the device in which we are coding. Python is also one of these programming languages that provide us with packages & modules that help us get full control over the keyboard. Now, talking specifically about Python, it supports many built-in and additional packages that come up with the functions that help us get full control over the system's keyboard. The keyboard module of Python is one such package offered by Python, which we can use in our Python programs to get full control over the keyboard. Therefore, we will learn about this keyboard module of Python in this tutorial and learn how we can have full control over the system's keyboard using the functions of this module. We will use the functions of this module in the example programs of this tutorial to understand their functions and how they work.

Introduction to Python Keyboard Module

The keyboard module is a Python package or module that comes with many built-in functions that are helpful for us to get full control over the keyboard. We can use the functions of the keyboard module in Python programs and get full control over the keyboard of the device on which we are working. Unlike many other big libraries, the keyboard module is a small library of Python which is designed to perform only a specific set of actions. The keyboard module of Python can be used to simulate key presses, register hotkeys, hook global events, and perform many other actions. We can perform all these actions through the functions of the keyboard module by importing the keyboard module in the example Python programs. We will learn more about this keyboard module of Python in the upcoming sections of this tutorial.

Keyboard Module of Python: Functions

As we have already learned that we can use the keyboard module of Python to get full control over the keyboard and perform many tasks using the functions of this module, we will now learn the functions that we can perform using the functions of this module. There are many popular functions such as simulating keypresses, registering hotkeys, and hooking global events, for which the keyboard module is used commonly. But apart from these functions, there are many other functions that we can easily perform using the functions of this module.

Following are the common functionalities of the keyboard module, which we can perform using the functions of this module:

(i) Capturing all keys: We can use the keyboard module to capture all the keys of a system's keyboard, including the on-screen keyboard we are using on that system. We can capture all the events performed on the keyboard, including the on-screen keyboard, using the functions of the keyboard module.

(ii) Operability: The keyboard module becomes an even more essential module for us as it can be used on Windows and Linux operating systems. The keyboard module works perfectly well on both types of operating systems, and we can use functions of these in the Python programs while working in any of these two operating systems.

(iii) Listening and Sending Events: We can use the functions of the keyboard module in a Python program to listen to the events that are performed by the keyboard of the device. Apart from this, we can also use this module to send the information regarding the events performed on the keyboard on which we are working. We can also perform both these actions simultaneously by using the functions of the keyboard module in a Python program.

(iv) Support complex Hotkeys: We can use the keyboard module to carry out hotkeys tasks of a keyboard. This module provides functions that support many complex hotkeys tasks, and we can perform all these tasks by using this module's functions in the example programs.

Apart from the above-given actions, the following functions can also be performed using the functions of the keyboard module. The keyboard module will continue to perform these actions until a specified key is entered to stop the task.

(v) Block keys: We can use the keyboard module functions to block the activity of a particular key from our device's keyboard.

(vi) Enter keys: The functions of the keyboard module can be used by us to enter the keys from the keyboard as an input command to the system.

(vii) Record the keyboard activities: We can use the keyboard module functions to record all the activities performed by our device's keyboard.

We can carry out all these tasks using the functions of the keyboard module in the Python programs, and we will perform some of these functions of this module in the implementation part of this tutorial. We will learn implementation many of these functions by using the keyboard module in the example programs of this tutorial in later sections.

Keyboard Module of Python: Installation

The keyboard module of Python is not an in-built module package of the Python which means that when we install Python in our system, this package doesn't get installed along with it. Therefore, if we want to work with the functions of this module and learn more about it, first, we have to install this module in our system (if this module is not already installed in our system). To install the keyboard module of Python, we can use multiple methods or ways, but in this installation part, we will only use the pip installation method to install this module. We will use the pip installer method from the command prompt shell to install the keyboard module because this is the easiest and simplest installation method of this module. To install the keyboard module in our system using the pip installer method, first, we have to open the command prompt shell of our system and write the following pip command in it:

When we are done writing the installation command, we have to press the 'enter' key, as it will give the command to our system to start the installation process of the keyboard module. Once the installation process for this module starts, we have to wait for a while as it may take some time to install this module in our system successfully.

Keyboard Module in Python

As we can see, the keyboard module is now successfully installed in our system, and we can even verify this by checking the version of this module through the command prompt shell. Now, we can move forward to the implementation part of this module and start working with the functions of this module in the example programs.

Keyboard Module of Python: Implementation

Here, in this part, we will understand the working of the keyboard module's functions and how we can use these functions of the keyboard module to perform a particular task from our keyboard. That's how we understand the implementation of this keyboard module. We will understand the functioning of this module by the example programs we will use in this part.

Look at the following example programs to understand the implementation of the keyboard module and its functions:

(1) Blocking and Working with Keys:

Here, we will learn how we can block and work with the keys of our keyboard using the functions of the keyboard module. Before we discuss more this, let's have a look at the following example program first:

Example 1: Look at the following Python program where we have used keyboard module functions to block and work with keyboard keys:

Output:

Hey Python Developers, You all are welcomed to JavaTpoint
JTP
ython

As we can see, the commands we have provided to the keyboard are printed in the output when the functions of the keyboard module are executed with the example program. The keys of the keyboard will remain blocked in the output screen until we press the 'Ctrl' key from the keyboard. That's how we can use the functions of this keyboard module to work with our keyboard and block the keys of the keyboard.

Explanation: We have first imported the keyboard module as 'kb' in the program to easily use the functions of this module in the given example program. After that, we have used the write() function of this module to write a sentence in the output. This function will give the command to the keyboard of our system to write the sentence we provided as an argument inside the function. Then, we used the press_and_release() function of this module to work with the two keys of our keyboard at once. First, we used the shift argument inside this function to print the given letters in capital form. Secondly, we directly provided some capital letters inside this function as an argument to print them in small-case letter form in the output. Lastly, we have used this module's wait() function to block the working of all the keys of our keyboard unless the key specified in the wait() function is pressed.

(2) Keyboard Module to Work with the Hotkeys:

We will now use this keyboard module's add_hotkey() function to perform the hotkey function using our keyboard when the example program is executed successfully. This keyboard module's add_hotkey() function will add the Hotkey feature to the command given to our keyboard through this module. The hotkey function will be performed according to the arguments we provide inside the module's add_hotkey() function. Look at the following example program to understand the functioning of this add_hotkey() function of the keyboard module:

Example 2: Look at the following Python program where we have used the Hotkey features from our keyboard:

Output:

Hello Python developer You have entered a hotkey while working with the keyboard module
mHey Python Developers, You all are welcomed to JavaTpoint

As we can see, the result of the hotkey commands is printed in the output for the arguments we have provided inside the add_hotkey() function. That's how we can use the hotkey feature of our keyboard by using the add_hotkey() function of the keyboard module.

Explanation: We have given commands inside the add_hotkey() function of the keyboard module to perform the hotkey functions with our keyboard. First, we gave the print command, and the sentence will be printed once we press the 'm' letter from our keyboard in the output. Secondly, we gave the command to print another sentence when the 'ctrl + shift + m' keys will be pressed at once in the output. Lastly, we blocked all the keyboard keys in the output screen until we pressed the 'esc' key.

(3) Recording and Replaying all the Keyboard Activities:

We will learn how to use the keyboard module's functions to record all the keyboard activities that we perform in the output screen and replay them in the output screen itself. We will use the record() and play() functions of this module in the following example program to carry out this task:

Example 3: Look at the following Python program where we have recorded and replayed all keyboard activities:

Output:

Hey Python Developers, You all are welcomed to JavaTpoint

As we can see, all the keys we have used in the output screen of the Python before pressing the 'esc' key are recorded and replayed in the output with the speed factor of 1. That's how we can use functions of the keyboard module to record and replay the keyboard activities we performed.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA