In this tutorial, you will learn how to control your computer keyboard in Python, this is useful for many tasks such as enabling us to automate various routine desktop tasks, building reinforcement learning agents and more.
We will be using keyboard module, let's install it:
pip3 install keyboard
Alright, open up a Python interactive shell or a Jupyter notebook/lab and follow along.
First, let's import the module:
This module provides us with the function add_abbreviation() that enables us to register a hotkey that replaces one typed text with another. For instance, let's replace the text "@email" to an email address "firstname.lastname@example.org":
# replaces every "@email" followed by a space with an actual email keyboard.add_abbreviation("@email", "email@example.com")
Now execute this line of code, and then open up any text editor and write "@email" followed with a space, you'll see magic!
Second, you can also invoke a callback everytime a hotkey is pressed:
keyboard.add_hotkey("ctrl+alt+p", lambda: print("CTRL+ALT+P 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.
You can also check whether a button is pressed:
# check if a ctrl is pressed print(keyboard.is_pressed('ctrl'))
Next, you can also simulate key presses using the send() function:
# press space keyboard.send("space")
This will press and release the space button. But what if you want to write a long text ? send() would be inefficient. Luckily for us, write() function does exactly that, it sends artificial keyboard events to the OS simulating the typing of a given text, let's try it out:
keyboard.write("Python Programming is always fun!", delay=0.1)
Setting delay to 0.1 indicates 0.1 seconds to wait between keypresses, this will look fancy like in hacking movies!
You can do many more cool things with this module such as recording keyboard events using keyboard.record() function and playing them again using keyboard.play() function:
# record all keyboard clicks until esc is clicked events = keyboard.record('esc') # play these events keyboard.play(events)
You can explore what events list contain by simply printing it.
Finally, if you want to remove all keyboard hooks in use, including hotkeys, abbreviations, etc:
I just introduced the module to you, please check their documentation or just type help(keyboard) in a Python interactive shell for learning other functionalities and methods.
You can also take full control of your mouse, the same author of this module made another one for handling the mouse !
With such modules, you can build desktop automation scripts, keyboard shortcuts, keyloggers (although the author isn't responsible), and much more!
Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more to Python, I highly suggest you get these two Python books, as these books helped me a lot in my learning journey:
Alright, that's it, check the full code here.
Learn Also: How to Convert Python Files into Executables.
Happy Coding ♥View Full Code