Keyboard module in Python Last Updated : 12 Apr, 2025 Comments Improve Suggest changes 18 Likes Like Report Python provides a library named keyboard which is used to get full control of the keyboard. It's a small Python library which can hook global events, register hotkeys, simulate key presses and much more. It helps to enter keys, record the keyboard activities and block the keys until a specified key is entered and simulate the keys.It captures all keys, even onscreen keyboard events are also captured.Keyboard module supports complex hotkeys.Using this module we can listen and send keyboard events.It works on both windows and linux operating system.Installation:To install the keyboard module, run the following command:pip install keyboardExamples of Keyboard ModuleExample 1: Simulating Key Presses and Blocking KeysThis code demonstrates how to simulate key presses and block the execution until a specific key is pressed using the keyboard module. Python import keyboard keyboard.write("GEEKS FOR GEEKS\n") keyboard.press_and_release('shift + r, shift + k, \n') keyboard.press_and_release('R, K') keyboard.wait('Ctrl') Output:GEEKS FOR GEEKS RKrkExplanation:keyboard.write() writes the text "GEEKS FOR GEEKS" to the output.keyboard.press_and_release() simulates pressing and releasing the 'Shift + R', 'Shift + K', and 'Enter' keys.keyboard.wait() pauses the program execution and waits until the 'Ctrl' key is pressed to proceed.Example 2: Using Hotkeys with Keyboard ModuleThis code shows how to use hotkeys with the keyboard module to trigger actions when specific keys or key combinations are pressed. Python import keyboard keyboard.add_hotkey('a', lambda: keyboard.write('Geek')) keyboard.add_hotkey('ctrl + shift + a', print, args =('you entered', 'hotkey')) keyboard.wait('esc') OutputaGeekyou entered hotkeyExplanation:keyboard.add_hotkey() assigns the action of writing "Geek" when the 'a' key is pressed.A second hotkey (ctrl + shift + a) prints a message when pressed.keyboard.wait('esc') pauses the program until the 'Esc' key is pressed, terminating the program.Example 3: Recording and Playing Key EventsThis code demonstrates how to record and replay key presses using the keyboard module. Python import keyboard rk = keyboard.record(until ='Esc') keyboard.play(rk, speed_factor = 1) Outputwww.geeksforgeeks.org Explanation:keyboard.record() records all key presses until the 'Esc' key is pressed.keyboard.play() replays the recorded key events at normal speed (speed_factor = 1). Comment B bestharadhakrishna Follow 18 Improve B bestharadhakrishna Follow 18 Improve Article Tags : Technical Scripter Python Technical Scripter 2018 Python-Library Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like