簡體   English   中英

熱鍵的輸出與預期的不同

[英]output for hotkeys is different than expected

我是制作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()

結果是“熱鍵完成”,沒有。 我希望完成熱鍵加上按下的字母

我不確定我是否理解問題,但是可以創建函數

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

然后用它

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM