如何使用Python“鼠标”记录鼠标的绝对位置图书馆

发布于 2025-01-19 12:44:39 字数 216 浏览 5 评论 0原文

我想使用mouse库来记录鼠标事件,但它的位置是相对的。如何设置记录仪记录绝对位置?

import mouse

events = mouse.record()  # record until clicking right button
mouse.play(events[:-1])  # skip right button clicked

I want to use mouse library to record mouse events, but its position is relative. How can I set the recorder to record the absolute position?

import mouse

events = mouse.record()  # record until clicking right button
mouse.play(events[:-1])  # skip right button clicked

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-01-26 12:44:39

这是解决您的问题的一种方法:

import mouse
import pyautogui

def record_mouse_events():
    events = []
    initial_position = pyautogui.position()

    def on_event(event):                           
        # Check if the right mouse button is clicked (event.button == "right")
        if event.event_type == "down" and event.button == "right":
            mouse.unhook(on_event)  # Stop recording when right mouse button is clicked
        else:
            absolute_position = (initial_position[0] + event.x, initial_position[1] + event.y) 
            events.append(mouse.MouseEvent(event.event_type, absolute_position, event.button))

    mouse.hook(on_event)            
    return events

recorded_events = record_mouse_events()

for event in recorded_events:
    print(event)

This is one way to solve your problem :

import mouse
import pyautogui

def record_mouse_events():
    events = []
    initial_position = pyautogui.position()

    def on_event(event):                           
        # Check if the right mouse button is clicked (event.button == "right")
        if event.event_type == "down" and event.button == "right":
            mouse.unhook(on_event)  # Stop recording when right mouse button is clicked
        else:
            absolute_position = (initial_position[0] + event.x, initial_position[1] + event.y) 
            events.append(mouse.MouseEvent(event.event_type, absolute_position, event.button))

    mouse.hook(on_event)            
    return events

recorded_events = record_mouse_events()

for event in recorded_events:
    print(event)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文