向使用参数作为输入的函数添加额外参数

Adding extra parameters to a function which uses arguments as input

我有一个函数,当按下某个键时,它是 运行,为此我使用以下代码:

with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

但是函数 on_press 需要一些额外的参数。它被定义为 on_press(*args, x)。我该怎么做?

使用lambda函数:

def on_press(event, arg1, arg2):
    ...

with Listener(on_press=lambda event: on_press(event, arg1=val1, arg2=val2)) as listener:
        listener.join()