I have tried the following code to detect mouse events and also its associated device id / vendor id etc. The value for device id is always 0 for any mouse except the trackpad. Is it possible to find which mouse was used for a mouse event on Mac.
from AppKit import NSEvent, NSApplication, NSLeftMouseDownMask, NSLeftMouseUpMask, NSMouseMovedMask
class MouseEventHandler:
def __init__(self):
self.app = NSApplication.sharedApplication()
mask = NSLeftMouseDownMask | NSLeftMouseUpMask | NSMouseMovedMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.event_handler)
def event_handler(self, event):
event_type = event.type()
device_id = event.deviceID()
if event_type == NSLeftMouseDownMask:
print(f"Mouse Clicked (Left) on device {device_id}")
elif event_type == NSLeftMouseUpMask:
print(f"Mouse Released (Left) on device {device_id}")
elif event_type == NSMouseMovedMask:
print(f"Mouse Moved on device {device_id}")
if __name__ == "__main__":
handler = MouseEventHandler()
NSApplication.sharedApplication().run()
Current Output
Mouse Clicked (Left) on device 5044031582654957 # for touch pad
Mouse Clicked (Left) on device 0. # for mouse 1
Mouse Clicked (Left) on device 0. # for mouse 2
Is there a way to distinguish between different devices on mac.