python - how to detect global key events code example
Example 1: python check key press
import keyboard
if keyboard.is_pressed('a'):
print('a key has ben pressed')
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)