【问题标题】:Invert mouse movement in python在python中反转鼠标移动
【发布时间】:2022-01-23 06:42:37
【问题描述】:

我一直在尝试编写一个反转鼠标移动的脚本。我有一个“正常运行”的脚本:

import mouse
    
last_pos = mouse.get_position()


def on_move(event):
    global last_pos

    if isinstance(event, mouse._mouse_event.MoveEvent):
        new_pos = ((last_pos[0] - event.x) * 2, (last_pos[1] - event.y) * 2)
        print()
        print(f'{last_pos = }')
        print(f'{new_pos = }')
        mouse.move(*new_pos, absolute=False)
        last_pos = mouse.get_position()
        print(f'*{last_pos = }')


mouse.hook(on_move)
input('Press enter to stop...')

虽然问题是,当我删除打印语句时,脚本不再起作用:

import mouse

last_pos = mouse.get_position()


def on_move(event):
    global last_pos

    if isinstance(event, mouse._mouse_event.MoveEvent):
        new_pos = ((last_pos[0] - event.x) * 2, (last_pos[1] - event.y) * 2)
        mouse.move(*new_pos, absolute=False)
        last_pos = mouse.get_position()


mouse.hook(on_move)
input('Press enter to stop...')

如果您知道我的问题的解决方案或更好的方法来做我想做的事情,请回复:D

【问题讨论】:

    标签: python python-3.x mouseevent mouse


    【解决方案1】:

    我不是 Python 方面的专家,但我认为 print 语句会减慢它的工作速度只是有点,因为很明显,当你删除 print 语句时它会发疯。所以我认为您可能需要保留打印语句才能使其正常工作。

    我测试了以下代码,去掉了打印语句:

    import mouse
    import asyncio
    
    last_pos = mouse.get_position()
    
    
    def on_move(event):
        global last_pos
    
        if isinstance(event, mouse._mouse_event.MoveEvent):
            new_pos = ((last_pos[0] - event.x) * 2, (last_pos[1] - event.y) * 2)
            asyncio.sleep(0)
            mouse.move(*new_pos, absolute=False)
            last_pos = mouse.get_position()
    
    mouse.hook(on_move)
    input('Press enter to stop...')
    

    ^^ 我认为这同样有效(或更好),但我不知道是否推荐。

    【讨论】:

    • Asyncio sleep 是协程,还能正常工作吗?
    • 它确实会发出警告,但它仍然有效
    • 鼠标移动真的有问题
    • 是的,这就是我所得到的,在这种情况下,我认为你应该坚持打印声明。
    猜你喜欢
    • 2023-03-08
    • 2018-01-27
    • 2015-11-27
    • 1970-01-01
    • 2018-11-03
    • 2023-03-30
    • 2017-07-02
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多