11"""
2- This is a sample class that you can use to control the mouse pointer.
3- It uses the pyautogui library. You can set the precision for mouse movement
4- (how much the mouse moves) and the speed (how fast it moves) by changing
5- precision_dict and speed_dict.
6- Calling the move function with the x and y output of the gaze estimation model
7- will move the pointer.
8- This class is provided to help get you started; you can choose whether you want to use it or create your own from scratch.
2+ Mouse controller utilities built on top of pyautogui.
3+
4+ Provides a lightweight MouseController class to drive the system pointer using
5+ relative movements that are scaled by a precision factor and animated over a
6+ configurable duration. This is useful for driving the pointer from gaze- or
7+ pose-estimation outputs where you receive small x, y offsets.
8+
9+ Constructor presets (string keys accepted):
10+ - precision: "high" (100), "medium" (500), "low" (1000)
11+ - speed: "fast" (1), "medium" (5), "slow" (10)
12+
13+ API summary:
14+ - MouseController.move(x, y)
15+ Move the pointer by (x * precision, -y * precision). Note the y value is
16+ negated internally to accommodate screen coordinate orientation.
17+ - MouseController.move_to_center()
18+ Move the pointer to the center of the primary screen using the same scaling.
19+
20+ Notes and requirements:
21+ - This module disables pyautogui.FAILSAFE by default. Re-enable it if you want
22+ the corner-escape behavior.
23+ - Install pyautogui before use.
24+ - Interpret x and y according to your gaze model (e.g., normalized offsets,
25+ pixel deltas, etc.) and tune precision/speed presets accordingly.
26+
27+ Example:
28+ mc = MouseController(precision="medium", speed="fast")
29+ mc.move(0.1, -0.05) # relative movement scaled by precision
930"""
1031
1132import pyautogui
@@ -25,3 +46,16 @@ def move(self, x, y):
2546 pyautogui .moveRel (
2647 x * self .precision , - 1 * y * self .precision , duration = self .speed
2748 )
49+
50+ def move_to_center (self ):
51+ w , h = pyautogui .size ()
52+ cx , cy = w // 2 , h // 2
53+ cur_x , cur_y = pyautogui .position ()
54+
55+ # required inputs for mc.move
56+ dx = (cx - cur_x ) / self .precision
57+ dy = (
58+ cur_y - cy
59+ ) / self .precision # invert because MouseController.move negates y internally
60+
61+ self .move (dx , dy )
0 commit comments