The Wayback Machine - https://web.archive.org/web/20240602235722/https://www.geeksforgeeks.org/keyboard-module-in-python/
Open In App

Keyboard module in Python

Last Updated : 24 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
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.

Install using this command:

pip install keyboard

Example #1:




# Using Keyboard module in Python
import keyboard
  
# It writes the content to output
keyboard.write("GEEKS FOR GEEKS\n")
  
# It writes the keys r, k and endofline 
keyboard.press_and_release('shift + r, shift + k, \n')
keyboard.press_and_release('R, K')
  
# it blocks until ctrl is pressed
keyboard.wait('Ctrl')


Output:

GEEKS FOR GEEKS 
RK
rk

 
Example #2: Keyboard module to enter hotkeys.




# Keyboard module in Python
import keyboard
  
# press a to print rk
keyboard.add_hotkey('a', lambda: keyboard.write('Geek'))
keyboard.add_hotkey('ctrl + shift + a', print, args =('you entered', 'hotkey'))
  
keyboard.wait('esc')


Output:

ark
you entered hotkey

 
Example #3: Keyboard module also used to record all the keyboard activities and replay them using play method.




# Keyboard module in Python
import keyboard
  
# It records all the keys until escape is pressed
rk = keyboard.record(until ='Esc')
  
# It replay back the all keys
keyboard.play(rk, speed_factor = 1)


Output:

www.geeksforgeeks.org 

 
Reference : https://pypi.org/project/keyboard/



Similar Reads

Os Module Vs. Sys Module In Python
Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you will be able to easily decide which module suits
5 min read
Simple Keyboard Racing with Python
Let's make a simple keyboard racing game using Python. In the game, the participant clicks a pair of keys in quick succession and the program shows the total time taken by the racer to cover the distance. Rules: As soon as you see 'GO!' on screen, start pressing the keys 'z' and 'x'. A '*' sign is shown for every meter covered. Pressing 'z' and 'x'
2 min read
Python | Vkeyboard (virtual keyboard) in kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Vkeyboard: VKeyboard is an onscreen keyboard for Kivy. Its operation is intended to be transparent to the user. Us
2 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 basic object in vlc module for playing the video. By
2 min read
Python Turtle - Graphics Keyboard Commands
Python Turtle module is a graphical tool that can be used to draw simple graphics on the screen using a cursor. Python Turtle was a part of Logo programming language which had similar purpose of letting the users draw graphics on the screen with the help of simple commands. Turtle is a pre-installed module and has inbuilt commands and features that
3 min read
Python Arcade - Handling Keyboard Input
In this article, we will discuss how to handle keyboard inputs in Python arcade module. In Arcade, you can easily check which keyboard button is pressed and perform tasks according to that. For this, we are going to use these functions: on_key_press()on_key_release() Syntax: on_key_press(symbol,modifiers)on_key_release (symbol, modifiers) Parameter
4 min read
Keyboard buttons in Telegram bot Using Python
One common way that we interact with the Telegram bot is through the use of keyboard buttons. These buttons give a convenient and intuitive way to input data and perform specific actions within the bot. Without keyboard buttons, we would have to manually type in commands or responses, which could be time-consuming and may lead to errors. Keyboard b
9 min read
Mouse and keyboard automation using Python
This article illustrates how to automate movements of mouse and keyboard using pyautogui module in python. This module is not preloaded with python. So to install it run the following command: pip3 install pyautogui Controlling mouse movements using pyautogui module Python tracks and controls mouse using the coordinate system of the screen. Suppose
5 min read
How to get keyboard input in PyGame ?
While using pygame module of Python, we sometimes need to use the keyboard input for various operations such as moving a character in a certain direction. To achieve this, we have to see all the events happening. Pygame keeps track of events that occur, which we can see with the events.get() function. In this article, we are going to discuss how we
3 min read
PyQt5 QSpinBox - Checking if user can change value using keyboard or not
In this article we will see how we can check if user can change the spin box value using key board or not, when we create a spin box there are basically two ways to change the value of the spin box one is by using arrow buttons or other way is using key board. We can stop keyboard input of spin box using setReadOnly method with its line edit object
2 min read