如何记录鼠标移动,直到用 Python 按下一个键?
How to record the mouse movement until a key is pressed with Python?
我想使函数 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
记录鼠标移动直到键盘事件的功能。
这可能吗?
您可以合并两者而不会弄乱模块文件:
1) 使用 mouse.hook()
无需等待即可记录事件(就像 mouse.record()
发生的那样)。它需要一个函数,return 它就是那个事件。
2)使用keyboard.wait(key)
等待按键被按下
3) 使用mouse.unhook()
停止录制。
这是一个示例代码:
import mouse
import keyboard
events = [] #This is the list where all the events will be stored
mouse.hook(events.append) #starting the recording
keyboard.wait("a") #Waiting for 'a' to be pressed
mouse.unhook(events.append) #Stopping the recording
您还可以使用线程将键盘和鼠标连接在一起。以下代码将记录鼠标事件和键盘事件,然后回放。您可以按 Escape 按钮停止监控。
import threading
import mouse
import keyboard
def monitorMouseKeyboardEvents():
#These are the list where all the events will be stored
mouse_events = []
keyboard_events = []
#Start recording
mouse.hook(mouse_events.append) #starting the mouse recording
# keyboard.hook(lambda _: keyboard_events.append(_))
keyboard.start_recording()
keyboard.wait("esc") #Waiting for 'Esc' button to be pressed
#Stopping recording
mouse.unhook(mouse_events.append)
# keyboard.unhook(keyboard_events.append)
keyboard_events = keyboard.stop_recording()
return mouse_events, keyboard_events
def playMouseMouseKeyboardEvents(mouse_events, keyboard_events):
'''
Playing the recorded events at the same time
'''
k_thread = threading.Thread(target = lambda :keyboard.play(keyboard_events))
k_thread.start()
#Mouse threadings:
m_thread = threading.Thread(target = lambda :mouse.play(mouse_events))
m_thread.start()
#waiting for both threadings to be completed
k_thread.join()
m_thread.join()
if __name__ == "__main__":
mouse_events, keyboard_events=monitorMouseKeyboardEvents()
playMouseMouseKeyboardEvents(mouse_events, keyboard_events)
我想使函数 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
记录鼠标移动直到键盘事件的功能。
这可能吗?
您可以合并两者而不会弄乱模块文件:
1) 使用 mouse.hook()
无需等待即可记录事件(就像 mouse.record()
发生的那样)。它需要一个函数,return 它就是那个事件。
2)使用keyboard.wait(key)
等待按键被按下
3) 使用mouse.unhook()
停止录制。
这是一个示例代码:
import mouse
import keyboard
events = [] #This is the list where all the events will be stored
mouse.hook(events.append) #starting the recording
keyboard.wait("a") #Waiting for 'a' to be pressed
mouse.unhook(events.append) #Stopping the recording
您还可以使用线程将键盘和鼠标连接在一起。以下代码将记录鼠标事件和键盘事件,然后回放。您可以按 Escape 按钮停止监控。
import threading
import mouse
import keyboard
def monitorMouseKeyboardEvents():
#These are the list where all the events will be stored
mouse_events = []
keyboard_events = []
#Start recording
mouse.hook(mouse_events.append) #starting the mouse recording
# keyboard.hook(lambda _: keyboard_events.append(_))
keyboard.start_recording()
keyboard.wait("esc") #Waiting for 'Esc' button to be pressed
#Stopping recording
mouse.unhook(mouse_events.append)
# keyboard.unhook(keyboard_events.append)
keyboard_events = keyboard.stop_recording()
return mouse_events, keyboard_events
def playMouseMouseKeyboardEvents(mouse_events, keyboard_events):
'''
Playing the recorded events at the same time
'''
k_thread = threading.Thread(target = lambda :keyboard.play(keyboard_events))
k_thread.start()
#Mouse threadings:
m_thread = threading.Thread(target = lambda :mouse.play(mouse_events))
m_thread.start()
#waiting for both threadings to be completed
k_thread.join()
m_thread.join()
if __name__ == "__main__":
mouse_events, keyboard_events=monitorMouseKeyboardEvents()
playMouseMouseKeyboardEvents(mouse_events, keyboard_events)