python keyboard key combination code example
Example 1: Presskeys in python
import pyautogui
pyautogui.keyDown("alt")
pyautogui.press("tab")
pyautogui.keyUp("alt")
Example 2: 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 3: how to execute key combinations with keyboard python lib
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.press('a')
keyboard.release('a')
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
keyboard.press('a')
keyboard.release('a')
keyboard.press(KeyCode.from_vk(0x5C))
keyboard.press(KeyCode.from_vk(0x27))
keyboard.release(KeyCode.from_vk(0x27))
keyboard.release(KeyCode.from_vk(0x5C))
keyboard.type('Hello World')
Example 4: python keyboard press
import keyboard
while True:
try:
if keyboard.is_pressed('q'):
print('You Pressed A Key!')
break
except:
break