Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Keyboard module in Python
Next article icon

Keyboard module in Python

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
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 keyboard

Examples of Keyboard Module

Example 1: Simulating Key Presses and Blocking Keys

This 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 
RK
rk

Explanation:

  • 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 Module

This 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')  

Output

aGeek
you entered hotkey

Explanation:

  • 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 Events

This 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) 

Output

www.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).

Next Article
Keyboard module in Python

B

bestharadhakrishna
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2018
  • Python-Library
Practice Tags :
  • python

Similar Reads

    Keyword Module in Python
    Python provides an in-built module keyword that allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are una
    2 min read
    Keywords in Python | Set 2
    Python Keywords - Introduction Keywords in Python | Set 1  More keywords:16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in "try" block is checked, if there is any type of error, except block is executed. 17. except : As expl
    4 min read
    Python VLC MediaPlayer – Enabling Keyboard Input
    In this article, we will see how we can enable keyboard input handling the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediaPlayer object is the bas
    2 min read
    Python EasyGUI module - Introduction
    EasyGUI is a module for very simple, very easy GUI programming in Python. EasyGUI is different from other GUI generators in that EasyGUI is NOT event-driven. Instead, all GUI interactions are invoked by simple function calls. Unlike other complicated GUI's EasyGUI is the simplest GUI till now. Insta
    2 min read
    Python Introduction
    Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPython’s simple and readable syntax makes it beginner-frien
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences