Keyboard module: Controlling your Keyboard in Python

By | March 2, 2021
Keyboard module- Controlling your Keyboard in Python

In Python, we have this amazing module “keyboard” that can be used to control the keyboard of any system. And in this tutorial, we will learn some of its most important functions and see how they work.

Python keyboard is an open-source third-party library that can control your keyboard. Although it is a very small API and supports only a few features but they are enough to serve you with complex functionalities.

Vamware

To install the keyboard for your Python environment run the following pip install command on your terminal or command prompt.

pip install keyboard

Add Abbreviation with Python keyboard library

The keyboard module provides us with a add_abbreviation() function that will set a specified full string for an abbreviation.

If you have ever worked with MS Word, there if you write the 2nd and hit space it will superscript the nd to 2 like 2nd. Similarly, we can use the add_abbreviation() function and specify a string or a value to a specific abbreviation.

Example 

import keyboard

#type cp for copyright character
keyboard.add_abbreviation('cp', '© ')

#type tgb for copyright character
keyboard.add_abbreviation('tgb', 'TechGeekBuzz.com')

print("Do not close the terminal")
#deactivate with Esc
keyboard.wait("Esc")

Output

Do not close the terminal

After executing the above script, you can open any text editor, and if you type cpit will print © and if your type tgb it will print TechGeekBuzz.com.

And if you want to stop the abbreviation you need to press Esc key.

Set HotKey with Python keyboard modules

Let’s say you want to create your own hotkey, that will perform a specific operation when a specific combination of keys is pressed. Hotkeys are also known as shortcut keys, for example, Ctrl+C is a hotkey for copy, Ctrl+V is the hotkey for paste, and so on.

Using the Python keyboard add_hotkey() function we can create a new shortcut key, let’s create a hotkey with a combination of Ctrl +G that will open Google Chrome.

import webbrowser
import keyboard

keyboard.add_hotkey("ctrl+g", lambda:webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("http://google.com"))

print("Press Ctrl+G to open Google Chrome")

#deactivate with Esc
keyboard.wait("Esc")

Output

Press Ctrl+G to open Google Chrome

When you execute the above program then you can press Ctrl+G it will open google chrome for you.

The webbrowser is an inbuilt Python module that is used to handle browsers in Python. To deactivate the hotkey you can press Esc.

If you are on macOS then instead of C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s path you should use

'open -a /Applications/Google\ Chrome.app %s'

Record the Keys with Python keyboard modules

Let’s say you want a log that contains all the records of keys that you have pressed. In the keyboard module, we have the record(until)function that will record all the keyboard events pressed by the user.

Example

import keyboard

#record the keyboard event till shift key press
rec = keyboard.record("shift")

#print the records
print(rec)

#deactivate the recorded program with Esc key
keyboard.wait("Esc")

The above program will record the keyboard events and print them as a Keyboard module event when you press the shift key. Let’s say you want to print all the events you have done after running the above program, for that, you need to use the play()function.

import keyboard

#record the keyboard event till shift key press
rec = keyboard.record("shift")

keyboard.play(rec, speed_factor=10)

print("Write Something on a text editor and press shift")
#deactivate the record program with Esc key
keyboard.wait("Esc")

Conclusion

In this Python tutorial, we learned how can we use the Python keyboard module to control some of the keyboard functionalities. In the above article, we learned how can we set abbreviations, hotkey, and record keyboard events using the Python keyboard library. The library is very small yet provides many powerful features. You can visit its Github page to know more about the functions and methods provided by this library.

Leave a Reply

Your email address will not be published. Required fields are marked *