python keyboard buttons code example
Example 1: Presskeys in python
import pyautogui
pyautogui.keyDown("alt")
pyautogui.press("tab")
pyautogui.keyUp("alt")
Example 2: python type on keyboard
pip install keyboard
import keyboard
keyboard.press_and_release('shift+s, space')
keyboard.write('The quick brown fox jumps over the lazy dog.')
keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))
keyboard.wait('esc')
recorded = keyboard.record(until='esc')
keyboard.play(recorded, speed_factor=3)
keyboard.add_abbreviation('@@', '[email protected]')
keyboard.wait()
Example 3: python get key module
import getkey, os
def clear():
if os.name == "nt":
_ = os.system('cls')
else:
_ = os.system('clear')
current = ""
while True:
clear()
print(current)
key = getkey.key()
if key == getkey.keys.BACKSPACE:
current = current[:-1]
elif key == getkey.keys.ENTER:
break
else:
current = current + key
clear()
clear()
print("\n\n\n You typed: " + current)
Example 4: module to read keyboard
import keyboard
while True:
try:
if keyboard.is_pressed('q'):
print('You Pressed A Key!')
break
except:
break