Keyboard module in Python
Last Updated :
24 Jan, 2019
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:
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
Example #2: Keyboard module to enter hotkeys.
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:
ark
you entered hotkey
Example #3: Keyboard module also used to record all the keyboard activities and replay them using play method.
import keyboard
rk = keyboard.record(until ='Esc')
keyboard.play(rk, speed_factor = 1)
|
Output:
www.geeksforgeeks.org
Reference : https://pypi.org/project/keyboard/
Please Login to comment...