热键的输出与预期的不同

时间:2019-08-05 02:27:32

标签: python keyboard hotkeys

我是制作python热键的新手。我正在尝试使用python pypi键盘模块,并且正在尝试获取lambda函数来识别键盘条目。我还提供了一份印刷声明,看是否可行。 print语句有效,但之后键盘输入将返回none值。我如何获得lambda函数来识别代码的这一部分?  谢谢你的帮助。

我尝试了print,keyboard.write,并且尝试通过keyboard.press和keyboard.press_and_release执行此操作。

import keyboard


combination_to_function = {
    (keyboard.add_hotkey('ctrl+shift+z', lambda: print("hotkey complete", keyboard.press('m')))), 
    (keyboard.add_hotkey('ctrl+shift+x', lambda: print("hotkey complete", keyboard.press('f')))),
    (keyboard.add_hotkey('ctrl+shift+v', lambda: print("hotkey complete", keyboard.press('o')))),
    (keyboard.add_hotkey('ctrl+shift+d', lambda: print("hotkey complete", keyboard.press('k'))))
}

# Currently pressed keys
current_keys = set()

def on_press(key):
    # When a key is pressed, add it to the set we are keeping track of and check if this set is in the dictionary
    current_keys.add(key)
    if frozenset(current_keys) in combination_to_function:
        # If the current set of keys are in the mapping, execute the function
        combination_to_function[frozenset(current_keys)]()

def on_release(key):
    # When a key is released, remove it from the set of keys we are keeping track of
    current_keys.remove(key)

keyboard.wait()

结果为“热键完成”,无。我希望完成热键加上按下的字母

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解问题,但是可以创建函数

def my_press(key):
    keyboard.press(key)
    print("hotkey complete", key)

然后使用

keyboard.add_hotkey('ctrl+shift+z', lambda: my_press('m'))