This is an example code from the pynput
documentation. It basically prints the key you are pressing.
from pynput import keyboard
def on_press(key):
print('You pressed {}'.format(key))
if key == keyboard.Key.esc:
return False
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()
I need to pass an argument to the on_press
function, but it's called without parentheses. I don't understand why and what this does. Ideally, this is what I'd like to make work:
from pynput import keyboard
def on_press(key, addition):
print('You pressed {}, {}'.format(key, addition))
if key == keyboard.Key.esc:
return False
string = 'congrats!'
with keyboard.Listener(
on_press=on_press(key, string)) as listener:
listener.join()