-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_controller.py
More file actions
89 lines (69 loc) · 3.25 KB
/
mouse_controller.py
File metadata and controls
89 lines (69 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pyautogui
import time
from utils import calculate_distance, smooth_coordinates
class MouseController:
def __init__(self, screen_width=None, screen_height=None, smoothing=True):
# Get screen dimensions
self.screen_width = screen_width or pyautogui.size().width
self.screen_height = screen_height or pyautogui.size().height
# Mouse control settings
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.001
# Smoothing
self.smoothing = smoothing
self.previous_position = None
# Click control
self.click_threshold = 50
self.last_click_time = 0
self.click_delay = 0.3 # Minimum time between clicks
def move_cursor(self, finger_positions, frame_width, frame_height):
"""Move cursor based on index finger position"""
if 'index' not in finger_positions:
return
index_tip = finger_positions['index']['tip']
# Convert finger position to screen coordinates
screen_x = int((index_tip[0] / frame_width) * self.screen_width)
screen_y = int((index_tip[1] / frame_height) * self.screen_height)
# Apply smoothing if enabled
if self.smoothing:
current_pos = (screen_x, screen_y)
smoothed_pos = smooth_coordinates(current_pos, self.previous_position)
screen_x, screen_y = int(smoothed_pos[0]), int(smoothed_pos[1])
self.previous_position = smoothed_pos
# Move cursor
pyautogui.moveTo(screen_x, screen_y)
def detect_click(self, finger_positions):
"""Detect click gesture based on thumb-index distance"""
if 'thumb' not in finger_positions or 'index' not in finger_positions:
return False
thumb_tip = finger_positions['thumb']['tip']
index_tip = finger_positions['index']['tip']
distance = calculate_distance(thumb_tip, index_tip)
return distance < self.click_threshold
def detect_right_click(self, finger_positions):
"""Detect right click gesture (thumb-middle finger)"""
if 'thumb' not in finger_positions or 'middle' not in finger_positions:
return False
thumb_tip = finger_positions['thumb']['tip']
middle_tip = finger_positions['middle']['tip']
distance = calculate_distance(thumb_tip, middle_tip)
return distance < self.click_threshold
def perform_click(self, click_type='left'):
"""Perform mouse click with delay protection"""
current_time = time.time()
if current_time - self.last_click_time > self.click_delay:
if click_type == 'left':
pyautogui.click()
elif click_type == 'right':
pyautogui.rightClick()
elif click_type == 'double':
pyautogui.doubleClick()
self.last_click_time = current_time
return True
return False
def scroll(self, finger_positions, direction='up', amount=3):
"""Perform scroll action"""
if direction == 'up':
pyautogui.scroll(amount)
else:
pyautogui.scroll(-amount)