Keyboard module: Controlling your Keyboard in Python

Posted in /  

Keyboard module: Controlling your Keyboard in Python
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    In Python, we have this amazing "keyboard" module that you can use for controlling the keyboard of any system. The module can come in handy in various instances, such as when you want to automate your tasks on your system that require inputs from the keyboard.

    Well, in this tutorial, we will learn some of the most important functions of the keyboard module and see how it works.

    What is Python Keyboard Module?

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

    To install the keyboard module for your Python environment, you need to run the following pip command on the terminal or command prompt:

    pip install keyboard

    Add Abbreviation with Python Keyboard Library

    The keyboard module provides us with an add_abbreviation() function that will set a specified full string for an abbreviation. If you have ever worked with MS Word, you might know that if you write the 2nd and hit space, it will superscript the nd to 2, like this 2 nd .

    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. In the text editor, you will see that if you type cp it will print ©, and if you type tgb it will print TechGeekBuzz.com. And if you want to stop the abbreviation you need to press the Esc key.

    Set HotKey with Python Keyboard Modules

    Let's say you want to create your own hotkey that performs a particular 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 and Ctrl+V is the hotkey for paste.

    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

    After you execute the above program, you can press Ctrl+G to open Google Chrome. 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 Module

    Let's say you want a log that contains all the records of keys that you have pressed. The keyboard module offers a record(until) function that will record all the keyboard events (keys 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 discussed how to use the Python keyboard module for controlling various keyboard functionalities. In particular, we mentioned how you can set abbreviations, hotkeys, and record keyboard events using the Python keyboard library. The library is small, but it comes with several powerful features.

    You can visit its Github page to know more about its functions and methods.

    People are also reading:

    Leave a Comment on this Post

    0 Comments