热键的输出与预期不同
Posted
技术标签:
【中文标题】热键的输出与预期不同【英文标题】:output for hotkeys is different than expected 【发布时间】:2019-12-12 13:32:07 【问题描述】:我是制作 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()
结果是“热键完成”无。我希望得到完整的热键和按下的字母
【问题讨论】:
print("hotkey complete m")
press()
将密钥发送到活动窗口,但这并不意味着它将值返回给print()
【参考方案1】:
我不确定我是否理解问题,但你可以创建函数
def my_press(key):
keyboard.press(key)
print("hotkey complete", key)
然后使用它
keyboard.add_hotkey('ctrl+shift+z', lambda: my_press('m'))
【讨论】:
以上是关于热键的输出与预期不同的主要内容,如果未能解决你的问题,请参考以下文章