I made a minimal pynput
program that does different actions when the left
and right
arrow keys are pressed. I want to turn this into a function that can take different values as arguments.
Here is a minimal working example without a function, and without arguments:
from pynput.keyboard import Key, Listener
def on_press(key):
print('{0} pressed'.format(key))
if key == Key.esc:
return False
with Listener(on_press=on_press) as listener:
listener.join()
It prints the key you press, and quits when you press esc
.
And here is what I am trying to implement. Note that it doesn't work. Just look at the logic. When left
arrow is pressed, I want the program to execute a command that depends on a given argument. And vice versa for the right arrow.
from pynput.keyboard import Key, Listener
def on_press(key, left, right):
if key == Key.left:
print("{}'s key was pressed.".format(left))
if key == Key.right:
print("{}'s key was pressed.".format(right))
if key == Key.esc:
return False
def activate(name1, name2):
with Listener(
on_press=on_press(key, left=name1, right=name2)) as listener:
listener.join()
# when pressed, the program should print what on_press() is given as argument
activate('Mark', 'John')