In fact, there is an equivalent function press_and_release() that does the same thing. This will press and release the space button. Next, you can also simulate key presses using the send() function: # press space Print(keyboard.is_pressed('ctrl')) Pressing & Releasing Buttons You can also check whether a button is actually pressed: # check if a ctrl is pressed "ctrl+alt+p" refers to the button CTRL, ALT, and P character pressed at once, so whenever these buttons are pressed at once, the callback will get called, in this case, it will just print a simple message, but you can make anything you want, such as desktop shortcuts. Second, you can also invoke a callback every time a hotkey is pressed: keyboard.add_hotkey("ctrl+alt+p", lambda: print("CTRL+ALT+P Pressed!")) Now execute this line of code, and then open up any text editor and write followed by a space, you'll see the magic! For instance, let's replace the text to the email address " ": # replaces every followed by a space with an actual " ") This module provides us with the function add_abbreviation() that enables us to register a hotkey that replaces one typed text with another. We will be using the keyboard module, let's install it: pip3 install keyboardĪlright, open up a Python interactive shell or a Jupyter notebook/lab and follow along.įirst, let's import the module: import keyboard Adding Hotkeys & Abbreviations
In this tutorial, you will learn how to use the keyboard module to control your computer keyboard in Python, this is of course useful for many tasks such as enabling us to automate various routine desktop tasks, building reinforcement learning agents, and much more.
Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.