为了账号安全,请及时绑定邮箱和手机立即绑定

如何记录鼠标移动,直到用 Python 按下一个键?

如何记录鼠标移动,直到用 Python 按下一个键?

绝地无双 2022-05-11 14:32:22
我想让函数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. 这可能吗?
查看完整描述

2 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

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


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


查看完整回答
反对 回复 2022-05-11
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

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

     


查看完整回答
反对 回复 2022-05-11
  • 2 回答
  • 0 关注
  • 265 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号