node-native-win-utils 2.1.5 → 2.2.2

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
@@ -1,695 +1,175 @@
1
-
2
- [![License][license-src]][license-href]
3
- ![Node-API v8 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v8%20Badge.svg)
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
-
1
+ [![License][license-src]][license-href]
2
+ [![Node-API v8](https://raw.githubusercontent.com/nodejs/abi-stable-node/b062e657e639aad36280e0c6f54e181563ef4b19/assets/Node-API%20v8%20Badge.svg)](https://nodejs.org/api/n-api.html)
3
+ [![npm version](https://img.shields.io/npm/v/node-native-win-utils.svg)](https://www.npmjs.com/package/node-native-win-utils)
4
+ [![npm downloads](https://img.shields.io/npm/dm/node-native-win-utils.svg)](https://www.npmjs.com/package/node-native-win-utils)
5
+ [![Platform: Windows only](https://img.shields.io/badge/platform-Windows%20only-blue.svg)](https://github.com/T-Rumibul/node-native-win-utils)
15
6
 
16
7
  # Node Native Win Utils
17
8
 
18
-
19
-
20
9
  I did it for myself because I didn't feel like dealing with libraries like 'node-ffi' to implement this functionality. Maybe someone will find it useful. It's WINDOWS OS ONLY
21
10
 
22
-
23
11
 
24
- This package is a native addon for Node.js that allows you to perform various utility operations on Windows systems. It includes key event listeners, window data retrieval, window screenshot capture functionality, mouse movement, mouse click, mouse drag, and typing functionality, also I included precompiled libs of OpenCV(core, imgcodecs, imgproc)
12
+ ## Features
25
13
 
26
-
27
- # Installation
14
+ - Global keyboard event listener (`keyDown` / `keyUp`)
15
+ - Window information & screenshots
16
+ - Mouse movement, clicks, drag & drop
17
+ - Keyboard emulation (`keyPress`, `typeString`)
18
+ - OpenCV integration (template matching, blur, grayscale, histogram equalization, color manipulation, ROI, drawing)
19
+ - Tesseract OCR text recognition
20
+ - Screen capture
21
+ - Mouse event listener
22
+ - Prebuilt binaries (no Python or build tools required on Windows)
23
+ - ESM + CommonJS support
28
24
 
29
-
30
-
31
- You can install the package using npm:
25
+ ## Requirements
32
26
 
33
-
27
+ - **Windows 10 or later** (x64)
28
+ - Node.js >= 18 (prebuilts for recent versions via `prebuildify`)
34
29
 
35
- ```shell
36
-
37
-
38
-
39
- npm install node-native-win-utils
40
-
41
-
30
+ ## Installation
42
31
 
32
+ ```bash
33
+ npm install node-native-win-utils
43
34
  ```
44
35
 
45
-
46
-
47
- # Usage
48
-
49
-
50
-
51
- ## Importing the Package
52
-
53
-
54
-
55
- To use the package, import the necessary functions, types, and classes:
56
-
57
-
58
-
59
- ```javascript
36
+ ## Usage
60
37
 
38
+ ```ts
61
39
  import {
62
-
63
- KeyboardListener
64
-
65
- getWindowData,
66
-
67
- captureWindow,
68
-
69
- captureWindowN,
70
-
71
- mouseMove,
72
-
73
- mouseClick,
74
-
75
- mouseDrag,
76
-
77
- typeString,
78
-
79
- OpenCV,
80
-
81
- } from "node-native-win-utils";
82
-
40
+ KeyboardListener,
41
+ KeyCodeHelper,
42
+ KeyListener,
43
+ getWindowData,
44
+ captureWindow,
45
+ captureWindowN,
46
+ captureScreenToFile,
47
+ mouseMove,
48
+ mouseClick,
49
+ mouseDrag,
50
+ typeString,
51
+ keyPress,
52
+ textRecognition,
53
+ OpenCV,
54
+ startMouseListener,
55
+ stopMouseListener,
56
+ } from "node-native-win-utils";
83
57
  ```
84
58
 
85
-
86
-
87
- ## Keyboard Event Listener
88
-
89
-
59
+ ### Keyboard
90
60
 
91
- The package provides KeyboardListener singletone class, which allow you to register event listeners for key down and key up events, respectively. The event callbacks receive the `keyCode` as a parameter:
61
+ ```ts
62
+ // Singleton listener
63
+ const listener = KeyboardListener.listener();
64
+ listener.on("keyDown", (data) => console.log("Down:", data.keyName));
65
+ listener.on("keyUp", (data) => console.log("Up:", data.keyName));
92
66
 
93
-
94
-
95
- ```javascript
96
-
97
- const listener = KeyboardListener.listener()
98
-
99
- listener.on("keyDown", (data: {
100
- keyCode: number;
101
- keyName: string;
102
- }) => {
103
- //your code
104
- })
105
- listener.on("keyUp", (data: {
106
- keyCode: number;
107
- keyName: string;
108
- }) => {
109
- //your code
110
- })
67
+ // Simulate key press
68
+ keyPress(KeyCodeHelper.A); // once
69
+ keyPress(KeyCodeHelper.Enter, 2); // twice
111
70
  ```
112
71
 
113
- ## Key Press
72
+ ### Mouse & Typing
114
73
 
115
-
116
-
117
- The keyPress function allows you to simulate a key press event. Provide the keyCode as a parameter and optionally the number of times to press the key:
118
-
119
-
120
-
121
- ```javascript
122
-
123
- keyPress(65); // Press 'A' key once
124
-
125
-
126
-
127
- keyPress(65, 3); // Press 'A' key three times
74
+ ```ts
75
+ mouseMove(500, 300);
76
+ mouseClick(); // left click
77
+ mouseClick("right");
78
+ mouseDrag(100, 100, 800, 600, 50); // optional speed
128
79
 
80
+ typeString("Hello from Node!", 30); // 30ms delay per char
129
81
  ```
130
82
 
131
- ## Key Code Helper
132
-
133
-
134
-
135
- The KeyCodeHelper enum provides a set of key codes that can be used with key event functions:
136
-
137
-
138
-
139
- ```javascript
140
-
141
-
83
+ ### Window Operations
142
84
 
143
- import { KeyCodeHelper } from "node-native-win-utils";
144
-
145
-
146
-
147
- console.log(KeyCodeHelper.A); // Outputs the key code for 'A'
85
+ ```ts
86
+ const data = getWindowData("Notepad");
87
+ console.log(data); // { width, height, x, y }
148
88
 
89
+ captureWindow("Notepad", "screenshot.png");
90
+ const buffer = captureWindowN("Notepad"); // Buffer
149
91
  ```
150
92
 
151
-
152
-
153
- ## Window Data
154
-
155
-
156
-
157
- The `getWindowData` function retrieves information about a specific window identified by its name. It returns an object with properties `width`, `height`, `x`, and `y`, representing the window dimensions and position:
158
-
159
-
160
-
161
- ```javascript
162
-
163
- const windowData = getWindowData("Window Name");
164
-
165
-
166
-
167
- console.log("Window data:", windowData);
168
-
169
-
170
-
171
- // Window data: { width: 800, height: 600, x: 50, y: 50 }
172
-
173
- ```
174
-
175
-
176
-
177
- ## Window Capture
178
-
179
-
180
-
181
- The `captureWindow` function allows you to capture a screenshot of a specific window identified by its name. Provide the window name and the output path as parameters:
182
-
183
-
184
-
185
- ```javascript
186
-
187
- captureWindow("Window Name", "output.png");
188
-
189
-
190
-
191
- // Output: output.png with a screenshot of the window
93
+ ### Screen Capture
192
94
 
95
+ ```ts
96
+ await captureScreenToFile("full-screen.png");
193
97
  ```
194
98
 
195
-
196
-
197
- ## Mouse Movement
198
-
199
-
99
+ ### Text Recognition (Tesseract OCR)
200
100
 
201
- The `mouseMove` function allows you to move the mouse to a specific position on the screen. Provide the `posX` and `posY` coordinates as parameters:
202
-
203
-
204
-
205
- ```javascript
206
-
207
- mouseMove(100, 200);
208
-
209
- ```
210
-
211
-
212
-
213
- ## Mouse Click
214
-
215
-
216
-
217
- The `mouseClick` function allows you to perform a mouse click event. Optionally, you can specify the mouse button as a parameter (`"left"`, `"middle"`, or `"right"`). If no button is specified, a left mouse click is performed by default:
218
-
219
-
220
-
221
- ```javascript
222
-
223
- mouseClick(); // Left mouse click
224
-
225
-
226
-
227
- mouseClick("right"); // Right mouse click
228
-
229
- ```
230
-
231
-
232
-
233
- ## Mouse Drag
234
-
235
-
236
-
237
- The `mouseDrag` function allows you to simulate dragging the mouse from one position to another. Provide the starting and ending coordinates (`startX`, `startY`, `endX`, `endY`) as parameters. Optionally, you can specify the speed at which the mouse should be dragged:
238
-
239
-
240
-
241
- ```javascript
242
-
243
- mouseDrag(100, 200, 300, 400);
244
-
245
-
246
-
247
- mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
101
+ ```ts
102
+ import path from "path";
248
103
 
104
+ const text = textRecognition(
105
+ path.join(__dirname, "traineddata"), // path to .traineddata files
106
+ "eng",
107
+ path.join(__dirname, "image.png")
108
+ );
109
+ console.log(text);
249
110
  ```
250
111
 
251
-
112
+ ### OpenCV (image processing)
252
113
 
253
- ## Typing
114
+ ```ts
115
+ import { OpenCV } from "node-native-win-utils";
254
116
 
255
-
256
-
257
- The `typeString` function allows you to simulate typing a string of characters. Provide the string to type as the `stringToType` parameter. Optionally, you can specify
258
-
259
-
117
+ const img = new OpenCV("image.png"); // or ImageData { width, height, data: Uint8Array }
260
118
 
261
- a delay between each character (in milliseconds) using the `delay` parameter:
262
-
263
-
264
-
265
- ```javascript
266
-
267
- typeString("Hello, world!");
268
-
269
-
119
+ // Chainable methods
120
+ const processed = img
121
+ .blur(5, 5)
122
+ .bgrToGray()
123
+ .equalizeHist()
124
+ .darkenColor([240, 240, 240], [255, 255, 255], 0.5) // lower, upper, factor
125
+ .drawRectangle([10, 10], [200, 100], [255, 0, 0], 3)
126
+ .getRegion([50, 50, 300, 200]);
270
127
 
271
- typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
128
+ processed.imwrite("processed.png");
272
129
 
130
+ // Template matching
131
+ const match = img.matchTemplate(templateImage, /* method */, /* mask */);
132
+ console.log(match); // { minValue, maxValue, minLocation, maxLocation }
273
133
  ```
274
134
 
275
-
276
-
277
- ## Key Listener Class
278
-
279
-
280
-
281
- The `KeyListener` class extends the EventEmitter class and simplifies working with the `keyDownHandler` and `keyUpHandler` functions. You can register event listeners for the "keyDown" and "keyUp" events using the `on` method:
282
-
283
-
284
-
285
- ```javascript
286
-
287
- const listener = new KeyListener();
288
-
289
-
290
-
291
- listener.on("keyDown", (data) => {
292
-
293
- console.log("Key down:", data.keyCode, data.keyName);
135
+ ### Mouse Event Listener
294
136
 
137
+ ```ts
138
+ startMouseListener(({ x, y, type }) => {
139
+ console.log(`${type} at ${x},${y}`); // move at 400 300
295
140
  });
296
141
 
297
-
298
-
299
- // Key down: 8 Backspace
300
-
301
-
302
-
303
- listener.on("keyUp", (data) => {
304
-
305
- console.log("Key up:", data.keyCode, data.keyName);
306
-
307
- });
308
-
309
-
310
-
311
- // Key up: 8 Backspace
142
+ mouseClick("left"); // trigger event
312
143
 
144
+ // terminates a thread
145
+ stopMouseListener();
313
146
  ```
314
147
 
315
-
316
-
317
- ## OpenCV
318
-
319
-
320
-
321
- The `OpenCV` class extends the capabilities of the native addon package by providing various image processing functionalities. It allows users to perform operations such as matching templates, blurring images, converting color formats, drawing rectangles, getting image regions, and writing images to files.
322
-
323
-
324
-
325
- #### Constructor
326
-
327
-
328
-
329
- ```typescript
330
-
331
- const image = new OpenCV(image: string | ImageData)
332
-
333
- ```
334
-
335
-
336
-
337
- Creates a new instance of the `OpenCV` class with the specified image data. The `image` parameter can be either a file path (string) or an existing `ImageData` object.
338
-
339
-
340
-
341
- #### Properties
342
-
343
-
344
-
345
- ##### `imageData: ImageData`
346
-
347
-
348
-
349
- Holds the underlying image data that will be used for image processing operations.
350
-
351
-
352
-
353
- ##### `width: number`
354
-
355
-
356
-
357
- Read-only property that returns the width of the image in pixels.
358
-
359
-
360
-
361
- ##### `height: number`
362
-
363
-
364
-
365
- Read-only property that returns the height of the image in pixels.
366
-
367
-
368
-
369
- #### Methods
370
-
371
-
372
-
373
- ##### `matchTemplate(template: ImageData, method?: number | null, mask?: ImageData): OpenCV`
374
-
375
-
376
-
377
- Matches a template image with the current image and returns a new `OpenCV` instance containing the result.
378
-
379
-
380
-
381
- - `template: ImageData`: The template image data to be matched.
382
-
383
- - `method?: number | null`: (Optional) The matching method to be used. If not provided, the default method will be used.(currently no implemented)
384
-
385
- - `mask?: ImageData`: (Optional) An optional mask image data to be used during the matching process.
386
-
387
-
388
-
389
- ##### `blur(sizeX: number, sizeY: number): OpenCV`
390
-
391
-
392
-
393
- Applies a blur filter to the current image and returns a new `OpenCV` instance containing the blurred result.
394
-
395
-
396
-
397
- - `sizeX: number`: The size of the blur kernel in the X direction.
398
-
399
- - `sizeY: number`: The size of the blur kernel in the Y direction.
400
-
401
-
402
-
403
- ##### `bgrToGray(): OpenCV`
404
-
405
-
406
-
407
- Converts the current image from the BGR color format to grayscale and returns a new `OpenCV` instance containing the grayscale result.
408
-
409
-
410
-
411
- ##### `drawRectangle(start: Point, end: Point, rgb: Color, thickness: number): OpenCV`
412
-
413
-
414
-
415
- Draws a rectangle on the current image and returns a new `OpenCV` instance containing the modified result.
416
-
417
-
418
-
419
- - `start: Point`: The starting point (top-left) of the rectangle.
420
-
421
- - `end: Point`: The ending point (bottom-right) of the rectangle.
422
-
423
- - `rgb: Color`: The color of the rectangle in the RGB format (e.g., `{ r: 255, g: 0, b: 0 }` for red).
424
-
425
- - `thickness: number`: The thickness of the rectangle's border.
426
-
427
-
428
-
429
- ##### `getRegion(region: ROI): OpenCV`
430
-
431
-
432
-
433
- Extracts a region of interest (ROI) from the current image and returns a new `OpenCV` instance containing the extracted region.
434
-
435
-
436
-
437
- - `region: ROI`: An object specifying the region of interest with properties `x`, `y`, `width`, `height`.
438
-
439
-
440
-
441
- ##### `imwrite(path: string): void`
442
-
443
-
444
-
445
- Writes the current image to a file specified by the `path`.
446
-
447
-
448
-
449
- - `path: string`: The file path where the image will be saved.
450
-
451
-
452
-
453
- ## Functions
454
-
455
-
456
-
457
- | Function | Parameters | Return Type |
458
- |-----------------|----------------------------------------------------------------------------------------------|-------------|
459
- | getWindowData | `windowName: string` | `WindowData`|
460
- | captureWindow | `windowName: string, outputPath: string` | `void` |
461
- | mouseMove | `posX: number, posY: number` | `boolean` |
462
- | mouseClick | `button?: "left" \| "middle" \| "right"` | `boolean` |
463
- | mouseDrag | `startX: number, startY: number, endX: number, endY: number, speed?: number` | `boolean` |
464
- | typeString | `stringToType: string, delay?: number` | `boolean` |
465
- | captureWindowN | `windowName: string` | `Buffer` |
466
- | keyPress | `keyCode: number, repeat?: number` | `boolean` |
467
- | textRecognition | `trainedDataPath: string, dataLang: string, imagePath: string` | `string` |
468
- | captureScreenToFile | `path: string` | `boolean` |
469
-
470
-
471
- ## Examples
472
-
473
-
474
-
475
- Here are some examples of using the package:
476
-
477
- ```javascript
478
- console.log(textRecognition(path.join(__dirname, 'traineddata'), 'eng', path.join(__dirname, 'images', '1.jpg'))) // ---> <recognized text>
148
+ ## Building from source
479
149
 
150
+ ```bash
151
+ npm install
152
+ npm run build
480
153
  ```
481
154
 
482
- ```javascript
483
-
484
- // Example usage of the OpenCV class
485
-
486
- import { OpenCV } from "node-native-win-utils";
487
-
488
-
489
-
490
- const image = new OpenCV("path/to/image.png");
491
-
492
-
493
-
494
- const template = new OpenCV("path/to/template.png");
495
-
496
- const matchedImage = image.matchTemplate(template.imageData);
497
-
498
-
499
-
500
- const blurredImage = image.blur(5, 5);
501
-
502
-
503
-
504
- const grayscaleImage = image.bgrToGray();
505
-
506
-
507
-
508
- const regionOfInterest = { x: 100, y: 100, width: 200, height: 150 };
509
-
510
- const regionImage = image.getRegion(regionOfInterest);
511
-
512
-
513
-
514
- const redColor = { r: 255, g: 0, b: 0 };
515
-
516
- const thickRectangle = image.drawRectangle(
517
-
518
- { x: 50, y: 50 },
519
-
520
- { x: 150, y: 150 },
521
-
522
- redColor,
523
-
524
- 3
525
-
526
- );
527
-
528
-
529
-
530
- matchedImage.imwrite("output/matched.png");
531
-
532
- blurredImage.imwrite("output/blurred.png");
533
-
534
- grayscaleImage.imwrite("output/grayscale.png");
535
-
536
- regionImage.imwrite("output/region.png");
537
-
538
- thickRectangle.imwrite("output/thick_rectangle.png");
539
-
155
+ #### Requires:
540
156
  ```
541
-
542
-
543
-
544
- ```javascript
545
-
546
- // If you want to aply blur and convert to gray then do it that order:
547
-
548
- image.blur(5, 5).bgrToGray();
549
-
550
- // Otherwise you will get an error.
551
-
157
+ Visual Studio Build Tools (C++).
158
+ Python 3
552
159
  ```
553
160
 
554
-
555
-
556
- Please note that the above example demonstrates the usage of different methods available in the `OpenCV` class. Make sure to replace `"path/to/image.png"` and `"path/to/template.png"` with actual image file paths.
557
-
558
-
559
-
560
- ```javascript
561
-
562
- import {
563
-
564
- getWindowData,
565
-
566
- captureWindow,
567
-
568
- mouseMove,
569
-
570
- mouseClick,
571
-
572
- mouseDrag,
573
-
574
- typeString,
575
-
576
- KeyListener,
577
-
578
- } from "node-native-win-utils";
579
-
580
-
581
-
582
- // Retrieve window data
583
-
584
-
585
-
586
- const windowData = getWindowData("My Window");
587
-
588
-
589
-
590
- console.log("Window data:", windowData);
591
-
592
-
593
-
594
- // Window data: { width: 1024, height: 768, x: 100, y: 100 }
595
-
596
-
597
-
598
- // Capture window screenshot
599
-
600
-
601
-
602
- captureWindow("My Window", "output.png");
603
-
604
-
605
-
606
- // Output: output.png with a screenshot of the window
607
-
608
-
609
-
610
- // Move the mouse
611
-
612
-
613
-
614
- mouseMove(100, 200);
615
-
616
-
617
-
618
- // Perform mouse click
619
-
620
-
621
-
622
- mouseClick(); // Left mouse click
623
-
624
-
625
-
626
- mouseClick("right"); // Right mouse click
627
-
628
-
629
-
630
- // Simulate mouse drag
631
-
632
-
633
-
634
- mouseDrag(100, 200, 300, 400);
635
-
636
-
637
-
638
- mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
639
-
640
-
641
-
642
- // Simulate typing
643
-
644
-
645
-
646
- typeString("Hello, world!");
647
-
648
-
649
-
650
- typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
651
-
652
-
653
-
654
- // Use KeyboardListener class
655
-
656
-
657
-
658
- const listener = KeyboardListener.listener();
659
-
660
-
661
-
662
- listener.on("keyDown", (data) => {
663
-
664
- console.log("Key down:", data.keyCode, data.keyName);
665
-
666
- });
667
-
668
-
669
-
670
- // Key down: keyCode keyName
671
-
672
-
673
-
674
- listener.on("keyUp", (data) => {
675
-
676
- console.log("Key up:", data.keyCode, data.keyName);
677
-
678
- });
679
-
680
-
681
-
682
- // Key up: keyCode keyName
683
-
684
-
685
-
686
-
687
- ```
161
+ ## License
162
+ --
688
163
 
689
164
  [OpenCV License](https://github.com/opencv/opencv/blob/master/LICENSE)
165
+
690
166
  [MIT License](https://github.com/T-Rumibul/node-native-win-utils/blob/main/LICENSE)
691
167
 
692
168
 
693
- [license-src]: https://img.shields.io/github/license/nuxt-modules/icon.svg?style=for-the-badge&colorA=18181B&colorB=28CF8D
169
+ [license-src]: https://img.shields.io/badge/License-MIT-yellow.svg
694
170
 
695
171
  [license-href]: https://github.com/T-Rumibul/node-native-win-utils/blob/main/LICENSE
172
+
173
+
174
+ **Made with ❤️ for Windows automation.**
175
+ Issues / PRs welcome!