gpt4 book ai didi

python - 热键的输出与预期不同

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:12 26 4
gpt4 key购买 nike

我是制作 python 热键的新手。我正在尝试使用 python pypi 键盘模块,并且正在尝试让 lambda 函数识别键盘条目。我还添加了一个打印语句来查看是否有效。 print 语句有效,但之后键盘输入返回无值。我如何让 lambda 函数识别这部分代码? 感谢您的帮助。

我尝试过打印、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'))

关于python - 热键的输出与预期不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com