【发布时间】:2021-04-02 11:41:47
【问题描述】:
我想让函数mouse.record() 运行直到按下一个键,而不是鼠标按钮。
mouse.record() 是 python 模块 mouse 中的一个函数:
(mouse/__init__.py)
def record(button=RIGHT, target_types=(DOWN,)):
"""
Records all mouse events until the user presses the given button. Then returns the list of events recorded. Pairs well with `play(events)`.
Note: this is a blocking function. Note: for more details on the mouse hook and events see `hook`.
"""
recorded = []
hook(recorded.append)
wait(button=button, target_types=target_types)
unhook(recorded.append)
return recorded
我想我可以将mouse 模块与keyboard 模块合并,以实现记录鼠标移动直到键盘事件的功能。有一个类似的键盘功能可能很方便:
(keyboard/__init__.py)
def record(until='escape', suppress=False, trigger_on_release=False):
"""
Records all keyboard events from all keyboards until the user presses the given hotkey. Then returns the list of events recorded, of type `keyboard.KeyboardEvent`. Pairs well with `play(events)`.
Note: this is a blocking function. Note: for more details on the keyboard hook and events see `hook`.
"""
start_recording()
wait(until, suppress=suppress, trigger_on_release=trigger_on_release)
return stop_recording()
所以,总而言之,我想要实现的是使用 Python 模块 mouse 和 keyboard 记录鼠标移动直到键盘事件发生的功能。
这可能吗?
【问题讨论】:
-
您可能想查看“鼠标”或“键盘”的实现,因为我怀疑您需要自己挂钩系统事件而不是使用库。
-
@MichaelD 不需要创建系统事件。检查我的答案