node-native-win-utils 1.3.2-alpha.2 → 1.3.3

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,350 +1,758 @@
1
- [![License][license-src]][license-href]
1
+
2
+ [![License][license-src]][license-href]
3
+
4
+ [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/ryner)
5
+
6
+
7
+
8
+ #### USDT TRC20 - TYAJ3K3MZraJhWimxxeCKcJ2SYABkVsrzi
9
+ #### USDT TON - UQDokuYZXr4OHvfslDqUoFYcp1_F8tcjQPk_TvqSSDk7SIa7
10
+ #### BTC - 1A3mNKFdWKXZ7Bcnez8LbWbYHgck1g4GeV
11
+ #### NOT - UQDokuYZXr4OHvfslDqUoFYcp1_F8tcjQPk_TvqSSDk7SIa7
12
+
13
+
14
+
15
+ #### Will be very thankful for any support
16
+
17
+
2
18
 
3
19
  # Node Native Win Utils
4
20
 
21
+
22
+
5
23
  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
6
24
 
25
+
26
+
7
27
  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)
8
28
 
29
+
30
+
31
+ # VESRION 1.3.3
32
+
33
+ Added a new function to simulate button press and release (`keyPress`), introduced an enumeration `KeyCodesHelper` with the most common key codes, and fixed a bug where the `typeString` function was not working with languages other than English.
34
+
35
+
36
+
9
37
  # Installation
10
38
 
39
+
40
+
11
41
  You can install the package using npm:
12
42
 
43
+
44
+
13
45
  ```shell
14
46
 
47
+
48
+
15
49
  npm install node-native-win-utils
16
50
 
51
+
52
+
17
53
  ```
18
54
 
55
+
56
+
19
57
  # Usage
20
58
 
59
+
60
+
21
61
  ## Importing the Package
22
62
 
63
+
64
+
23
65
  To use the package, import the necessary functions, types, and classes:
24
66
 
67
+
68
+
25
69
  ```javascript
70
+
26
71
  import {
27
- keyDownHandler,
28
- keyUpHandler,
29
- getWindowData,
30
- captureWindow,
31
- captureWindowN,
32
- mouseMove,
33
- mouseClick,
34
- mouseDrag,
35
- typeString,
36
- OpenCV,
37
- } from "node-native-win-utils";
72
+
73
+ keyDownHandler,
74
+
75
+ keyUpHandler,
76
+
77
+ getWindowData,
78
+
79
+ captureWindow,
80
+
81
+ captureWindowN,
82
+
83
+ mouseMove,
84
+
85
+ mouseClick,
86
+
87
+ mouseDrag,
88
+
89
+ typeString,
90
+
91
+ OpenCV,
92
+
93
+ } from "node-native-win-utils";
94
+
38
95
  ```
39
96
 
97
+
98
+
40
99
  ## Key Event Listeners
41
100
 
101
+
102
+
42
103
  The package provides `keyDownHandler` and `keyUpHandler` functions, which allow you to register callbacks for key down and key up events, respectively. The callbacks receive the `keyCode` as a parameter:
43
104
 
105
+
106
+
44
107
  ```javascript
108
+
45
109
  keyDownHandler((keyCode) => {
46
- console.log("Key down:", keyCode);
110
+
111
+ console.log("Key down:", keyCode);
112
+
47
113
  });
48
114
 
115
+
116
+
49
117
  // Key down: 8
50
118
 
119
+
120
+
51
121
  keyUpHandler((keyCode) => {
52
- console.log("Key up:", keyCode);
122
+
123
+ console.log("Key up:", keyCode);
124
+
53
125
  });
54
126
 
127
+
128
+
55
129
  // Key up: 8
130
+
56
131
  ```
57
132
 
133
+ ## Key Press
134
+
135
+
136
+
137
+ 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:
138
+
139
+
140
+
141
+ ```javascript
142
+
143
+ keyPress(65); // Press 'A' key once
144
+
145
+
146
+
147
+ keyPress(65, 3); // Press 'A' key three times
148
+
149
+ ```
150
+
151
+ ## Key Code Helper
152
+
153
+
154
+
155
+ The KeyCodeHelper enum provides a set of key codes that can be used with key event functions:
156
+
157
+
158
+
159
+ ```javascript
160
+
161
+
162
+
163
+ import { KeyCodeHelper } from "node-native-win-utils";
164
+
165
+
166
+
167
+ console.log(KeyCodeHelper.A); // Outputs the key code for 'A'
168
+
169
+ ```
170
+
171
+
172
+
58
173
  ## Window Data
59
174
 
175
+
176
+
60
177
  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:
61
178
 
179
+
180
+
62
181
  ```javascript
63
- const windowData = getWindowData("Window Name");
182
+
183
+ const windowData = getWindowData("Window Name");
184
+
185
+
64
186
 
65
187
  console.log("Window data:", windowData);
66
188
 
189
+
190
+
67
191
  // Window data: { width: 800, height: 600, x: 50, y: 50 }
192
+
68
193
  ```
69
194
 
195
+
196
+
70
197
  ## Window Capture
71
198
 
199
+
200
+
72
201
  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:
73
202
 
203
+
204
+
74
205
  ```javascript
206
+
75
207
  captureWindow("Window Name", "output.png");
76
208
 
209
+
210
+
77
211
  // Output: output.png with a screenshot of the window
212
+
78
213
  ```
79
214
 
215
+
216
+
80
217
  ## Mouse Movement
81
218
 
219
+
220
+
82
221
  The `mouseMove` function allows you to move the mouse to a specific position on the screen. Provide the `posX` and `posY` coordinates as parameters:
83
222
 
223
+
224
+
84
225
  ```javascript
226
+
85
227
  mouseMove(100, 200);
228
+
86
229
  ```
87
230
 
231
+
232
+
88
233
  ## Mouse Click
89
234
 
235
+
236
+
90
237
  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:
91
238
 
239
+
240
+
92
241
  ```javascript
242
+
93
243
  mouseClick(); // Left mouse click
94
244
 
245
+
246
+
95
247
  mouseClick("right"); // Right mouse click
248
+
96
249
  ```
97
250
 
251
+
252
+
98
253
  ## Mouse Drag
99
254
 
255
+
256
+
100
257
  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:
101
258
 
259
+
260
+
102
261
  ```javascript
262
+
103
263
  mouseDrag(100, 200, 300, 400);
104
264
 
265
+
266
+
105
267
  mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
268
+
106
269
  ```
107
270
 
271
+
272
+
108
273
  ## Typing
109
274
 
275
+
276
+
110
277
  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
111
278
 
279
+
280
+
112
281
  a delay between each character (in milliseconds) using the `delay` parameter:
113
282
 
283
+
284
+
114
285
  ```javascript
286
+
115
287
  typeString("Hello, world!");
116
288
 
289
+
290
+
117
291
  typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
292
+
118
293
  ```
119
294
 
295
+
296
+
120
297
  ## Key Listener Class
121
298
 
299
+
300
+
122
301
  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:
123
302
 
303
+
304
+
124
305
  ```javascript
125
- const listener = new KeyListener();
306
+
307
+ const listener = new KeyListener();
308
+
309
+
126
310
 
127
311
  listener.on("keyDown", (data) => {
128
- console.log("Key down:", data.keyCode, data.keyName);
312
+
313
+ console.log("Key down:", data.keyCode, data.keyName);
314
+
129
315
  });
130
316
 
317
+
318
+
131
319
  // Key down: 8 Backspace
132
320
 
321
+
322
+
133
323
  listener.on("keyUp", (data) => {
134
- console.log("Key up:", data.keyCode, data.keyName);
324
+
325
+ console.log("Key up:", data.keyCode, data.keyName);
326
+
135
327
  });
136
328
 
329
+
330
+
137
331
  // Key up: 8 Backspace
332
+
138
333
  ```
139
334
 
335
+
336
+
140
337
  ## OpenCV
141
338
 
339
+
340
+
142
341
  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.
143
342
 
343
+
344
+
144
345
  #### Constructor
145
346
 
347
+
348
+
146
349
  ```typescript
147
- const image = new OpenCV(image: string | ImageData)
350
+
351
+ const image = new OpenCV(image: string | ImageData)
352
+
148
353
  ```
149
354
 
355
+
356
+
150
357
  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.
151
358
 
359
+
360
+
152
361
  #### Properties
153
362
 
363
+
364
+
154
365
  ##### `imageData: ImageData`
155
366
 
367
+
368
+
156
369
  Holds the underlying image data that will be used for image processing operations.
157
370
 
371
+
372
+
158
373
  ##### `width: number`
159
374
 
375
+
376
+
160
377
  Read-only property that returns the width of the image in pixels.
161
378
 
379
+
380
+
162
381
  ##### `height: number`
163
382
 
383
+
384
+
164
385
  Read-only property that returns the height of the image in pixels.
165
386
 
387
+
388
+
166
389
  #### Methods
167
390
 
391
+
392
+
168
393
  ##### `matchTemplate(template: ImageData, method?: number | null, mask?: ImageData): OpenCV`
169
394
 
395
+
396
+
170
397
  Matches a template image with the current image and returns a new `OpenCV` instance containing the result.
171
398
 
172
- - `template: ImageData`: The template image data to be matched.
173
- - `method?: number | null`: (Optional) The matching method to be used. If not provided, the default method will be used.(currently no implemented)
174
- - `mask?: ImageData`: (Optional) An optional mask image data to be used during the matching process.
399
+
400
+
401
+ - `template: ImageData`: The template image data to be matched.
402
+
403
+ - `method?: number | null`: (Optional) The matching method to be used. If not provided, the default method will be used.(currently no implemented)
404
+
405
+ - `mask?: ImageData`: (Optional) An optional mask image data to be used during the matching process.
406
+
407
+
175
408
 
176
409
  ##### `blur(sizeX: number, sizeY: number): OpenCV`
177
410
 
411
+
412
+
178
413
  Applies a blur filter to the current image and returns a new `OpenCV` instance containing the blurred result.
179
414
 
180
- - `sizeX: number`: The size of the blur kernel in the X direction.
181
- - `sizeY: number`: The size of the blur kernel in the Y direction.
415
+
416
+
417
+ - `sizeX: number`: The size of the blur kernel in the X direction.
418
+
419
+ - `sizeY: number`: The size of the blur kernel in the Y direction.
420
+
421
+
182
422
 
183
423
  ##### `bgrToGray(): OpenCV`
184
424
 
425
+
426
+
185
427
  Converts the current image from the BGR color format to grayscale and returns a new `OpenCV` instance containing the grayscale result.
186
428
 
429
+
430
+
187
431
  ##### `drawRectangle(start: Point, end: Point, rgb: Color, thickness: number): OpenCV`
188
432
 
433
+
434
+
189
435
  Draws a rectangle on the current image and returns a new `OpenCV` instance containing the modified result.
190
436
 
191
- - `start: Point`: The starting point (top-left) of the rectangle.
192
- - `end: Point`: The ending point (bottom-right) of the rectangle.
193
- - `rgb: Color`: The color of the rectangle in the RGB format (e.g., `{ r: 255, g: 0, b: 0 }` for red).
194
- - `thickness: number`: The thickness of the rectangle's border.
437
+
438
+
439
+ - `start: Point`: The starting point (top-left) of the rectangle.
440
+
441
+ - `end: Point`: The ending point (bottom-right) of the rectangle.
442
+
443
+ - `rgb: Color`: The color of the rectangle in the RGB format (e.g., `{ r: 255, g: 0, b: 0 }` for red).
444
+
445
+ - `thickness: number`: The thickness of the rectangle's border.
446
+
447
+
195
448
 
196
449
  ##### `getRegion(region: ROI): OpenCV`
197
450
 
451
+
452
+
198
453
  Extracts a region of interest (ROI) from the current image and returns a new `OpenCV` instance containing the extracted region.
199
454
 
200
- - `region: ROI`: An object specifying the region of interest with properties `x`, `y`, `width`, `height`.
455
+
456
+
457
+ - `region: ROI`: An object specifying the region of interest with properties `x`, `y`, `width`, `height`.
458
+
459
+
201
460
 
202
461
  ##### `imwrite(path: string): void`
203
462
 
463
+
464
+
204
465
  Writes the current image to a file specified by the `path`.
205
466
 
206
- - `path: string`: The file path where the image will be saved.
467
+
468
+
469
+ - `path: string`: The file path where the image will be saved.
470
+
471
+
207
472
 
208
473
  ## Functions
209
474
 
210
- | Function | Parameters | Return Type |
211
- | -------------- | ---------------------------------------------------------------------------- | ------------ |
212
- | keyDownHandler | `callback: (keyCode: number) => void` | `void` |
213
- | keyUpHandler | `callback: (keyCode: number) => void` | `void` |
214
- | getWindowData | `windowName: string` | `WindowData` |
215
- | captureWindow | `windowName: string, outputPath: string` | `void` |
216
- | mouseMove | `posX: number, posY: number` | `boolean` |
217
- | mouseClick | `button?: "left" \| "middle" \| "right"` | `boolean` |
218
- | mouseDrag | `startX: number, startY: number, endX: number, endY: number, speed?: number` | `boolean` |
219
- | typeString | `stringToType: string, delay?: number` | `boolean` |
220
- | captureWindowN | `windowName: string` | `Buffer` |
475
+
476
+
477
+ | Function | Parameters | Return Type |
478
+ |-----------------|----------------------------------------------------------------------------------------------|-------------|
479
+ | keyDownHandler | `callback: (keyCode: number) => void` | `void` |
480
+ | keyUpHandler | `callback: (keyCode: number) => void` | `void` |
481
+ | getWindowData | `windowName: string` | `WindowData`|
482
+ | captureWindow | `windowName: string, outputPath: string` | `void` |
483
+ | mouseMove | `posX: number, posY: number` | `boolean` |
484
+ | mouseClick | `button?: "left" \| "middle" \| "right"` | `boolean` |
485
+ | mouseDrag | `startX: number, startY: number, endX: number, endY: number, speed?: number` | `boolean` |
486
+ | typeString | `stringToType: string, delay?: number` | `boolean` |
487
+ | captureWindowN | `windowName: string` | `Buffer` |
488
+ | keyPress | `keyCode: number, repeat?: number` | `boolean` |
489
+
490
+
491
+
221
492
 
222
493
  ## Examples
223
494
 
495
+
496
+
224
497
  Here are some examples of using the package:
225
498
 
499
+
500
+
226
501
  ```javascript
502
+
227
503
  // Example usage of the OpenCV class
228
- import { OpenCV } from "node-native-win-utils";
229
504
 
230
- const image = new OpenCV("path/to/image.png");
505
+ import { OpenCV } from "node-native-win-utils";
506
+
507
+
508
+
509
+ const image = new OpenCV("path/to/image.png");
510
+
511
+
512
+
513
+ const template = new OpenCV("path/to/template.png");
514
+
515
+ const matchedImage = image.matchTemplate(template.imageData);
516
+
517
+
518
+
519
+ const blurredImage = image.blur(5, 5);
520
+
521
+
522
+
523
+ const grayscaleImage = image.bgrToGray();
231
524
 
232
- const template = new OpenCV("path/to/template.png");
233
- const matchedImage = image.matchTemplate(template.imageData);
525
+
234
526
 
235
- const blurredImage = image.blur(5, 5);
527
+ const regionOfInterest = { x: 100, y: 100, width: 200, height: 150 };
236
528
 
237
- const grayscaleImage = image.bgrToGray();
529
+ const regionImage = image.getRegion(regionOfInterest);
238
530
 
239
- const regionOfInterest = { x: 100, y: 100, width: 200, height: 150 };
240
- const regionImage = image.getRegion(regionOfInterest);
531
+
532
+
533
+ const redColor = { r: 255, g: 0, b: 0 };
534
+
535
+ const thickRectangle = image.drawRectangle(
536
+
537
+ { x: 50, y: 50 },
538
+
539
+ { x: 150, y: 150 },
540
+
541
+ redColor,
542
+
543
+ 3
241
544
 
242
- const redColor = { r: 255, g: 0, b: 0 };
243
- const thickRectangle = image.drawRectangle(
244
- { x: 50, y: 50 },
245
- { x: 150, y: 150 },
246
- redColor,
247
- 3
248
545
  );
249
546
 
547
+
548
+
250
549
  matchedImage.imwrite("output/matched.png");
550
+
251
551
  blurredImage.imwrite("output/blurred.png");
552
+
252
553
  grayscaleImage.imwrite("output/grayscale.png");
554
+
253
555
  regionImage.imwrite("output/region.png");
556
+
254
557
  thickRectangle.imwrite("output/thick_rectangle.png");
558
+
255
559
  ```
256
560
 
561
+
562
+
257
563
  ```javascript
564
+
258
565
  // If you want to aply blur and convert to gray then do it that order:
566
+
259
567
  image.blur(5, 5).bgrToGray();
568
+
260
569
  // Otherwise you will get an error.
570
+
261
571
  ```
262
572
 
573
+
574
+
263
575
  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.
264
576
 
577
+
578
+
265
579
  ```javascript
580
+
266
581
  import {
267
- keyDownHandler,
268
- keyUpHandler,
269
- getWindowData,
270
- captureWindow,
271
- mouseMove,
272
- mouseClick,
273
- mouseDrag,
274
- typeString,
275
- KeyListener,
276
- } from "node-native-win-utils";
582
+
583
+ keyDownHandler,
584
+
585
+ keyUpHandler,
586
+
587
+ getWindowData,
588
+
589
+ captureWindow,
590
+
591
+ mouseMove,
592
+
593
+ mouseClick,
594
+
595
+ mouseDrag,
596
+
597
+ typeString,
598
+
599
+ KeyListener,
600
+
601
+ } from "node-native-win-utils";
602
+
603
+
277
604
 
278
605
  // Register key event handlers
279
606
 
607
+
608
+
280
609
  keyDownHandler((keyCode) => {
281
- console.log("Key down:", keyCode);
610
+
611
+ console.log("Key down:", keyCode);
612
+
282
613
  });
283
614
 
615
+
616
+
284
617
  // Key down: 123
285
618
 
619
+
620
+
286
621
  keyUpHandler((keyCode) => {
287
- console.log("Key up:", keyCode);
622
+
623
+ console.log("Key up:", keyCode);
624
+
288
625
  });
289
626
 
627
+
628
+
290
629
  // Key up: 123
291
630
 
631
+
632
+
292
633
  // Retrieve window data
293
634
 
294
- const windowData = getWindowData("My Window");
635
+
636
+
637
+ const windowData = getWindowData("My Window");
638
+
639
+
295
640
 
296
641
  console.log("Window data:", windowData);
297
642
 
643
+
644
+
298
645
  // Window data: { width: 1024, height: 768, x: 100, y: 100 }
299
646
 
647
+
648
+
300
649
  // Capture window screenshot
301
650
 
651
+
652
+
302
653
  captureWindow("My Window", "output.png");
303
654
 
655
+
656
+
304
657
  // Output: output.png with a screenshot of the window
305
658
 
659
+
660
+
306
661
  // Move the mouse
307
662
 
663
+
664
+
308
665
  mouseMove(100, 200);
309
666
 
667
+
668
+
310
669
  // Perform mouse click
311
670
 
671
+
672
+
312
673
  mouseClick(); // Left mouse click
313
674
 
675
+
676
+
314
677
  mouseClick("right"); // Right mouse click
315
678
 
679
+
680
+
316
681
  // Simulate mouse drag
317
682
 
683
+
684
+
318
685
  mouseDrag(100, 200, 300, 400);
319
686
 
687
+
688
+
320
689
  mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
321
690
 
691
+
692
+
322
693
  // Simulate typing
323
694
 
695
+
696
+
324
697
  typeString("Hello, world!");
325
698
 
699
+
700
+
326
701
  typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
327
702
 
703
+
704
+
328
705
  // Use KeyListener class
329
706
 
330
- const listener = new KeyListener();
707
+
708
+
709
+ const listener = new KeyListener();
710
+
711
+
331
712
 
332
713
  listener.on("keyDown", (data) => {
333
- console.log("Key down:", data.keyCode, data.keyName);
714
+
715
+ console.log("Key down:", data.keyCode, data.keyName);
716
+
334
717
  });
335
718
 
336
- // Key down: 8 Backspace
719
+
720
+
721
+ // Key down: keyCode keyName
722
+
723
+
337
724
 
338
725
  listener.on("keyUp", (data) => {
339
- console.log("Key up:", data.keyCode, data.keyName);
726
+
727
+ console.log("Key up:", data.keyCode, data.keyName);
728
+
340
729
  });
341
730
 
342
- // Key up: 8 Backspace
731
+
732
+
733
+ // Key up: keyCode keyName
734
+
735
+
736
+
737
+
343
738
  ```
344
739
 
345
- P.S.: As my knowledge of C++ is just brief, most of the C++ code is written with help of GPT-3.5 and GPT-4
740
+ # TODO
741
+
742
+
743
+
744
+ - Write proper tests
745
+
746
+ - Add more useful functions
747
+
748
+ - Fix issue with Windows scale for font,text,apps
749
+
750
+
346
751
 
347
752
  [OpenCV License](https://github.com/opencv/opencv/blob/master/LICENSE)
753
+ [MIT License](https://github.com/RynerNO/node-native-win-utils/blob/main/LICENSE)
754
+
348
755
 
349
756
  [license-src]: https://img.shields.io/github/license/nuxt-modules/icon.svg?style=for-the-badge&colorA=18181B&colorB=28CF8D
757
+
350
758
  [license-href]: https://github.com/RynerNO/node-native-win-utils/blob/main/LICENSE
package/binding.gyp CHANGED
@@ -40,7 +40,7 @@
40
40
  "defines": ["NAPI_CPP_EXCEPTIONS"],
41
41
  "cflags!": ["-fno-exceptions"],
42
42
  "cflags_cc!": ["-fno-rtti"],
43
- "cflags": ["/std:c++17"]
43
+ "cflags": ["/std:c++21"]
44
44
  }
45
45
  ]
46
46
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import EventEmitter = require("events");
2
+ import { KeyCodeHelper } from "./keyCodes";
2
3
  /**
3
4
  * Represents the data of a window.
4
5
  */
@@ -80,6 +81,7 @@ export type BgrToGray = (image: ImageData) => ImageData;
80
81
  export type DrawRectangle = (image: ImageData, start: Point, end: Point, rgb: Color, thickness: number) => ImageData;
81
82
  export type GetRegion = (image: ImageData, region: ROI) => ImageData;
82
83
  declare const keyDownHandler: KeyDownHandler, keyUpHandler: KeyUpHandler, getWindowData: GetWindowData, captureWindowN: CaptureWindow, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString;
84
+ declare const rawPressKey: PressKey;
83
85
  /**
84
86
  * Captures a window and saves it to a file.
85
87
  * @param windowName - The name of the window to capture.
@@ -172,5 +174,5 @@ export declare class OpenCV {
172
174
  */
173
175
  imwrite(path: string): void;
174
176
  }
175
- declare function keyPress(keyCode: number, repeat?: number): Promise<unknown>;
176
- export { keyDownHandler, keyUpHandler, getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress };
177
+ declare function keyPress(keyCode: number, repeat?: number): Promise<boolean>;
178
+ export { keyDownHandler, keyUpHandler, getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper };
package/dist/index.js CHANGED
@@ -3,14 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.typeString = exports.mouseDrag = exports.mouseClick = exports.mouseMove = exports.captureWindowN = exports.getWindowData = exports.keyUpHandler = exports.keyDownHandler = exports.OpenCV = exports.KeyListener = void 0;
6
+ exports.KeyCodeHelper = exports.rawPressKey = exports.typeString = exports.mouseDrag = exports.mouseClick = exports.mouseMove = exports.captureWindowN = exports.getWindowData = exports.keyUpHandler = exports.keyDownHandler = exports.OpenCV = exports.KeyListener = void 0;
7
7
  exports.captureWindow = captureWindow;
8
8
  exports.keyPress = keyPress;
9
9
  const EventEmitter = require("events");
10
10
  const path_1 = __importDefault(require("path"));
11
11
  const fs_1 = __importDefault(require("fs"));
12
12
  const bindings = require("node-gyp-build")(path_1.default.resolve(__dirname, ".."));
13
- const keyCodes_1 = __importDefault(require("./keyCodes"));
13
+ const keyCodes_1 = require("./keyCodes");
14
+ Object.defineProperty(exports, "KeyCodeHelper", { enumerable: true, get: function () { return keyCodes_1.KeyCodeHelper; } });
14
15
  const { keyDownHandler, keyUpHandler, getWindowData, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, pressKey, imread, imwrite, matchTemplate, blur, bgrToGray, drawRectangle, getRegion, } = bindings;
15
16
  exports.keyDownHandler = keyDownHandler;
16
17
  exports.keyUpHandler = keyUpHandler;
@@ -20,6 +21,8 @@ exports.mouseMove = mouseMove;
20
21
  exports.mouseClick = mouseClick;
21
22
  exports.mouseDrag = mouseDrag;
22
23
  exports.typeString = typeString;
24
+ const rawPressKey = pressKey;
25
+ exports.rawPressKey = rawPressKey;
23
26
  /**
24
27
  * Captures a window and saves it to a file.
25
28
  * @param windowName - The name of the window to capture.
@@ -41,14 +44,14 @@ class KeyListener extends EventEmitter {
41
44
  constructor() {
42
45
  super();
43
46
  keyDownHandler((keyCode) => {
44
- const keyName = keyCodes_1.default.get(keyCode.toString());
47
+ const keyName = keyCodes_1.keyCodes.get(keyCode.toString());
45
48
  this.emit("keyDown", {
46
49
  keyCode,
47
50
  keyName,
48
51
  });
49
52
  });
50
53
  keyUpHandler((keyCode) => {
51
- const keyName = keyCodes_1.default.get(keyCode.toString());
54
+ const keyName = keyCodes_1.keyCodes.get(keyCode.toString());
52
55
  this.emit("keyUp", {
53
56
  keyCode,
54
57
  keyName,
@@ -145,10 +148,15 @@ exports.OpenCV = OpenCV;
145
148
  function keyPress(keyCode, repeat) {
146
149
  return new Promise((resolve, reject) => {
147
150
  if (!repeat) {
148
- return resolve(pressKey(keyCode));
151
+ let result = rawPressKey(keyCode);
152
+ if (!result)
153
+ reject('Something went wrong');
154
+ return resolve(true);
149
155
  }
150
156
  for (let i = 0; i < repeat; i++) {
151
- pressKey(keyCode);
157
+ let result = rawPressKey(keyCode);
158
+ if (!result)
159
+ reject('Something went wrong');
152
160
  }
153
161
  return resolve(true);
154
162
  });
@@ -1,2 +1,101 @@
1
- declare const _default: Map<string, string>;
2
- export default _default;
1
+ export declare const keyCodes: Map<string, string>;
2
+ export declare enum KeyCodeHelper {
3
+ "Backspace" = 8,
4
+ "Tab" = 9,
5
+ "Enter" = 13,
6
+ "Shift" = 16,
7
+ "Ctrl" = 17,
8
+ "Alt" = 18,
9
+ "CapsLock" = 20,
10
+ "Escape" = 27,
11
+ "Space" = 32,
12
+ "PageUp" = 33,
13
+ "PageDown" = 34,
14
+ "End" = 35,
15
+ "Home" = 36,
16
+ "ArrowLeft" = 37,
17
+ "ArrowUp" = 38,
18
+ "ArrowRight" = 39,
19
+ "ArrowDown" = 40,
20
+ "Insert" = 45,
21
+ "Delete" = 46,
22
+ "Zero" = 48,
23
+ "One" = 49,
24
+ "Two" = 50,
25
+ "Three" = 51,
26
+ "Four" = 52,
27
+ "Five" = 53,
28
+ "Six" = 54,
29
+ "Seven" = 55,
30
+ "Eight" = 56,
31
+ "Nine" = 57,
32
+ "A" = 65,
33
+ "B" = 66,
34
+ "C" = 67,
35
+ "D" = 68,
36
+ "E" = 69,
37
+ "F" = 70,
38
+ "G" = 71,
39
+ "H" = 72,
40
+ "I" = 73,
41
+ "J" = 74,
42
+ "K" = 75,
43
+ "L" = 76,
44
+ "M" = 77,
45
+ "N" = 78,
46
+ "O" = 79,
47
+ "P" = 80,
48
+ "Q" = 81,
49
+ "R" = 82,
50
+ "S" = 83,
51
+ "T" = 84,
52
+ "U" = 85,
53
+ "V" = 86,
54
+ "W" = 87,
55
+ "X" = 88,
56
+ "Y" = 89,
57
+ "Z" = 90,
58
+ "MetaLeft" = 91,
59
+ "MetaRight" = 92,
60
+ "ContextMenu" = 93,
61
+ "Numpad0" = 96,
62
+ "Numpad1" = 97,
63
+ "Numpad2" = 98,
64
+ "Numpad3" = 99,
65
+ "Numpad4" = 100,
66
+ "Numpad5" = 101,
67
+ "Numpad6" = 102,
68
+ "Numpad7" = 103,
69
+ "Numpad8" = 104,
70
+ "Numpad9" = 105,
71
+ "NumpadMultiply" = 106,
72
+ "NumpadAdd" = 107,
73
+ "NumpadSubtract" = 109,
74
+ "NumpadDecimal" = 110,
75
+ "NumpadDivide" = 111,
76
+ "F1" = 112,
77
+ "F2" = 113,
78
+ "F3" = 114,
79
+ "F4" = 115,
80
+ "F5" = 116,
81
+ "F6" = 117,
82
+ "F7" = 118,
83
+ "F8" = 119,
84
+ "F9" = 120,
85
+ "F10" = 121,
86
+ "F11" = 122,
87
+ "F12" = 123,
88
+ "NumLock" = 144,
89
+ "ScrollLock" = 145,
90
+ "Semicolon" = 186,
91
+ "Equal" = 187,
92
+ "Comma" = 188,
93
+ "Minus" = 189,
94
+ "Period" = 190,
95
+ "Slash" = 191,
96
+ "Backquote" = 192,
97
+ "BracketLeft" = 219,
98
+ "Backslash" = 220,
99
+ "BracketRight" = 221,
100
+ "Quote" = 222
101
+ }
package/dist/keyCodes.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = new Map(Object.entries({
3
+ exports.KeyCodeHelper = exports.keyCodes = void 0;
4
+ exports.keyCodes = new Map(Object.entries({
4
5
  8: "Backspace",
5
6
  9: "Tab",
6
7
  13: "Enter",
@@ -100,3 +101,104 @@ exports.default = new Map(Object.entries({
100
101
  221: "BracketRight",
101
102
  222: "Quote",
102
103
  }));
104
+ var KeyCodeHelper;
105
+ (function (KeyCodeHelper) {
106
+ KeyCodeHelper[KeyCodeHelper["Backspace"] = 8] = "Backspace";
107
+ KeyCodeHelper[KeyCodeHelper["Tab"] = 9] = "Tab";
108
+ KeyCodeHelper[KeyCodeHelper["Enter"] = 13] = "Enter";
109
+ KeyCodeHelper[KeyCodeHelper["Shift"] = 16] = "Shift";
110
+ KeyCodeHelper[KeyCodeHelper["Ctrl"] = 17] = "Ctrl";
111
+ KeyCodeHelper[KeyCodeHelper["Alt"] = 18] = "Alt";
112
+ KeyCodeHelper[KeyCodeHelper["CapsLock"] = 20] = "CapsLock";
113
+ KeyCodeHelper[KeyCodeHelper["Escape"] = 27] = "Escape";
114
+ KeyCodeHelper[KeyCodeHelper["Space"] = 32] = "Space";
115
+ KeyCodeHelper[KeyCodeHelper["PageUp"] = 33] = "PageUp";
116
+ KeyCodeHelper[KeyCodeHelper["PageDown"] = 34] = "PageDown";
117
+ KeyCodeHelper[KeyCodeHelper["End"] = 35] = "End";
118
+ KeyCodeHelper[KeyCodeHelper["Home"] = 36] = "Home";
119
+ KeyCodeHelper[KeyCodeHelper["ArrowLeft"] = 37] = "ArrowLeft";
120
+ KeyCodeHelper[KeyCodeHelper["ArrowUp"] = 38] = "ArrowUp";
121
+ KeyCodeHelper[KeyCodeHelper["ArrowRight"] = 39] = "ArrowRight";
122
+ KeyCodeHelper[KeyCodeHelper["ArrowDown"] = 40] = "ArrowDown";
123
+ KeyCodeHelper[KeyCodeHelper["Insert"] = 45] = "Insert";
124
+ KeyCodeHelper[KeyCodeHelper["Delete"] = 46] = "Delete";
125
+ KeyCodeHelper[KeyCodeHelper["Zero"] = 48] = "Zero";
126
+ KeyCodeHelper[KeyCodeHelper["One"] = 49] = "One";
127
+ KeyCodeHelper[KeyCodeHelper["Two"] = 50] = "Two";
128
+ KeyCodeHelper[KeyCodeHelper["Three"] = 51] = "Three";
129
+ KeyCodeHelper[KeyCodeHelper["Four"] = 52] = "Four";
130
+ KeyCodeHelper[KeyCodeHelper["Five"] = 53] = "Five";
131
+ KeyCodeHelper[KeyCodeHelper["Six"] = 54] = "Six";
132
+ KeyCodeHelper[KeyCodeHelper["Seven"] = 55] = "Seven";
133
+ KeyCodeHelper[KeyCodeHelper["Eight"] = 56] = "Eight";
134
+ KeyCodeHelper[KeyCodeHelper["Nine"] = 57] = "Nine";
135
+ KeyCodeHelper[KeyCodeHelper["A"] = 65] = "A";
136
+ KeyCodeHelper[KeyCodeHelper["B"] = 66] = "B";
137
+ KeyCodeHelper[KeyCodeHelper["C"] = 67] = "C";
138
+ KeyCodeHelper[KeyCodeHelper["D"] = 68] = "D";
139
+ KeyCodeHelper[KeyCodeHelper["E"] = 69] = "E";
140
+ KeyCodeHelper[KeyCodeHelper["F"] = 70] = "F";
141
+ KeyCodeHelper[KeyCodeHelper["G"] = 71] = "G";
142
+ KeyCodeHelper[KeyCodeHelper["H"] = 72] = "H";
143
+ KeyCodeHelper[KeyCodeHelper["I"] = 73] = "I";
144
+ KeyCodeHelper[KeyCodeHelper["J"] = 74] = "J";
145
+ KeyCodeHelper[KeyCodeHelper["K"] = 75] = "K";
146
+ KeyCodeHelper[KeyCodeHelper["L"] = 76] = "L";
147
+ KeyCodeHelper[KeyCodeHelper["M"] = 77] = "M";
148
+ KeyCodeHelper[KeyCodeHelper["N"] = 78] = "N";
149
+ KeyCodeHelper[KeyCodeHelper["O"] = 79] = "O";
150
+ KeyCodeHelper[KeyCodeHelper["P"] = 80] = "P";
151
+ KeyCodeHelper[KeyCodeHelper["Q"] = 81] = "Q";
152
+ KeyCodeHelper[KeyCodeHelper["R"] = 82] = "R";
153
+ KeyCodeHelper[KeyCodeHelper["S"] = 83] = "S";
154
+ KeyCodeHelper[KeyCodeHelper["T"] = 84] = "T";
155
+ KeyCodeHelper[KeyCodeHelper["U"] = 85] = "U";
156
+ KeyCodeHelper[KeyCodeHelper["V"] = 86] = "V";
157
+ KeyCodeHelper[KeyCodeHelper["W"] = 87] = "W";
158
+ KeyCodeHelper[KeyCodeHelper["X"] = 88] = "X";
159
+ KeyCodeHelper[KeyCodeHelper["Y"] = 89] = "Y";
160
+ KeyCodeHelper[KeyCodeHelper["Z"] = 90] = "Z";
161
+ KeyCodeHelper[KeyCodeHelper["MetaLeft"] = 91] = "MetaLeft";
162
+ KeyCodeHelper[KeyCodeHelper["MetaRight"] = 92] = "MetaRight";
163
+ KeyCodeHelper[KeyCodeHelper["ContextMenu"] = 93] = "ContextMenu";
164
+ KeyCodeHelper[KeyCodeHelper["Numpad0"] = 96] = "Numpad0";
165
+ KeyCodeHelper[KeyCodeHelper["Numpad1"] = 97] = "Numpad1";
166
+ KeyCodeHelper[KeyCodeHelper["Numpad2"] = 98] = "Numpad2";
167
+ KeyCodeHelper[KeyCodeHelper["Numpad3"] = 99] = "Numpad3";
168
+ KeyCodeHelper[KeyCodeHelper["Numpad4"] = 100] = "Numpad4";
169
+ KeyCodeHelper[KeyCodeHelper["Numpad5"] = 101] = "Numpad5";
170
+ KeyCodeHelper[KeyCodeHelper["Numpad6"] = 102] = "Numpad6";
171
+ KeyCodeHelper[KeyCodeHelper["Numpad7"] = 103] = "Numpad7";
172
+ KeyCodeHelper[KeyCodeHelper["Numpad8"] = 104] = "Numpad8";
173
+ KeyCodeHelper[KeyCodeHelper["Numpad9"] = 105] = "Numpad9";
174
+ KeyCodeHelper[KeyCodeHelper["NumpadMultiply"] = 106] = "NumpadMultiply";
175
+ KeyCodeHelper[KeyCodeHelper["NumpadAdd"] = 107] = "NumpadAdd";
176
+ KeyCodeHelper[KeyCodeHelper["NumpadSubtract"] = 109] = "NumpadSubtract";
177
+ KeyCodeHelper[KeyCodeHelper["NumpadDecimal"] = 110] = "NumpadDecimal";
178
+ KeyCodeHelper[KeyCodeHelper["NumpadDivide"] = 111] = "NumpadDivide";
179
+ KeyCodeHelper[KeyCodeHelper["F1"] = 112] = "F1";
180
+ KeyCodeHelper[KeyCodeHelper["F2"] = 113] = "F2";
181
+ KeyCodeHelper[KeyCodeHelper["F3"] = 114] = "F3";
182
+ KeyCodeHelper[KeyCodeHelper["F4"] = 115] = "F4";
183
+ KeyCodeHelper[KeyCodeHelper["F5"] = 116] = "F5";
184
+ KeyCodeHelper[KeyCodeHelper["F6"] = 117] = "F6";
185
+ KeyCodeHelper[KeyCodeHelper["F7"] = 118] = "F7";
186
+ KeyCodeHelper[KeyCodeHelper["F8"] = 119] = "F8";
187
+ KeyCodeHelper[KeyCodeHelper["F9"] = 120] = "F9";
188
+ KeyCodeHelper[KeyCodeHelper["F10"] = 121] = "F10";
189
+ KeyCodeHelper[KeyCodeHelper["F11"] = 122] = "F11";
190
+ KeyCodeHelper[KeyCodeHelper["F12"] = 123] = "F12";
191
+ KeyCodeHelper[KeyCodeHelper["NumLock"] = 144] = "NumLock";
192
+ KeyCodeHelper[KeyCodeHelper["ScrollLock"] = 145] = "ScrollLock";
193
+ KeyCodeHelper[KeyCodeHelper["Semicolon"] = 186] = "Semicolon";
194
+ KeyCodeHelper[KeyCodeHelper["Equal"] = 187] = "Equal";
195
+ KeyCodeHelper[KeyCodeHelper["Comma"] = 188] = "Comma";
196
+ KeyCodeHelper[KeyCodeHelper["Minus"] = 189] = "Minus";
197
+ KeyCodeHelper[KeyCodeHelper["Period"] = 190] = "Period";
198
+ KeyCodeHelper[KeyCodeHelper["Slash"] = 191] = "Slash";
199
+ KeyCodeHelper[KeyCodeHelper["Backquote"] = 192] = "Backquote";
200
+ KeyCodeHelper[KeyCodeHelper["BracketLeft"] = 219] = "BracketLeft";
201
+ KeyCodeHelper[KeyCodeHelper["Backslash"] = 220] = "Backslash";
202
+ KeyCodeHelper[KeyCodeHelper["BracketRight"] = 221] = "BracketRight";
203
+ KeyCodeHelper[KeyCodeHelper["Quote"] = 222] = "Quote";
204
+ })(KeyCodeHelper || (exports.KeyCodeHelper = KeyCodeHelper = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-native-win-utils",
3
- "version": "1.3.2-alpha.2",
3
+ "version": "1.3.3",
4
4
  "author": "RynerNO",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/RynerNO/node-native-win-utils.git",
@@ -32,7 +32,8 @@
32
32
  "gypfile": true,
33
33
  "scripts": {
34
34
  "install": "node-gyp-build",
35
- "build": "node-gyp clean && prebuildify --napi && tsc"
35
+ "build": "node-gyp clean && prebuildify --napi && tsc",
36
+ "test": "jest"
36
37
  },
37
38
  "dependencies": {
38
39
  "node-addon-api": "^6.1.0",
@@ -40,6 +41,7 @@
40
41
  "node-gyp-build": "4.8.0"
41
42
  },
42
43
  "devDependencies": {
43
- "@types/node": "^20.2.5"
44
+ "@types/node": "^20.2.5",
45
+ "jest": "^29.7.0"
44
46
  }
45
47
  }
@@ -164,44 +164,41 @@ Napi::Value SetKeyUpCallback(const Napi::CallbackInfo &info)
164
164
  return env.Undefined();
165
165
  }
166
166
 
167
- Napi::Value TypeString(const Napi::CallbackInfo &info)
168
- {
169
- Napi::Env env = info.Env();
167
+ Napi::Value TypeString(const Napi::CallbackInfo &info) {
168
+ Napi::Env env = info.Env();
170
169
 
171
- if (info.Length() < 1 || !info[0].IsString())
172
- {
173
- Napi::TypeError::New(env, "You should provide a string to type").ThrowAsJavaScriptException();
174
- return Napi::Boolean::New(env, false);
175
- }
176
- int delay = 15;
177
- std::string text = info[0].As<Napi::String>();
178
- if (info.Length() > 1 && info[1].IsNumber())
179
- {
180
- delay = info[1].As<Napi::Number>();
181
- }
182
- // Convert the string to wide string
183
- std::wstring wideText(text.begin(), text.end());
170
+ if (info.Length() < 1 || !info[0].IsString()) {
171
+ Napi::TypeError::New(env, "You should provide a string to type").ThrowAsJavaScriptException();
172
+ return Napi::Boolean::New(env, false);
173
+ }
184
174
 
185
- // Simulate typing by sending keyboard events
186
- for (const wchar_t &character : wideText)
187
- {
188
- INPUT keyboardInput = {0};
189
- keyboardInput.type = INPUT_KEYBOARD;
190
- keyboardInput.ki.wVk = 0;
191
- keyboardInput.ki.wScan = character;
192
- keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE;
193
- keyboardInput.ki.time = 0; // System will provide the timestamp
175
+ int delay = 15;
176
+ std::u16string text = info[0].As<Napi::String>().Utf16Value();
177
+ if (info.Length() > 1 && info[1].IsNumber()) {
178
+ delay = info[1].As<Napi::Number>().Int32Value();
179
+ }
194
180
 
195
- SendInput(1, &keyboardInput, sizeof(keyboardInput));
181
+ for (const auto &character : text) {
182
+ INPUT keyboardInput = {0};
183
+ keyboardInput.type = INPUT_KEYBOARD;
184
+ keyboardInput.ki.wVk = 0;
185
+ keyboardInput.ki.wScan = character;
186
+ keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE;
187
+ keyboardInput.ki.time = 0;
188
+ keyboardInput.ki.dwExtraInfo = 0;
196
189
 
197
- keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
198
- SendInput(1, &keyboardInput, sizeof(keyboardInput));
190
+ // Press the key
191
+ SendInput(1, &keyboardInput, sizeof(INPUT));
199
192
 
200
- // Sleep for 15 milliseconds between each character input
201
- Sleep(delay);
202
- }
193
+ // Release the key
194
+ keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
195
+ SendInput(1, &keyboardInput, sizeof(INPUT));
203
196
 
204
- return Napi::Boolean::New(env, true);
197
+ // Sleep for the specified delay between keystrokes
198
+ Sleep(delay);
199
+ }
200
+
201
+ return Napi::Boolean::New(env, true);
205
202
  }
206
203
 
207
204
  // Function to simulate key press and release using provided key code