我如何使用键盘 python 模块使用带有参数的自定义函数来创建热键?

问题描述 投票:0回答:1

我一直在尝试制作一个在按下热键时按下按钮一定次数的东西。效果不太好。

这是代码:

import keyboard
def moveArrow(times: int):
    while times > 0:
        keyboard.press_and_release("a")
        times = times - 1
keyboard.add_hotkey("ctrl+shift+g", moveArrow, 20, True)
keyboard.wait("esc")

当我尝试使用热键时,它不断吐出此错误:

Error in keyboard hook:
Traceback (most recent call last):
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\_winkeyboard.py", line 541, in low_level_keyboard_handler
    should_continue = process_key(event_type, vk, scan_code, is_extended)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\_winkeyboard.py", line 529, in process_key
    return callback(KeyboardEvent(event_type=event_type, scan_code=scan_code or -vk, name=name, is_keypad=is_keypad))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\__init__.py", line 
268, in direct_callback
    callback_results = [callback(event) for callback in self.blocking_hotkeys[hotkey]]
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\__init__.py", line 
268, in <listcomp>
    callback_results = [callback(event) for callback in self.blocking_hotkeys[hotkey]]
                        ^^^^^^^^^^^^^^^
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\__init__.py", line 
649, in <lambda>
    handler = lambda e: (event_type == KEY_DOWN and e.event_type == KEY_UP and e.scan_code in _logically_pressed_keys) or (event_type == e.event_type and callback())

                                             ^^^^^^^^^^
  File "c:\Users\ricky\AppData\Local\Programs\Python\Python311\Lib\site-packages\keyboard\__init__.py", line 
637, in <lambda>
    callback = lambda callback=callback: callback(*args)
                                         ^^^^^^^^^^^^^^^
TypeError: __main__.moveArrow() argument after * must be an iterable, not int

代码应在同时按下 ctrl+shift+g 时键入“a”二十次,同时还阻止任何其他程序受到按钮按下的影响。相反,它两者都没有做,并且给了我一个错误。我也尝试过使用 args = (20) 但它旁边的 true 给了我一个错误。

python keyboard hotkeys
1个回答
0
投票

我需要使用可交互的,例如 (20,) 而不是 int。 感谢 juanpa.arrivilliga 的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.