python keyboard wait for key code example

Example 1: 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'))

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

# Blocks until you press esc.
keyboard.wait('esc')

# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', '[email protected]')

# Block forever, like `while True`.
keyboard.wait()

Example 2: python get key module

import getkey, os
def clear():
  if os.name == "nt":
    _ = os.system('cls') #Clear function
  else:
    _ = os.system('clear')
current = "" 
while True:
  clear() #Clearing, required at beginning at end in order for algorithm to work
  print(current)
  key = getkey.key() # Gets the key
  if key == getkey.keys.BACKSPACE: # Detects if key is backspace
    current = current[:-1]
  elif key == getkey.keys.ENTER: # Detecs if key is the enter(return) key
    break
  else:
    current = current + key # Otherwise, adds on the the current variable
  clear()
clear()
print("\n\n\n You typed: " + current)