【问题标题】:How to record the mouse movement until a key is pressed with Python?如何记录鼠标移动,直到用 Python 按下一个键?
【发布时间】: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 模块 mousekeyboard 记录鼠标移动直到键盘事件发生的功能。 这可能吗?

【问题讨论】:

  • 您可能想查看“鼠标”或“键盘”的实现,因为我怀疑您需要自己挂钩系统事件而不是使用库。
  • @MichaelD 不需要创建系统事件。检查我的答案

标签: python keyboard mouse


【解决方案1】:

您可以合并两者而不会弄乱模块文件:

1) 使用 mouse.hook() 无需等待即可记录事件(就像使用 mouse.record() 一样)。它接受一个函数并将该事件返回给它。
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

【讨论】:

    【解决方案2】:

    您还可以使用线程将键盘和鼠标连接在一起。以下代码将记录鼠标事件和键盘事件,然后回放。您可以按 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)
         
    

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多