如何使用 Python "mouse" 库记录鼠标的绝对位置



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

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

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

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)