pyautoclick 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyautoclick/__init__.py +3 -0
- pyautoclick/__main__.py +95 -0
- pyautoclick/assets/__init__.py +0 -0
- pyautoclick/assets/py-autoclick.png +0 -0
- pyautoclick/core/__init__.py +0 -0
- pyautoclick/core/buttons.py +25 -0
- pyautoclick/core/clicker.py +305 -0
- pyautoclick/core/hotkeys.py +106 -0
- pyautoclick/core/platform_capabilities.py +96 -0
- pyautoclick/core/single_instance.py +169 -0
- pyautoclick/i18n/__init__.py +18 -0
- pyautoclick/i18n/core.py +218 -0
- pyautoclick/i18n/direction.py +49 -0
- pyautoclick/i18n/locales/__init__.py +0 -0
- pyautoclick/i18n/locales/ar.json +101 -0
- pyautoclick/i18n/locales/de.json +99 -0
- pyautoclick/i18n/locales/en.json +98 -0
- pyautoclick/i18n/locales/es.json +99 -0
- pyautoclick/i18n/locales/fr.json +98 -0
- pyautoclick/i18n/locales/he.json +101 -0
- pyautoclick/i18n/locales/it.json +99 -0
- pyautoclick/i18n/locales/ja.json +100 -0
- pyautoclick/i18n/locales/pt.json +99 -0
- pyautoclick/i18n/locales/ru.json +100 -0
- pyautoclick/i18n/locales/zh.json +100 -0
- pyautoclick/logging_config.py +84 -0
- pyautoclick/paths.py +54 -0
- pyautoclick/persistence/__init__.py +0 -0
- pyautoclick/persistence/migrations.py +41 -0
- pyautoclick/persistence/settings.py +251 -0
- pyautoclick/services/__init__.py +0 -0
- pyautoclick/services/notifications.py +96 -0
- pyautoclick/ui/__init__.py +0 -0
- pyautoclick/ui/app.py +314 -0
- pyautoclick/ui/colors.py +26 -0
- pyautoclick/ui/constants.py +72 -0
- pyautoclick/ui/context.py +104 -0
- pyautoclick/ui/status_bar.py +73 -0
- pyautoclick/ui/tabs/__init__.py +11 -0
- pyautoclick/ui/tabs/auto.py +161 -0
- pyautoclick/ui/tabs/base.py +98 -0
- pyautoclick/ui/tabs/hold.py +144 -0
- pyautoclick/ui/tabs/hotkeys.py +125 -0
- pyautoclick/ui/tabs/positions.py +88 -0
- pyautoclick/ui/tabs/test.py +62 -0
- pyautoclick/ui/theme.py +62 -0
- pyautoclick/ui/widgets/__init__.py +0 -0
- pyautoclick/ui/widgets/capture_dialog.py +86 -0
- pyautoclick/ui/widgets/hotkey_entry.py +43 -0
- pyautoclick/ui/widgets/tooltip.py +58 -0
- pyautoclick-0.3.0.data/data/share/icons/hicolor/256x256/apps/py-autoclick.png +0 -0
- pyautoclick-0.3.0.dist-info/METADATA +928 -0
- pyautoclick-0.3.0.dist-info/RECORD +56 -0
- pyautoclick-0.3.0.dist-info/WHEEL +4 -0
- pyautoclick-0.3.0.dist-info/entry_points.txt +2 -0
- pyautoclick-0.3.0.dist-info/licenses/LICENSE +674 -0
pyautoclick/__init__.py
ADDED
pyautoclick/__main__.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Entry point: ``python -m pyautoclick`` or via the ``pyautoclick`` script."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import signal
|
|
7
|
+
import sys
|
|
8
|
+
import tkinter as tk
|
|
9
|
+
from tkinter import messagebox
|
|
10
|
+
from types import FrameType
|
|
11
|
+
|
|
12
|
+
from pyautoclick.core.platform_capabilities import detect as detect_platform
|
|
13
|
+
from pyautoclick.core.platform_capabilities import log_summary as log_platform
|
|
14
|
+
from pyautoclick.core.single_instance import (
|
|
15
|
+
acquire_single_instance_lock,
|
|
16
|
+
signal_existing_instance,
|
|
17
|
+
start_focus_server,
|
|
18
|
+
)
|
|
19
|
+
from pyautoclick.i18n import LANGUAGES, detect_system_language, t
|
|
20
|
+
from pyautoclick.logging_config import configure_logging
|
|
21
|
+
from pyautoclick.paths import ICON_PATH
|
|
22
|
+
from pyautoclick.persistence.settings import Settings
|
|
23
|
+
from pyautoclick.ui.app import App
|
|
24
|
+
from pyautoclick.ui.theme import bring_to_front
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _show_already_running_dialog() -> None:
|
|
30
|
+
"""Best-effort localized warning before exiting (used as a fallback)."""
|
|
31
|
+
try:
|
|
32
|
+
s = Settings.load()
|
|
33
|
+
lang = s.language or detect_system_language()
|
|
34
|
+
except (OSError, ValueError):
|
|
35
|
+
lang = detect_system_language()
|
|
36
|
+
if lang not in LANGUAGES:
|
|
37
|
+
lang = "en"
|
|
38
|
+
root = tk.Tk()
|
|
39
|
+
root.withdraw()
|
|
40
|
+
messagebox.showwarning(t("app_title", lang),
|
|
41
|
+
t("instance_already_running", lang))
|
|
42
|
+
root.destroy()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def main() -> None:
|
|
46
|
+
configure_logging()
|
|
47
|
+
logger.info("py-autoclick starting up")
|
|
48
|
+
capabilities = detect_platform()
|
|
49
|
+
log_platform(capabilities)
|
|
50
|
+
|
|
51
|
+
# Single-instance enforcement
|
|
52
|
+
lock = acquire_single_instance_lock()
|
|
53
|
+
if lock is None:
|
|
54
|
+
logger.info("another instance already running")
|
|
55
|
+
if signal_existing_instance():
|
|
56
|
+
logger.info("focus request delivered to existing instance, exiting")
|
|
57
|
+
sys.exit(0)
|
|
58
|
+
logger.warning("existing instance is unreachable (zombie?); showing dialog")
|
|
59
|
+
_show_already_running_dialog()
|
|
60
|
+
sys.exit(0)
|
|
61
|
+
|
|
62
|
+
root = tk.Tk(className="PyAutoClick")
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
icon = tk.PhotoImage(file=str(ICON_PATH))
|
|
66
|
+
root.iconphoto(True, icon)
|
|
67
|
+
root._icon_ref = icon # keep ref to avoid GC
|
|
68
|
+
except tk.TclError:
|
|
69
|
+
logger.warning("could not load window icon from %s", ICON_PATH, exc_info=True)
|
|
70
|
+
|
|
71
|
+
app = App(root, capabilities=capabilities)
|
|
72
|
+
# Listen for second-instance focus requests (run on socket thread,
|
|
73
|
+
# marshaled to UI thread via root.after)
|
|
74
|
+
start_focus_server(lambda: root.after(0, lambda: bring_to_front(root)))
|
|
75
|
+
|
|
76
|
+
# SIGINT (Ctrl+C in terminal) and SIGTERM (kill / logout) trigger a
|
|
77
|
+
# clean shutdown via the existing WM_DELETE_WINDOW handler.
|
|
78
|
+
def _signal_handler(signum: int, _frame: FrameType | None) -> None:
|
|
79
|
+
name = signal.Signals(signum).name
|
|
80
|
+
logger.info("received %s, requesting graceful shutdown", name)
|
|
81
|
+
# Tk handlers must run on the main thread; root.after schedules safely
|
|
82
|
+
root.after(0, app._on_close)
|
|
83
|
+
|
|
84
|
+
signal.signal(signal.SIGINT, _signal_handler)
|
|
85
|
+
signal.signal(signal.SIGTERM, _signal_handler)
|
|
86
|
+
|
|
87
|
+
logger.info("UI ready, entering mainloop")
|
|
88
|
+
try:
|
|
89
|
+
root.mainloop()
|
|
90
|
+
finally:
|
|
91
|
+
logger.info("mainloop exited cleanly")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if __name__ == "__main__":
|
|
95
|
+
main()
|
|
File without changes
|
|
Binary file
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Mouse button identifiers.
|
|
2
|
+
|
|
3
|
+
Stable language-agnostic keys -> pynput Button objects.
|
|
4
|
+
Combobox labels are resolved via the i18n layer (translation keys
|
|
5
|
+
``trigger_<key>`` and ``action_<key>``).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pynput.mouse import Button
|
|
11
|
+
|
|
12
|
+
TRIGGER_KEYS = ["button8", "button9", "middle", "right"]
|
|
13
|
+
TRIGGER_BUTTONS = {
|
|
14
|
+
"button8": Button.button8,
|
|
15
|
+
"button9": Button.button9,
|
|
16
|
+
"middle": Button.middle,
|
|
17
|
+
"right": Button.right,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
ACTION_KEYS = ["left", "right", "middle"]
|
|
21
|
+
ACTION_BUTTONS = {
|
|
22
|
+
"left": Button.left,
|
|
23
|
+
"right": Button.right,
|
|
24
|
+
"middle": Button.middle,
|
|
25
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"""Click engine: hold mode + auto mode running on background threads.
|
|
2
|
+
|
|
3
|
+
Thread safety
|
|
4
|
+
-------------
|
|
5
|
+
All mutable state shared between the listener / loop threads and the UI
|
|
6
|
+
thread is protected by ``self.lock``. Setters used from the UI must go
|
|
7
|
+
through the explicit setter methods (``set_*``); the loops snapshot values
|
|
8
|
+
under the lock at the start of each iteration to avoid torn reads.
|
|
9
|
+
|
|
10
|
+
Lifecycle
|
|
11
|
+
---------
|
|
12
|
+
The mouse ``Listener`` handle is captured so it can be ``stop()``-ed at
|
|
13
|
+
shutdown via :meth:`shutdown`. The two loop threads are daemons; they exit
|
|
14
|
+
when the interpreter does, but :meth:`shutdown` also clears their wait
|
|
15
|
+
events to give them a chance to exit cleanly first.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import logging
|
|
21
|
+
import random
|
|
22
|
+
import threading
|
|
23
|
+
import time
|
|
24
|
+
|
|
25
|
+
from pynput import mouse
|
|
26
|
+
from pynput.mouse import Button, Controller
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger(__name__)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def jittered(value_ms: float, jitter_ms: float) -> float:
|
|
32
|
+
if jitter_ms <= 0:
|
|
33
|
+
return max(0, value_ms)
|
|
34
|
+
return max(0, value_ms + random.uniform(-jitter_ms, jitter_ms))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AutoClicker:
|
|
38
|
+
"""Two background loops: hold (gated by mouse trigger) and auto (free-running)."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, on_button_seen=None, on_auto_stopped=None):
|
|
41
|
+
self.controller = Controller()
|
|
42
|
+
self.lock = threading.RLock() # all mutable state below
|
|
43
|
+
|
|
44
|
+
# Mode maintenu
|
|
45
|
+
self.hold_enabled = threading.Event()
|
|
46
|
+
self.hold_enabled.set()
|
|
47
|
+
self.holding = threading.Event()
|
|
48
|
+
self._cps = 15
|
|
49
|
+
self._hold_jitter_ms = 0
|
|
50
|
+
self._trigger = Button.button8
|
|
51
|
+
self._click_btn = Button.left
|
|
52
|
+
|
|
53
|
+
# Mode automatique
|
|
54
|
+
self.auto_running = threading.Event()
|
|
55
|
+
self._auto_btn = Button.left
|
|
56
|
+
self._auto_interval_ms = 100
|
|
57
|
+
self._auto_press_ms = 10
|
|
58
|
+
self._auto_jitter_ms = 0
|
|
59
|
+
self._auto_click_limit = 0
|
|
60
|
+
self._auto_session_count = 0
|
|
61
|
+
self._use_positions = False
|
|
62
|
+
self._positions: list[tuple[int, int]] = []
|
|
63
|
+
self._pos_idx = 0
|
|
64
|
+
|
|
65
|
+
self._click_count = 0
|
|
66
|
+
|
|
67
|
+
self.on_button_seen = on_button_seen
|
|
68
|
+
self.on_auto_stopped = on_auto_stopped
|
|
69
|
+
|
|
70
|
+
# Lifecycle
|
|
71
|
+
self._stop_event = threading.Event()
|
|
72
|
+
self._mouse_listener = mouse.Listener(on_click=self._on_click)
|
|
73
|
+
self._mouse_listener.daemon = True
|
|
74
|
+
self._mouse_listener.start()
|
|
75
|
+
self._hold_thread = threading.Thread(
|
|
76
|
+
target=self._hold_loop, name="autoclicker-hold", daemon=True,
|
|
77
|
+
)
|
|
78
|
+
self._auto_thread = threading.Thread(
|
|
79
|
+
target=self._auto_loop, name="autoclicker-auto", daemon=True,
|
|
80
|
+
)
|
|
81
|
+
self._hold_thread.start()
|
|
82
|
+
self._auto_thread.start()
|
|
83
|
+
|
|
84
|
+
# ------------------------------------------------------------------
|
|
85
|
+
# Read-only properties (locked snapshots) for UI tick
|
|
86
|
+
# ------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def click_count(self) -> int:
|
|
90
|
+
with self.lock:
|
|
91
|
+
return self._click_count
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def auto_session_count(self) -> int:
|
|
95
|
+
with self.lock:
|
|
96
|
+
return self._auto_session_count
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def auto_click_limit(self) -> int:
|
|
100
|
+
with self.lock:
|
|
101
|
+
return self._auto_click_limit
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def positions(self) -> list[tuple[int, int]]:
|
|
105
|
+
# Returns a snapshot; mutating it doesn't affect the engine.
|
|
106
|
+
# Use ``set_positions``/``add_position``/``clear_positions`` instead.
|
|
107
|
+
with self.lock:
|
|
108
|
+
return list(self._positions)
|
|
109
|
+
|
|
110
|
+
# ------------------------------------------------------------------
|
|
111
|
+
# Loops
|
|
112
|
+
# ------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
def _hold_loop(self) -> None:
|
|
115
|
+
while not self._stop_event.is_set():
|
|
116
|
+
self.holding.wait()
|
|
117
|
+
if self._stop_event.is_set():
|
|
118
|
+
return
|
|
119
|
+
with self.lock:
|
|
120
|
+
btn = self._click_btn
|
|
121
|
+
cps = self._cps
|
|
122
|
+
jitter = self._hold_jitter_ms
|
|
123
|
+
self.controller.click(btn)
|
|
124
|
+
with self.lock:
|
|
125
|
+
self._click_count += 1
|
|
126
|
+
base = 1000.0 / max(1, cps)
|
|
127
|
+
time.sleep(jittered(base, jitter) / 1000.0)
|
|
128
|
+
|
|
129
|
+
def _auto_loop(self) -> None:
|
|
130
|
+
while not self._stop_event.is_set():
|
|
131
|
+
self.auto_running.wait()
|
|
132
|
+
if self._stop_event.is_set():
|
|
133
|
+
return
|
|
134
|
+
|
|
135
|
+
with self.lock:
|
|
136
|
+
use_positions = self._use_positions
|
|
137
|
+
positions_snapshot = list(self._positions)
|
|
138
|
+
pos_idx = self._pos_idx
|
|
139
|
+
btn = self._auto_btn
|
|
140
|
+
press_ms = self._auto_press_ms
|
|
141
|
+
jitter = self._auto_jitter_ms
|
|
142
|
+
interval_ms = self._auto_interval_ms
|
|
143
|
+
limit = self._auto_click_limit
|
|
144
|
+
|
|
145
|
+
if use_positions and positions_snapshot:
|
|
146
|
+
pos = positions_snapshot[pos_idx % len(positions_snapshot)]
|
|
147
|
+
with self.lock:
|
|
148
|
+
self._pos_idx += 1
|
|
149
|
+
try:
|
|
150
|
+
self.controller.position = pos
|
|
151
|
+
except (ValueError, OSError):
|
|
152
|
+
logger.exception("failed to move cursor to %r", pos)
|
|
153
|
+
|
|
154
|
+
self.controller.press(btn)
|
|
155
|
+
time.sleep(jittered(press_ms, jitter) / 1000.0)
|
|
156
|
+
self.controller.release(btn)
|
|
157
|
+
|
|
158
|
+
with self.lock:
|
|
159
|
+
self._click_count += 1
|
|
160
|
+
self._auto_session_count += 1
|
|
161
|
+
reached = limit > 0 and self._auto_session_count >= limit
|
|
162
|
+
|
|
163
|
+
if reached:
|
|
164
|
+
self.auto_running.clear()
|
|
165
|
+
if self.on_auto_stopped:
|
|
166
|
+
self.on_auto_stopped(reason="limit")
|
|
167
|
+
continue
|
|
168
|
+
|
|
169
|
+
time.sleep(jittered(interval_ms, jitter) / 1000.0)
|
|
170
|
+
|
|
171
|
+
# ------------------------------------------------------------------
|
|
172
|
+
# Mouse listener
|
|
173
|
+
# ------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
def _on_click(self, x, y, button, pressed):
|
|
176
|
+
if self.on_button_seen:
|
|
177
|
+
try:
|
|
178
|
+
self.on_button_seen(x, y, button, pressed)
|
|
179
|
+
except Exception: # never let a UI callback crash the listener
|
|
180
|
+
logger.exception("on_button_seen callback raised")
|
|
181
|
+
if not self.hold_enabled.is_set():
|
|
182
|
+
self.holding.clear()
|
|
183
|
+
return
|
|
184
|
+
with self.lock:
|
|
185
|
+
trigger = self._trigger
|
|
186
|
+
if button == trigger:
|
|
187
|
+
if pressed:
|
|
188
|
+
self.holding.set()
|
|
189
|
+
else:
|
|
190
|
+
self.holding.clear()
|
|
191
|
+
|
|
192
|
+
# ------------------------------------------------------------------
|
|
193
|
+
# Setters (UI-thread)
|
|
194
|
+
# ------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
def set_trigger(self, btn: Button) -> None:
|
|
197
|
+
with self.lock:
|
|
198
|
+
self._trigger = btn
|
|
199
|
+
self.holding.clear()
|
|
200
|
+
|
|
201
|
+
def set_click_btn(self, btn: Button) -> None:
|
|
202
|
+
with self.lock:
|
|
203
|
+
self._click_btn = btn
|
|
204
|
+
|
|
205
|
+
def set_cps(self, cps: int) -> None:
|
|
206
|
+
with self.lock:
|
|
207
|
+
self._cps = cps
|
|
208
|
+
|
|
209
|
+
def set_hold_jitter_ms(self, ms: int) -> None:
|
|
210
|
+
with self.lock:
|
|
211
|
+
self._hold_jitter_ms = ms
|
|
212
|
+
|
|
213
|
+
def set_auto_btn(self, btn: Button) -> None:
|
|
214
|
+
with self.lock:
|
|
215
|
+
self._auto_btn = btn
|
|
216
|
+
|
|
217
|
+
def set_auto_interval_ms(self, ms: int) -> None:
|
|
218
|
+
with self.lock:
|
|
219
|
+
self._auto_interval_ms = ms
|
|
220
|
+
|
|
221
|
+
def set_auto_press_ms(self, ms: int) -> None:
|
|
222
|
+
with self.lock:
|
|
223
|
+
self._auto_press_ms = ms
|
|
224
|
+
|
|
225
|
+
def set_auto_jitter_ms(self, ms: int) -> None:
|
|
226
|
+
with self.lock:
|
|
227
|
+
self._auto_jitter_ms = ms
|
|
228
|
+
|
|
229
|
+
def set_auto_click_limit(self, n: int) -> None:
|
|
230
|
+
with self.lock:
|
|
231
|
+
self._auto_click_limit = n
|
|
232
|
+
|
|
233
|
+
def set_use_positions(self, on: bool) -> None:
|
|
234
|
+
with self.lock:
|
|
235
|
+
self._use_positions = on
|
|
236
|
+
|
|
237
|
+
def set_positions(self, positions: list[tuple[int, int]]) -> None:
|
|
238
|
+
with self.lock:
|
|
239
|
+
self._positions = list(positions)
|
|
240
|
+
self._pos_idx = 0
|
|
241
|
+
|
|
242
|
+
def add_position(self, pos: tuple[int, int]) -> None:
|
|
243
|
+
with self.lock:
|
|
244
|
+
self._positions.append(pos)
|
|
245
|
+
|
|
246
|
+
def remove_position(self, idx: int) -> None:
|
|
247
|
+
with self.lock:
|
|
248
|
+
if 0 <= idx < len(self._positions):
|
|
249
|
+
del self._positions[idx]
|
|
250
|
+
|
|
251
|
+
def clear_positions(self) -> None:
|
|
252
|
+
with self.lock:
|
|
253
|
+
self._positions.clear()
|
|
254
|
+
self._pos_idx = 0
|
|
255
|
+
|
|
256
|
+
def set_hold_enabled(self, on: bool) -> None:
|
|
257
|
+
if on:
|
|
258
|
+
self.hold_enabled.set()
|
|
259
|
+
else:
|
|
260
|
+
self.hold_enabled.clear()
|
|
261
|
+
self.holding.clear()
|
|
262
|
+
|
|
263
|
+
def set_auto(self, on: bool, *, reset_session: bool = True) -> None:
|
|
264
|
+
if on:
|
|
265
|
+
if reset_session:
|
|
266
|
+
with self.lock:
|
|
267
|
+
self._auto_session_count = 0
|
|
268
|
+
self._pos_idx = 0
|
|
269
|
+
self.auto_running.set()
|
|
270
|
+
else:
|
|
271
|
+
was = self.auto_running.is_set()
|
|
272
|
+
self.auto_running.clear()
|
|
273
|
+
if was and self.on_auto_stopped:
|
|
274
|
+
self.on_auto_stopped(reason="user")
|
|
275
|
+
|
|
276
|
+
def reset_click_count(self) -> None:
|
|
277
|
+
with self.lock:
|
|
278
|
+
self._click_count = 0
|
|
279
|
+
|
|
280
|
+
def panic_stop(self) -> None:
|
|
281
|
+
self.auto_running.clear()
|
|
282
|
+
self.holding.clear()
|
|
283
|
+
if self.on_auto_stopped:
|
|
284
|
+
self.on_auto_stopped(reason="panic")
|
|
285
|
+
|
|
286
|
+
# ------------------------------------------------------------------
|
|
287
|
+
# Lifecycle
|
|
288
|
+
# ------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
def shutdown(self) -> None:
|
|
291
|
+
"""Stop the mouse listener and unblock both loop threads.
|
|
292
|
+
|
|
293
|
+
Safe to call multiple times. After calling, the clicker is unusable.
|
|
294
|
+
"""
|
|
295
|
+
if self._stop_event.is_set():
|
|
296
|
+
return
|
|
297
|
+
logger.info("shutting down clicker")
|
|
298
|
+
self._stop_event.set()
|
|
299
|
+
# Wake up both wait()s so the loops can observe _stop_event
|
|
300
|
+
self.holding.set()
|
|
301
|
+
self.auto_running.set()
|
|
302
|
+
try:
|
|
303
|
+
self._mouse_listener.stop()
|
|
304
|
+
except (RuntimeError, OSError):
|
|
305
|
+
logger.exception("error stopping mouse listener")
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Global hotkey manager: single keyboard listener, multiple bindings."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import threading
|
|
7
|
+
|
|
8
|
+
from pynput import keyboard
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class HotkeyManager:
|
|
14
|
+
"""Listener unique qui dispatche vers plusieurs bindings.
|
|
15
|
+
|
|
16
|
+
Each binding is a dict ``{combo, mode, on_press, on_release?}`` where
|
|
17
|
+
``mode`` is ``"toggle"`` (callback once when all keys pressed) or
|
|
18
|
+
``"momentary"`` (callbacks at full-press and full-release).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self):
|
|
22
|
+
self.listener: keyboard.Listener | None = None
|
|
23
|
+
self.bindings: list[dict] = []
|
|
24
|
+
self.pressed: set = set()
|
|
25
|
+
self._lock = threading.Lock()
|
|
26
|
+
|
|
27
|
+
def configure(self, specs: list[dict]) -> list[str]:
|
|
28
|
+
"""(Re)configure the listener with the given bindings.
|
|
29
|
+
|
|
30
|
+
Returns the list of combo strings that failed to parse.
|
|
31
|
+
"""
|
|
32
|
+
self.stop()
|
|
33
|
+
new_bindings = []
|
|
34
|
+
invalid: list[str] = []
|
|
35
|
+
for spec in specs:
|
|
36
|
+
combo = (spec.get("combo") or "").strip()
|
|
37
|
+
if not combo:
|
|
38
|
+
continue
|
|
39
|
+
try:
|
|
40
|
+
keys = frozenset(keyboard.HotKey.parse(combo))
|
|
41
|
+
except (ValueError, TypeError):
|
|
42
|
+
logger.warning("invalid hotkey combo %r — ignored", combo)
|
|
43
|
+
invalid.append(combo)
|
|
44
|
+
continue
|
|
45
|
+
new_bindings.append({
|
|
46
|
+
"combo": combo,
|
|
47
|
+
"keys": keys,
|
|
48
|
+
"mode": spec["mode"],
|
|
49
|
+
"on_press": spec.get("on_press"),
|
|
50
|
+
"on_release": spec.get("on_release"),
|
|
51
|
+
"active": False,
|
|
52
|
+
})
|
|
53
|
+
with self._lock:
|
|
54
|
+
self.bindings = new_bindings
|
|
55
|
+
self.pressed = set()
|
|
56
|
+
if new_bindings:
|
|
57
|
+
self.listener = keyboard.Listener(
|
|
58
|
+
on_press=self._on_press, on_release=self._on_release
|
|
59
|
+
)
|
|
60
|
+
self.listener.start()
|
|
61
|
+
return invalid
|
|
62
|
+
|
|
63
|
+
def stop(self) -> None:
|
|
64
|
+
if self.listener is not None:
|
|
65
|
+
try:
|
|
66
|
+
self.listener.stop()
|
|
67
|
+
except (RuntimeError, OSError):
|
|
68
|
+
logger.exception("error stopping keyboard listener")
|
|
69
|
+
self.listener = None
|
|
70
|
+
|
|
71
|
+
def _canonical(self, key):
|
|
72
|
+
if self.listener is not None:
|
|
73
|
+
try:
|
|
74
|
+
return self.listener.canonical(key)
|
|
75
|
+
except (AttributeError, ValueError):
|
|
76
|
+
return key
|
|
77
|
+
return key
|
|
78
|
+
|
|
79
|
+
def _on_press(self, key):
|
|
80
|
+
ck = self._canonical(key)
|
|
81
|
+
with self._lock:
|
|
82
|
+
self.pressed.add(ck)
|
|
83
|
+
for b in self.bindings:
|
|
84
|
+
if not b["active"] and b["keys"].issubset(self.pressed):
|
|
85
|
+
b["active"] = True
|
|
86
|
+
cb = b["on_press"]
|
|
87
|
+
if cb:
|
|
88
|
+
try:
|
|
89
|
+
cb()
|
|
90
|
+
except Exception: # never let user callback kill the listener
|
|
91
|
+
logger.exception("hotkey on_press callback raised")
|
|
92
|
+
|
|
93
|
+
def _on_release(self, key):
|
|
94
|
+
ck = self._canonical(key)
|
|
95
|
+
with self._lock:
|
|
96
|
+
self.pressed.discard(ck)
|
|
97
|
+
for b in self.bindings:
|
|
98
|
+
if b["active"] and not b["keys"].issubset(self.pressed):
|
|
99
|
+
b["active"] = False
|
|
100
|
+
if b["mode"] == "momentary":
|
|
101
|
+
cb = b["on_release"]
|
|
102
|
+
if cb:
|
|
103
|
+
try:
|
|
104
|
+
cb()
|
|
105
|
+
except Exception: # never let user callback kill the listener
|
|
106
|
+
logger.exception("hotkey on_release callback raised")
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Runtime detection of OS + display server + expected pynput capabilities.
|
|
2
|
+
|
|
3
|
+
The detection is conservative: when in doubt, we assume the capability is
|
|
4
|
+
*not* available so the UI surfaces a warning rather than silently failing.
|
|
5
|
+
|
|
6
|
+
Capability matrix (best-effort, based on pynput 1.7 behaviour as of 2026):
|
|
7
|
+
|
|
8
|
+
| Display server | mouse listener | keyboard listener (global hotkeys) |
|
|
9
|
+
|---------------------|----------------|----------------------------------------|
|
|
10
|
+
| Linux X11 | yes | yes |
|
|
11
|
+
| Linux Wayland | partial (XWayland windows only) | usually no — Wayland blocks
|
|
12
|
+
| | | global keyboard grabbing for security |
|
|
13
|
+
| macOS | yes (with Accessibility permission) |
|
|
14
|
+
| Windows | yes | yes |
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import logging
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
from dataclasses import dataclass, field
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class PlatformCapabilities:
|
|
29
|
+
os_name: str # "linux" | "macos" | "windows" | other
|
|
30
|
+
display_server: str # "x11" | "wayland" | "macos" | "windows" | "unknown"
|
|
31
|
+
has_global_hotkeys: bool
|
|
32
|
+
has_global_mouse_events: bool
|
|
33
|
+
caveats: list[str] = field(default_factory=list)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def detect() -> PlatformCapabilities:
|
|
37
|
+
"""Inspect environment variables and ``sys.platform`` to guess capabilities."""
|
|
38
|
+
if sys.platform == "win32":
|
|
39
|
+
return PlatformCapabilities(
|
|
40
|
+
os_name="windows", display_server="windows",
|
|
41
|
+
has_global_hotkeys=True, has_global_mouse_events=True,
|
|
42
|
+
caveats=[],
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if sys.platform == "darwin":
|
|
46
|
+
return PlatformCapabilities(
|
|
47
|
+
os_name="macos", display_server="macos",
|
|
48
|
+
has_global_hotkeys=True, has_global_mouse_events=True,
|
|
49
|
+
caveats=[
|
|
50
|
+
"macos_accessibility_required",
|
|
51
|
+
],
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if sys.platform.startswith("linux"):
|
|
55
|
+
session = (os.environ.get("XDG_SESSION_TYPE") or "").lower()
|
|
56
|
+
wayland_display = os.environ.get("WAYLAND_DISPLAY")
|
|
57
|
+
if session == "wayland" or (not session and wayland_display):
|
|
58
|
+
return PlatformCapabilities(
|
|
59
|
+
os_name="linux", display_server="wayland",
|
|
60
|
+
# Conservative: assume hotkeys/mouse don't work on Wayland.
|
|
61
|
+
# In practice both depend on whether XWayland forwards events,
|
|
62
|
+
# which depends on focused window type (X11 vs native Wayland).
|
|
63
|
+
has_global_hotkeys=False,
|
|
64
|
+
has_global_mouse_events=False,
|
|
65
|
+
caveats=[
|
|
66
|
+
"wayland_hotkeys_blocked",
|
|
67
|
+
"wayland_mouse_xwayland_only",
|
|
68
|
+
],
|
|
69
|
+
)
|
|
70
|
+
if session == "x11" or os.environ.get("DISPLAY"):
|
|
71
|
+
return PlatformCapabilities(
|
|
72
|
+
os_name="linux", display_server="x11",
|
|
73
|
+
has_global_hotkeys=True, has_global_mouse_events=True,
|
|
74
|
+
caveats=[],
|
|
75
|
+
)
|
|
76
|
+
return PlatformCapabilities(
|
|
77
|
+
os_name="linux", display_server="unknown",
|
|
78
|
+
has_global_hotkeys=False, has_global_mouse_events=False,
|
|
79
|
+
caveats=["unknown_display_server"],
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return PlatformCapabilities(
|
|
83
|
+
os_name=sys.platform, display_server="unknown",
|
|
84
|
+
has_global_hotkeys=False, has_global_mouse_events=False,
|
|
85
|
+
caveats=["unknown_os"],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def log_summary(caps: PlatformCapabilities) -> None:
|
|
90
|
+
"""Emit one INFO line summarizing detected capabilities."""
|
|
91
|
+
logger.info(
|
|
92
|
+
"platform: os=%s display=%s hotkeys=%s mouse=%s caveats=%s",
|
|
93
|
+
caps.os_name, caps.display_server,
|
|
94
|
+
caps.has_global_hotkeys, caps.has_global_mouse_events,
|
|
95
|
+
caps.caveats or "none",
|
|
96
|
+
)
|