@mintplex-labs/advanced-selection-hook 1.0.0

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.
package/index.d.ts ADDED
@@ -0,0 +1,396 @@
1
+ /**
2
+ * Node Selection Hook
3
+ *
4
+ * This module provides a Node.js interface for monitoring text selections
5
+ * across applications on Windows using UI Automation and Accessibility APIs.
6
+ */
7
+
8
+ import { EventEmitter } from "events";
9
+
10
+ /**
11
+ * Text selection data returned by the native module
12
+ *
13
+ * Contains the selected text and its position information.
14
+ * Position coordinates are in screen coordinates (pixels).
15
+ */
16
+ export interface TextSelectionData {
17
+ /** The selected text content */
18
+ text: string;
19
+ /** The program name that triggered the selection */
20
+ programName: string;
21
+ /** First paragraph's left-top point (x, y) in pixels */
22
+ startTop: { x: number; y: number };
23
+ /** First paragraph's left-bottom point (x, y) in pixels */
24
+ startBottom: { x: number; y: number };
25
+ /** Last paragraph's right-top point (x, y) in pixels */
26
+ endTop: { x: number; y: number };
27
+ /** Last paragraph's right-bottom point (x, y) in pixels */
28
+ endBottom: { x: number; y: number };
29
+ /** Current mouse position (x, y) in pixels */
30
+ mousePosStart: { x: number; y: number };
31
+ /** Mouse down position (x, y) in pixels */
32
+ mousePosEnd: { x: number; y: number };
33
+ /** Selection method identifier */
34
+ method: (typeof SelectionHook.SelectionMethod)[keyof typeof SelectionHook.SelectionMethod];
35
+ /** Position level identifier */
36
+ posLevel: (typeof SelectionHook.PositionLevel)[keyof typeof SelectionHook.PositionLevel];
37
+ /** Whether the current app's front window is in fullscreen mode, macOS only */
38
+ isFullscreen?: boolean;
39
+ /** Whether the selection is in an editable text field (input, textarea, contenteditable, etc.) */
40
+ isEditable?: boolean;
41
+ }
42
+
43
+ /**
44
+ * Mouse event data structure
45
+ *
46
+ * Contains information about mouse events such as clicks and movements.
47
+ * Coordinates are in screen coordinates (pixels).
48
+ */
49
+ export interface MouseEventData {
50
+ /** X coordinate of mouse pointer (px) */
51
+ x: number;
52
+ /** Y coordinate of mouse pointer (px) */
53
+ y: number;
54
+ /** Mouse button identifier,
55
+ * same as WebAPIs' MouseEvent.button
56
+ * Left = 0, Middle = 1, Right = 2, Back = 3, Forward = 4,
57
+ * Unknown = -1
58
+ */
59
+ button: number;
60
+ }
61
+
62
+ /**
63
+ * Mouse wheel event data structure
64
+ *
65
+ * Contains information about mouse wheel events.
66
+ */
67
+ export interface MouseWheelEventData {
68
+ /** Mouse wheel button type
69
+ * 0: Vertical
70
+ * 1: Horizontal
71
+ */
72
+ button: number;
73
+ /** Mouse wheel direction flag
74
+ * 1: Up/Right
75
+ * -1: Down/Left
76
+ */
77
+ flag: number;
78
+ }
79
+
80
+ /**
81
+ * Keyboard event data structure
82
+ *
83
+ * Contains information about keyboard events such as key presses and releases.
84
+ */
85
+ export interface KeyboardEventData {
86
+ /**
87
+ * Unified key value of the vkCode. Same as MDN `KeyboardEvent.key` values.
88
+ * Converted from platform-specific vkCode.
89
+ *
90
+ * Values defined in https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
91
+ */
92
+ uniKey: string;
93
+ /** Virtual key code. The value is different on different platforms.
94
+ *
95
+ * Windows: VK_* values of vkCode, refer to https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
96
+ * macOS: kVK_* values of kCGKeyboardEventKeycode, defined in `HIToolbox/Events.h`
97
+ */
98
+ vkCode: number;
99
+ /** Whether modifier keys (Alt/Ctrl/Win/⌘/⌥/Fn) are pressed simultaneously */
100
+ sys: boolean;
101
+ /** Keyboard scan code. Windows Only. */
102
+ scanCode?: number;
103
+ /** Additional key flags. Varies on different platforms. */
104
+ flags: number;
105
+ /** Internal event type identifier */
106
+ type?: string;
107
+ /** Specific keyboard action (e.g., "key-down", "key-up") */
108
+ action?: string;
109
+ }
110
+
111
+ /**
112
+ * Configuration interface for text selection monitoring
113
+ *
114
+ * Contains settings that control the behavior of the text selection hook
115
+ * and its various features like mouse tracking and clipboard fallback.
116
+ */
117
+ export interface SelectionConfig {
118
+ /** Enable debug logging for warnings and errors */
119
+ debug?: boolean;
120
+ /** Enable high CPU usage mouse movement tracking */
121
+ enableMouseMoveEvent?: boolean;
122
+ /** Enable clipboard fallback for text selection */
123
+ enableClipboard?: boolean;
124
+ /** Enable passive mode where selection requires manual trigger */
125
+ selectionPassiveMode?: boolean;
126
+ /** Mode for clipboard fallback behavior */
127
+ clipboardMode?: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode];
128
+ /** List of program names for clipboard mode filtering */
129
+ programList?: string[];
130
+ }
131
+
132
+ /**
133
+ * SelectionHook - Main class for text selection monitoring
134
+ *
135
+ * This class provides methods to start/stop monitoring text selections
136
+ * across applications on Windows and emits events when selections occur.
137
+ */
138
+ declare class SelectionHook extends EventEmitter {
139
+ static SelectionMethod: {
140
+ NONE: 0;
141
+ UIA: 1;
142
+ /** @deprecated This method has been removed */
143
+ FOCUSCTL: 2;
144
+ ACCESSIBLE: 3;
145
+ AXAPI: 11;
146
+ CLIPBOARD: 99;
147
+ };
148
+
149
+ static PositionLevel: {
150
+ NONE: 0;
151
+ MOUSE_SINGLE: 1;
152
+ MOUSE_DUAL: 2;
153
+ SEL_FULL: 3;
154
+ SEL_DETAILED: 4;
155
+ };
156
+
157
+ static FilterMode: {
158
+ DEFAULT: 0;
159
+ INCLUDE_LIST: 1;
160
+ EXCLUDE_LIST: 2;
161
+ };
162
+
163
+ static FineTunedListType: {
164
+ EXCLUDE_CLIPBOARD_CURSOR_DETECT: 0;
165
+ INCLUDE_CLIPBOARD_DELAY_READ: 1;
166
+ };
167
+
168
+ /**
169
+ * Start monitoring text selections
170
+ *
171
+ * Initiates the native hooks to listen for text selection events
172
+ * across all applications. This must be called before any events
173
+ * will be emitted.
174
+ *
175
+ * @param debug Enable console debug logging (warnings and errors)
176
+ * @returns Success status (true if started successfully)
177
+ */
178
+ start(config?: SelectionConfig | null): boolean;
179
+
180
+ /**
181
+ * Stop monitoring text selections
182
+ *
183
+ * Stops the native hooks and prevents further events from being emitted.
184
+ * This should be called when monitoring is no longer needed to free resources.
185
+ *
186
+ * @returns Success status (true if stopped successfully or wasn't running)
187
+ */
188
+ stop(): boolean;
189
+
190
+ /**
191
+ * Check if hook is running
192
+ *
193
+ * Determines if the selection monitoring is currently active.
194
+ *
195
+ * @returns Running status (true if monitoring is active)
196
+ */
197
+ isRunning(): boolean;
198
+
199
+ /**
200
+ * Get current text selection
201
+ *
202
+ * Retrieves the current text selection, if any exists.
203
+ * Returns null if no text is currently selected or hook isn't running.
204
+ *
205
+ * @returns Current selection data or null if no selection exists
206
+ */
207
+ getCurrentSelection(): TextSelectionData | null;
208
+
209
+ /**
210
+ * Enable mousemove events (high CPU usage)
211
+ *
212
+ * Enables "mouse-move" events to be emitted when the mouse moves.
213
+ * Note: This can cause high CPU usage due to frequent event firing.
214
+ *
215
+ * @returns Success status (true if enabled successfully)
216
+ */
217
+ enableMouseMoveEvent(): boolean;
218
+
219
+ /**
220
+ * Disable mousemove events
221
+ *
222
+ * Stops emitting "mouse-move" events to reduce CPU usage.
223
+ * This is the default state after starting the hook.
224
+ *
225
+ * @returns Success status (true if disabled successfully)
226
+ */
227
+ disableMouseMoveEvent(): boolean;
228
+
229
+ /**
230
+ * Enable clipboard fallback for text selection
231
+ *
232
+ * Uses Ctrl+C as a last resort to get selected text when other methods fail.
233
+ * This might modify clipboard contents.
234
+ *
235
+ * @returns Success status (true if enabled successfully)
236
+ */
237
+ enableClipboard(): boolean;
238
+
239
+ /**
240
+ * Disable clipboard fallback for text selection
241
+ *
242
+ * Will not use Ctrl+C to get selected text.
243
+ * This preserves clipboard contents.
244
+ *
245
+ * @returns Success status (true if disabled successfully)
246
+ */
247
+ disableClipboard(): boolean;
248
+
249
+ /**
250
+ * Set clipboard mode and program list for text selection
251
+ *
252
+ * Configures how the clipboard fallback mechanism works for different programs.
253
+ * Mode can be:
254
+ * - DEFAULT: Use clipboard for all programs
255
+ * - INCLUDE_LIST: Only use clipboard for programs in the list
256
+ * - EXCLUDE_LIST: Use clipboard for all programs except those in the list
257
+ *
258
+ * @param {number} mode - Clipboard mode (SelectionHook.ClipboardMode)
259
+ * @param {string[]} programList - Array of program names to include/exclude
260
+ * @returns {boolean} Success status
261
+ */
262
+ setClipboardMode(
263
+ mode: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode],
264
+ programList?: string[]
265
+ ): boolean;
266
+
267
+ /**
268
+ * Set global filter mode for text selection
269
+ *
270
+ * Configures how the global filter mechanism works for different programs.
271
+ * Mode can be:
272
+ * - DEFAULT: disable global filter
273
+ * - INCLUDE_LIST: Only use global filter for programs in the list
274
+ * - EXCLUDE_LIST: Use global filter for all programs except those in the list
275
+ *
276
+ * @param {number} mode - Filter mode (SelectionHook.FilterMode)
277
+ * @param {string[]} programList - Array of program names to include/exclude
278
+ * @returns {boolean} Success status
279
+ */
280
+ setGlobalFilterMode(
281
+ mode: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode],
282
+ programList?: string[]
283
+ ): boolean;
284
+
285
+ /**
286
+ * Set fine-tuned list for specific behaviors
287
+ *
288
+ * Configures fine-tuned lists for specific application behaviors.
289
+ * List types:
290
+ * - EXCLUDE_CLIPBOARD_CURSOR_DETECT: Exclude cursor detection for clipboard operations
291
+ * - INCLUDE_CLIPBOARD_DELAY_READ: Include delay when reading clipboard content
292
+ *
293
+ * @param {number} listType - Fine-tuned list type (SelectionHook.FineTunedListType)
294
+ * @param {string[]} programList - Array of program names for the fine-tuned list
295
+ * @returns {boolean} Success status
296
+ */
297
+ setFineTunedList(
298
+ listType: (typeof SelectionHook.FineTunedListType)[keyof typeof SelectionHook.FineTunedListType],
299
+ programList?: string[]
300
+ ): boolean;
301
+
302
+ /**
303
+ * Set selection passive mode
304
+ * @param {boolean} passive - Passive mode
305
+ * @returns {boolean} Success status
306
+ */
307
+ setSelectionPassiveMode(passive: boolean): boolean;
308
+
309
+ /**
310
+ * Write text to clipboard
311
+ * @param {string} text - Text to write to clipboard
312
+ * @returns {boolean} Success status
313
+ */
314
+ writeToClipboard(text: string): boolean;
315
+
316
+ /**
317
+ * Read text from clipboard
318
+ * @returns {string|null} Text from clipboard or null if empty or error
319
+ */
320
+ readFromClipboard(): string | null;
321
+
322
+ /**
323
+ * Check if the process is trusted for accessibility (macOS only)
324
+ *
325
+ * Checks whether the current process has accessibility permissions
326
+ * required for text selection monitoring on macOS.
327
+ *
328
+ * @returns {boolean} True if the process is trusted for accessibility, false otherwise
329
+ */
330
+ macIsProcessTrusted(): boolean;
331
+
332
+ /**
333
+ * Try to request accessibility permissions (macOS only)
334
+ *
335
+ * This MAY show a dialog to the user if permissions are not granted.
336
+ *
337
+ * @returns {boolean} The current permission status, not the request result
338
+ */
339
+ macRequestProcessTrust(): boolean;
340
+
341
+ /**
342
+ * Release resources
343
+ *
344
+ * Stops monitoring and releases all native resources.
345
+ * Should be called before the application exits to prevent memory leaks.
346
+ */
347
+ cleanup(): void;
348
+
349
+ /**
350
+ * Register event listeners
351
+ *
352
+ * The hook emits various events that can be listened for:
353
+ */
354
+
355
+ /**
356
+ * Emitted when text is selected in any application
357
+ * @param event The event name
358
+ * @param listener Callback function receiving selection data
359
+ */
360
+ on(event: "text-selection", listener: (data: TextSelectionData) => void): this;
361
+
362
+ on(event: "mouse-up", listener: (data: MouseEventData) => void): this;
363
+ on(event: "mouse-down", listener: (data: MouseEventData) => void): this;
364
+ on(event: "mouse-move", listener: (data: MouseEventData) => void): this;
365
+ on(event: "mouse-wheel", listener: (data: MouseWheelEventData) => void): this;
366
+
367
+ on(event: "key-down", listener: (data: KeyboardEventData) => void): this;
368
+ on(event: "key-up", listener: (data: KeyboardEventData) => void): this;
369
+
370
+ on(event: "status", listener: (status: string) => void): this;
371
+ on(event: "error", listener: (error: Error) => void): this;
372
+
373
+ // Same events available with once() for one-time listeners
374
+ once(event: "text-selection", listener: (data: TextSelectionData) => void): this;
375
+ once(event: "mouse-up", listener: (data: MouseEventData) => void): this;
376
+ once(event: "mouse-down", listener: (data: MouseEventData) => void): this;
377
+ once(event: "mouse-move", listener: (data: MouseEventData) => void): this;
378
+ once(event: "mouse-wheel", listener: (data: MouseWheelEventData) => void): this;
379
+ once(event: "key-down", listener: (data: KeyboardEventData) => void): this;
380
+ once(event: "key-up", listener: (data: KeyboardEventData) => void): this;
381
+ once(event: "status", listener: (status: string) => void): this;
382
+ once(event: "error", listener: (error: Error) => void): this;
383
+ }
384
+
385
+ /**
386
+ * Instance type for the SelectionHook class
387
+ */
388
+ export type SelectionHookInstance = InstanceType<typeof SelectionHook>;
389
+
390
+ /**
391
+ * Constructor type for the SelectionHook class
392
+ */
393
+ export type SelectionHookConstructor = typeof SelectionHook;
394
+
395
+ // Export the SelectionHook class
396
+ export default SelectionHook;