node-native-win-utils 2.1.5 → 2.2.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/README.md CHANGED
@@ -2,17 +2,6 @@
2
2
  [![License][license-src]][license-href]
3
3
  ![Node-API v8 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v8%20Badge.svg)
4
4
 
5
- #### USDT TRC20 - TYAJ3K3MZraJhWimxxeCKcJ2SYABkVsrzi
6
- #### USDT TON - UQDokuYZXr4OHvfslDqUoFYcp1_F8tcjQPk_TvqSSDk7SIa7
7
- #### BTC - 1A3mNKFdWKXZ7Bcnez8LbWbYHgck1g4GeV
8
- #### NOT - UQDokuYZXr4OHvfslDqUoFYcp1_F8tcjQPk_TvqSSDk7SIa7
9
-
10
-
11
-
12
- #### Will be very thankful for any support
13
-
14
-
15
-
16
5
  # Node Native Win Utils
17
6
 
18
7
 
package/dist/index.d.mts CHANGED
@@ -75,6 +75,16 @@ export type Color = [r: number, g: number, b: number];
75
75
  export type ROI = [x: number, y: number, width: number, height: number];
76
76
  export type Imread = (path: string) => ImageData;
77
77
  export type Imwrite = (image: ImageData) => Buffer;
78
+ export type StartMouseListener = (callback: (data: {
79
+ x: number;
80
+ y: number;
81
+ type: string;
82
+ }) => void) => void;
83
+ export type StopMouseListener = (error: {
84
+ success: boolean;
85
+ error?: string;
86
+ errorCode?: number;
87
+ }) => void;
78
88
  export type MatchTemplate = (image: ImageData, template: ImageData, method?: number | null, mask?: ImageData) => MatchData;
79
89
  export type Blur = (image: ImageData, sizeX: number, sizeY: number) => ImageData;
80
90
  export type BgrToGray = (image: ImageData) => ImageData;
@@ -85,7 +95,8 @@ export type DrawRectangle = (image: ImageData, start: Point, end: Point, rgb: Co
85
95
  export type GetRegion = (image: ImageData, region: ROI) => ImageData;
86
96
  export type TextRecognition = (trainedDataPath: string, dataLang: string, imagePath: string) => string;
87
97
  export type CaptureScreenAsync = () => Promise<Buffer>;
88
- declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, captureScreenAsync: CaptureScreenAsync, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition;
98
+ export type BringWindowToFront = (windowName: string) => boolean;
99
+ declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, captureScreenAsync: CaptureScreenAsync, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition, bringWindowToFront: BringWindowToFront, startMouseListener: StartMouseListener, stopMouseListener: StopMouseListener;
89
100
  declare const rawPressKey: PressKey;
90
101
  /**
91
102
  * Captures a window and saves it to a file.
@@ -252,7 +263,7 @@ declare class OpenCV {
252
263
  * @returns A new OpenCV instance with the equalized image data.
253
264
  */
254
265
  equalizeHist(): OpenCV;
255
- rgbToHsv(rgb: ColorBound): number[];
266
+ rgbToHsv(rgb: ColorBound): number[][];
256
267
  darkenColor(lowerBound: ColorBound, upperBound: ColorBound, darkenFactor: number): OpenCV;
257
268
  /**
258
269
  * Draws a rectangle on the image.
@@ -276,4 +287,4 @@ declare class OpenCV {
276
287
  imwrite(path: string): void;
277
288
  }
278
289
  declare function keyPress(keyCode: number, repeat?: number): Promise<boolean>;
279
- export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, KeyboardListener, OpenCV };
290
+ export { getWindowData, bringWindowToFront, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, startMouseListener, stopMouseListener, KeyboardListener, OpenCV };
package/dist/index.mjs CHANGED
@@ -8,7 +8,7 @@ const nodeGypBuild = __require("node-gyp-build");
8
8
  import { keyCodes, KeyCodeHelper } from "./keyCodes.mjs";
9
9
  import { __dirnameLocal } from "./dirnameLocal.mjs";
10
10
  const bindings = nodeGypBuild(path.resolve(__dirnameLocal, ".."));
11
- const { setKeyDownCallback, setKeyUpCallback, unsetKeyDownCallback, unsetKeyUpCallback, getWindowData, captureWindowN, captureScreenAsync, mouseMove, mouseClick, mouseDrag, typeString, pressKey, imread, imwrite, matchTemplate, blur, bgrToGray, drawRectangle, getRegion, textRecognition, equalizeHist, darkenColor } = bindings;
11
+ const { setKeyDownCallback, setKeyUpCallback, unsetKeyDownCallback, unsetKeyUpCallback, getWindowData, captureWindowN, captureScreenAsync, mouseMove, mouseClick, mouseDrag, typeString, pressKey, imread, imwrite, matchTemplate, blur, bgrToGray, drawRectangle, getRegion, textRecognition, equalizeHist, darkenColor, bringWindowToFront, startMouseListener, stopMouseListener } = bindings;
12
12
  const rawPressKey = pressKey;
13
13
  /**
14
14
  * Captures a window and saves it to a file.
@@ -209,11 +209,8 @@ class OpenCV {
209
209
  if (Cmax !== 0)
210
210
  Saturation = (delta / Cmax) * 100;
211
211
  let Value = Cmax * 100;
212
- // 🛠 Convert to OpenCV format
213
212
  return [
214
- Math.round((Hue / 360) * 179), // Scale H from [0, 360] → [0, 179]
215
- Math.round((Saturation / 100) * 255), // Scale S from [0, 100] → [0, 255]
216
- Math.round((Value / 100) * 255) // Scale V from [0, 100] → [0, 255]
213
+ [Hue, Saturation, Value]
217
214
  ];
218
215
  }
219
216
  darkenColor(lowerBound, upperBound, darkenFactor) {
@@ -265,4 +262,4 @@ function keyPress(keyCode, repeat) {
265
262
  return resolve(true);
266
263
  });
267
264
  }
268
- export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, KeyboardListener, OpenCV };
265
+ export { getWindowData, bringWindowToFront, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, startMouseListener, stopMouseListener, KeyboardListener, OpenCV };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-native-win-utils",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "author": "Andrew K.",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/T-Rumibul/node-native-win-utils.git",
@@ -43,18 +43,13 @@
43
43
  "test": "tap run "
44
44
  },
45
45
  "dependencies": {
46
- "node-addon-api": "^8.3.1",
47
- "node-gyp": "^11.1.0",
46
+ "node-addon-api": "^8.6.0",
47
+ "node-gyp": "^12.2.0",
48
48
  "node-gyp-build": "^4.8.4"
49
49
  },
50
50
  "devDependencies": {
51
- "@types/node": "^20.2.5",
52
- "node-api-headers": "^1.5.0",
53
- "tap": "^21.1.0"
54
- },
55
- "tap": {
56
- "plugin": [
57
- "@tapjs/sinon"
58
- ]
51
+ "@types/node": "^25.3.5",
52
+ "node-api-headers": "^1.8.0",
53
+ "tap": "^21.6.2"
59
54
  }
60
55
  }
@@ -42,5 +42,78 @@ Napi::Value GetWindowData(const Napi::CallbackInfo &info)
42
42
  result.Set("x", Napi::Number::New(env, x));
43
43
  result.Set("y", Napi::Number::New(env, y));
44
44
 
45
+ return result;
46
+ }
47
+
48
+ Napi::Value BringWindowToFront(const Napi::CallbackInfo &info)
49
+ {
50
+ Napi::Env env = info.Env();
51
+ Napi::Object result = Napi::Object::New(env);
52
+
53
+ auto returnError = [&](const std::string& message, DWORD errorCode = 0) {
54
+ result.Set("success", false);
55
+ result.Set("error", message);
56
+ if (errorCode != 0) {
57
+ result.Set("errorCode", Napi::Number::New(env, errorCode));
58
+ }
59
+ return result;
60
+ };
61
+
62
+ if (info.Length() < 1 || !info[0].IsString())
63
+ {
64
+ return returnError("Window name must be provided as a string");
65
+ }
66
+
67
+ std::string windowName = info[0].As<Napi::String>().Utf8Value();
68
+
69
+ HWND hWnd = GetWindowByName(windowName.c_str());
70
+ if (!hWnd || !IsWindow(hWnd))
71
+ {
72
+ return returnError("Window not found or invalid: " + windowName);
73
+ }
74
+
75
+ // Restore if minimized
76
+ if (IsIconic(hWnd))
77
+ {
78
+ ShowWindow(hWnd, SW_RESTORE);
79
+ // Give the window a moment to restore before trying to focus it
80
+ Sleep(50);
81
+ }
82
+
83
+ HWND fg = GetForegroundWindow();
84
+
85
+ // Bug fix 1: must attach the TARGET window's thread, not just the foreground
86
+ // window's thread. We need a triangle: cur <-> fg thread <-> target thread.
87
+ DWORD fgThread = fg ? GetWindowThreadProcessId(fg, nullptr) : 0;
88
+ DWORD tgtThread = GetWindowThreadProcessId(hWnd, nullptr);
89
+ DWORD curThread = GetCurrentThreadId();
90
+
91
+ // Bug fix 2: attach all relevant thread pairs so input state is fully shared
92
+ bool attachedFg = (fgThread && fgThread != curThread) ? AttachThreadInput(curThread, fgThread, TRUE) != 0 : false;
93
+ bool attachedTgt = (tgtThread && tgtThread != curThread) ? AttachThreadInput(curThread, tgtThread, TRUE) != 0 : false;
94
+
95
+ // Bug fix 3: use AllowSetForegroundWindow to explicitly grant permission,
96
+ // which bypasses Windows' focus-stealing prevention for cross-process calls
97
+ AllowSetForegroundWindow(ASFW_ANY);
98
+
99
+ // Bring the window to front using the full sequence
100
+ SetForegroundWindow(hWnd);
101
+ SetActiveWindow(hWnd);
102
+ BringWindowToTop(hWnd);
103
+ // Bug fix 4: SetFocus only works reliably once input queues are attached
104
+ SetFocus(hWnd);
105
+
106
+ // Always detach — don't leave threads attached on any code path
107
+ if (attachedFg) AttachThreadInput(curThread, fgThread, FALSE);
108
+ if (attachedTgt) AttachThreadInput(curThread, tgtThread, FALSE);
109
+
110
+ // Verify it actually worked
111
+ HWND newFg = GetForegroundWindow();
112
+ if (newFg != hWnd)
113
+ {
114
+ return returnError("Window did not come to foreground (focus-stealing prevention may be active)");
115
+ }
116
+
117
+ result.Set("success", true);
45
118
  return result;
46
119
  }
package/src/cpp/main.cpp CHANGED
@@ -9,6 +9,7 @@
9
9
 
10
10
  Napi::Object Init(Napi::Env env, Napi::Object exports)
11
11
  {
12
+
12
13
  exports.Set("getWindowData", Napi::Function::New(env, GetWindowData));
13
14
  exports.Set("captureWindowN", Napi::Function::New(env, CaptureWindow));
14
15
  exports.Set("captureScreenAsync", Napi::Function::New(env, CaptureScreenAsync));
@@ -31,6 +32,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
31
32
  exports.Set("drawRectangle", Napi::Function::New(env, DrawRectangle));
32
33
  exports.Set("getRegion", Napi::Function::New(env, GetRegion));
33
34
  exports.Set("textRecognition", Napi::Function::New(env, TextRecognition));
35
+ exports.Set("bringWindowToFront", Napi::Function::New(env, BringWindowToFront));
36
+ exports.Set("startMouseListener", Napi::Function::New(env, StartMouseListener));
37
+ exports.Set("stopMouseListener", Napi::Function::New(env, StopMouseListener));
34
38
  static GdiPlusInitializer gdiplusInitializer;
35
39
  // Register cleanup hook
36
40
  env.AddCleanupHook(CleanupHook);
package/src/cpp/mouse.cpp CHANGED
@@ -1,6 +1,11 @@
1
1
  // patrially used code from https://github.com/octalmage/robotjs witch is under MIT License Copyright (c) 2014 Jason Stallings
2
2
  #include <napi.h>
3
3
  #include <windows.h>
4
+
5
+ HHOOK mouseHook;
6
+ Napi::ThreadSafeFunction tsfn;
7
+ static HANDLE hHookThread = NULL;
8
+ static DWORD hookThreadId = 0;
4
9
  /**
5
10
  * Move the mouse to a specific point.
6
11
  * @param point The coordinates to move the mouse to (x, y).
@@ -173,3 +178,150 @@ Napi::Value DragMouse(const Napi::CallbackInfo &info)
173
178
 
174
179
  return Napi::Boolean::New(env, true);
175
180
  }
181
+
182
+
183
+ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
184
+ {
185
+ if (nCode >= 0)
186
+ {
187
+ MSLLHOOKSTRUCT *mouse = (MSLLHOOKSTRUCT *)lParam;
188
+
189
+ int x = mouse->pt.x;
190
+ int y = mouse->pt.y;
191
+
192
+ std::string type = "move";
193
+
194
+ switch (wParam)
195
+ {
196
+ case WM_LBUTTONDOWN:
197
+ type = "leftDown";
198
+ break;
199
+ case WM_LBUTTONUP:
200
+ type = "leftUp";
201
+ break;
202
+ case WM_RBUTTONDOWN:
203
+ type = "rightDown";
204
+ break;
205
+ case WM_RBUTTONUP:
206
+ type = "rightUp";
207
+ break;
208
+ case WM_MBUTTONDOWN:
209
+ type = "middleDown";
210
+ break;
211
+ case WM_MBUTTONUP:
212
+ type = "middleUp";
213
+ break;
214
+ case WM_MOUSEMOVE:
215
+ type = "move";
216
+ break;
217
+ }
218
+
219
+ auto callback = [x, y, type](Napi::Env env, Napi::Function jsCallback)
220
+ {
221
+ Napi::Object event = Napi::Object::New(env);
222
+ event.Set("x", x);
223
+ event.Set("y", y);
224
+ event.Set("type", type);
225
+
226
+ jsCallback.Call({event});
227
+ };
228
+
229
+ tsfn.BlockingCall(callback);
230
+ }
231
+
232
+ return CallNextHookEx(mouseHook, nCode, wParam, lParam);
233
+ }
234
+
235
+
236
+ DWORD WINAPI HookThread(LPVOID)
237
+ {
238
+ mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
239
+
240
+ MSG msg;
241
+ while (GetMessage(&msg, NULL, 0, 0))
242
+ {
243
+ TranslateMessage(&msg);
244
+ DispatchMessage(&msg);
245
+ }
246
+
247
+ UnhookWindowsHookEx(mouseHook);
248
+ mouseHook = NULL;
249
+ return 0;
250
+ }
251
+
252
+ Napi::Value StartMouseListener(const Napi::CallbackInfo &info)
253
+ {
254
+ Napi::Env env = info.Env();
255
+
256
+ if (!info[0].IsFunction())
257
+ {
258
+ Napi::TypeError::New(env, "Callback expected").ThrowAsJavaScriptException();
259
+ return env.Null();
260
+ }
261
+ // Bug fix: prevent double-start leaking a thread and hook
262
+ if (hHookThread != NULL)
263
+ {
264
+ Napi::TypeError::New(env, "Mouse listener already running").ThrowAsJavaScriptException();
265
+ return env.Null();
266
+ }
267
+ Napi::Function callback = info[0].As<Napi::Function>();
268
+
269
+ tsfn = Napi::ThreadSafeFunction::New(
270
+ env,
271
+ callback,
272
+ "MouseListener",
273
+ 0,
274
+ 1);
275
+
276
+ // Store handle and thread ID so StopMouseListener can signal and wait
277
+ hHookThread = CreateThread(NULL, 0, HookThread, NULL, 0, &hookThreadId);
278
+
279
+ return Napi::Boolean::New(env, true);
280
+ }
281
+
282
+
283
+ Napi::Value StopMouseListener(const Napi::CallbackInfo &info)
284
+ {
285
+ Napi::Env env = info.Env();
286
+ Napi::Object result = Napi::Object::New(env);
287
+
288
+ if (hHookThread == NULL)
289
+ {
290
+ result.Set("success", false);
291
+ result.Set("error", "No listener is running");
292
+ return result;
293
+ }
294
+
295
+ // PostThreadMessage with WM_QUIT breaks the GetMessage loop in HookThread,
296
+ // which then runs UnhookWindowsHookEx and exits
297
+ if (!PostThreadMessage(hookThreadId, WM_QUIT, 0, 0))
298
+ {
299
+ result.Set("success", false);
300
+ result.Set("error", "Failed to signal hook thread");
301
+ result.Set("errorCode", Napi::Number::New(env, GetLastError()));
302
+ return result;
303
+ }
304
+
305
+ // Wait for the thread to fully exit before releasing resources
306
+ DWORD waitResult = WaitForSingleObject(hHookThread, 3000);
307
+ if (waitResult != WAIT_OBJECT_0)
308
+ {
309
+ // Thread didn't exit in time — force it and warn
310
+ TerminateThread(hHookThread, 1);
311
+ result.Set("success", false);
312
+ result.Set("error", "Hook thread did not exit cleanly, was forcefully terminated");
313
+ }
314
+ else
315
+ {
316
+ result.Set("success", true);
317
+ }
318
+
319
+ // Release the TSFN so Node.js can garbage collect the JS callback
320
+ tsfn.Release();
321
+
322
+ CloseHandle(hHookThread);
323
+ hHookThread = NULL;
324
+ hookThreadId = 0;
325
+
326
+ return result;
327
+ }
package/src/index.mts CHANGED
@@ -107,6 +107,8 @@ export type Imread = (path: string) => ImageData;
107
107
 
108
108
  export type Imwrite = (image: ImageData) => Buffer;
109
109
 
110
+ export type StartMouseListener = (callback: (data: {x: number, y: number, type: string}) => void) => void;
111
+ export type StopMouseListener = () => {success: boolean, error?: string, errorCode?: number};
110
112
  export type MatchTemplate = (
111
113
  image: ImageData,
112
114
  template: ImageData,
@@ -135,7 +137,7 @@ export type GetRegion = (image: ImageData, region: ROI) => ImageData;
135
137
 
136
138
  export type TextRecognition = (trainedDataPath: string, dataLang: string, imagePath: string) => string;
137
139
  export type CaptureScreenAsync = () => Promise<Buffer>;
138
-
140
+ export type BringWindowToFront = (windowName: string) => boolean;
139
141
  const {
140
142
  setKeyDownCallback,
141
143
  setKeyUpCallback,
@@ -158,7 +160,11 @@ const {
158
160
  getRegion,
159
161
  textRecognition,
160
162
  equalizeHist,
161
- darkenColor
163
+ darkenColor,
164
+ bringWindowToFront,
165
+ startMouseListener,
166
+ stopMouseListener
167
+
162
168
  }: {
163
169
  setKeyDownCallback: SetKeyCallback;
164
170
  setKeyUpCallback: SetKeyCallback;
@@ -182,6 +188,9 @@ const {
182
188
  captureScreenAsync: CaptureScreenAsync;
183
189
  equalizeHist: EqualizeHist;
184
190
  darkenColor: DarkenColor;
191
+ bringWindowToFront: BringWindowToFront;
192
+ startMouseListener: StartMouseListener;
193
+ stopMouseListener: StopMouseListener
185
194
  } = bindings;
186
195
 
187
196
  const rawPressKey = pressKey;
@@ -525,6 +534,7 @@ function keyPress(keyCode: number, repeat?: number): Promise<boolean> {
525
534
 
526
535
  export {
527
536
  getWindowData,
537
+ bringWindowToFront,
528
538
  captureWindow,
529
539
  captureWindowN,
530
540
  mouseMove,
@@ -537,6 +547,8 @@ export {
537
547
  textRecognition,
538
548
  captureScreenToFile,
539
549
  captureScreenAsync,
550
+ startMouseListener,
551
+ stopMouseListener,
540
552
  KeyboardListener,
541
553
  OpenCV
542
554
  };