hidpi.mouse

The Mouse class provides methods to control a HID mouse device. It allows moving the cursor and simulating mouse clicks.

 1"""
 2The Mouse class provides methods to control a HID mouse device.
 3It allows moving the cursor and simulating mouse clicks.
 4"""
 5
 6import time
 7from .mouse_buttons import *
 8
 9MOUSE_DEVICE = "/dev/hidg1"
10
11class Mouse:
12    """
13    A class for controlling a HID-compliant mouse device.
14    """
15
16    @staticmethod
17    def move(x, y, wheel=0):
18        """
19        Moves the mouse cursor by the specified x and y offsets.
20        
21        :param x: The movement offset along the X-axis.
22        :type x: int
23        :param y: The movement offset along the Y-axis.
24        :type y: int
25        :param wheel: The scroll wheel movement.
26        :type wheel: int, optional
27        """
28        report = bytes([0, x & 0xFF, y & 0xFF, wheel & 0xFF])
29        Mouse._send_report(report)
30
31    @staticmethod
32    def click(button, hold=0):
33        """
34        Simulates a mouse button click.
35        
36        :param button: The button to click (e.g., left, right, middle button).
37        :type button: int
38        :param hold: Time in seconds to hold the button before releasing.
39        :type hold: float, optional
40        """
41        Mouse._send_report(bytes([button, 0, 0, 0]), hold)
42        Mouse._send_report(bytes([0, 0, 0, 0]), hold)
43
44    @staticmethod
45    def _send_report(report, hold=0):
46        """
47        Sends a raw HID report to the mouse device.
48        
49        :param report: The raw HID report data.
50        :type report: bytes
51        :param hold: Time in seconds to wait after sending the report.
52        :type hold: float, optional
53        """
54        with open(MOUSE_DEVICE, "rb+") as fd:
55            fd.write(report)
56            if hold:
57                time.sleep(hold)
58            fd.write(bytes([0, 0, 0, 0]))
MOUSE_DEVICE = '/dev/hidg1'
class Mouse:
12class Mouse:
13    """
14    A class for controlling a HID-compliant mouse device.
15    """
16
17    @staticmethod
18    def move(x, y, wheel=0):
19        """
20        Moves the mouse cursor by the specified x and y offsets.
21        
22        :param x: The movement offset along the X-axis.
23        :type x: int
24        :param y: The movement offset along the Y-axis.
25        :type y: int
26        :param wheel: The scroll wheel movement.
27        :type wheel: int, optional
28        """
29        report = bytes([0, x & 0xFF, y & 0xFF, wheel & 0xFF])
30        Mouse._send_report(report)
31
32    @staticmethod
33    def click(button, hold=0):
34        """
35        Simulates a mouse button click.
36        
37        :param button: The button to click (e.g., left, right, middle button).
38        :type button: int
39        :param hold: Time in seconds to hold the button before releasing.
40        :type hold: float, optional
41        """
42        Mouse._send_report(bytes([button, 0, 0, 0]), hold)
43        Mouse._send_report(bytes([0, 0, 0, 0]), hold)
44
45    @staticmethod
46    def _send_report(report, hold=0):
47        """
48        Sends a raw HID report to the mouse device.
49        
50        :param report: The raw HID report data.
51        :type report: bytes
52        :param hold: Time in seconds to wait after sending the report.
53        :type hold: float, optional
54        """
55        with open(MOUSE_DEVICE, "rb+") as fd:
56            fd.write(report)
57            if hold:
58                time.sleep(hold)
59            fd.write(bytes([0, 0, 0, 0]))

A class for controlling a HID-compliant mouse device.

@staticmethod
def move(x, y, wheel=0):
17    @staticmethod
18    def move(x, y, wheel=0):
19        """
20        Moves the mouse cursor by the specified x and y offsets.
21        
22        :param x: The movement offset along the X-axis.
23        :type x: int
24        :param y: The movement offset along the Y-axis.
25        :type y: int
26        :param wheel: The scroll wheel movement.
27        :type wheel: int, optional
28        """
29        report = bytes([0, x & 0xFF, y & 0xFF, wheel & 0xFF])
30        Mouse._send_report(report)

Moves the mouse cursor by the specified x and y offsets.

Parameters
  • x: The movement offset along the X-axis.
  • y: The movement offset along the Y-axis.
  • wheel: The scroll wheel movement.
@staticmethod
def click(button, hold=0):
32    @staticmethod
33    def click(button, hold=0):
34        """
35        Simulates a mouse button click.
36        
37        :param button: The button to click (e.g., left, right, middle button).
38        :type button: int
39        :param hold: Time in seconds to hold the button before releasing.
40        :type hold: float, optional
41        """
42        Mouse._send_report(bytes([button, 0, 0, 0]), hold)
43        Mouse._send_report(bytes([0, 0, 0, 0]), hold)

Simulates a mouse button click.

Parameters
  • button: The button to click (e.g., left, right, middle button).
  • hold: Time in seconds to hold the button before releasing.