pynput documentation code example
Example 1: pynput#
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
return False
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()
Example 2: pynput windwos key
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.type('Hello World')
Example 3: pynput keyboard backspace
from pynput.keyboard import Key, Controller
kb = Controller()
kb.press(Key.backspace)
kb.release(Key.backspace)