node-native-win-utils 1.3.4 → 2.1.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.
Files changed (191) hide show
  1. package/README.md +30 -83
  2. package/binding.gyp +3 -0
  3. package/dist/dirnameLocal.cjs +4 -0
  4. package/dist/dirnameLocal.d.cts +1 -0
  5. package/dist/dirnameLocal.d.mts +1 -0
  6. package/dist/dirnameLocal.mjs +2 -0
  7. package/dist/{index.js → index.cjs} +49 -17
  8. package/dist/{index.d.ts → index.d.cts} +25 -12
  9. package/dist/index.d.mts +191 -0
  10. package/dist/index.mjs +177 -0
  11. package/dist/keyCodes.d.mts +101 -0
  12. package/dist/keyCodes.mjs +201 -0
  13. package/node22.json +16 -0
  14. package/package.json +22 -9
  15. package/prebuilds/win32-x64/node-native-win-utils.node +0 -0
  16. package/prebuilds/win32-x64/tesseract.dll +0 -0
  17. package/prebuilds/win32-x64/tiff.dll +0 -0
  18. package/src/cpp/keyboard.cpp +141 -140
  19. package/src/cpp/main.cpp +11 -3
  20. package/src/cpp/{capturewindow.cpp → screenshot.cpp} +174 -2
  21. package/src/cpp/tesseract.cpp +49 -0
  22. package/src/dirnameLocal.mts +3 -0
  23. package/src/index.mts +454 -0
  24. package/src/keyCodes.mts +203 -0
  25. package/include/opencv2/core/affine.hpp +0 -678
  26. package/include/opencv2/core/async.hpp +0 -105
  27. package/include/opencv2/core/base.hpp +0 -664
  28. package/include/opencv2/core/bindings_utils.hpp +0 -325
  29. package/include/opencv2/core/bufferpool.hpp +0 -40
  30. package/include/opencv2/core/check.hpp +0 -170
  31. package/include/opencv2/core/core.hpp +0 -48
  32. package/include/opencv2/core/core_c.h +0 -3128
  33. package/include/opencv2/core/cuda/block.hpp +0 -211
  34. package/include/opencv2/core/cuda/border_interpolate.hpp +0 -722
  35. package/include/opencv2/core/cuda/color.hpp +0 -309
  36. package/include/opencv2/core/cuda/common.hpp +0 -131
  37. package/include/opencv2/core/cuda/datamov_utils.hpp +0 -113
  38. package/include/opencv2/core/cuda/detail/color_detail.hpp +0 -2018
  39. package/include/opencv2/core/cuda/detail/reduce.hpp +0 -365
  40. package/include/opencv2/core/cuda/detail/reduce_key_val.hpp +0 -502
  41. package/include/opencv2/core/cuda/detail/transform_detail.hpp +0 -392
  42. package/include/opencv2/core/cuda/detail/type_traits_detail.hpp +0 -191
  43. package/include/opencv2/core/cuda/detail/vec_distance_detail.hpp +0 -121
  44. package/include/opencv2/core/cuda/dynamic_smem.hpp +0 -88
  45. package/include/opencv2/core/cuda/emulation.hpp +0 -269
  46. package/include/opencv2/core/cuda/filters.hpp +0 -293
  47. package/include/opencv2/core/cuda/funcattrib.hpp +0 -79
  48. package/include/opencv2/core/cuda/functional.hpp +0 -805
  49. package/include/opencv2/core/cuda/limits.hpp +0 -128
  50. package/include/opencv2/core/cuda/reduce.hpp +0 -209
  51. package/include/opencv2/core/cuda/saturate_cast.hpp +0 -292
  52. package/include/opencv2/core/cuda/scan.hpp +0 -258
  53. package/include/opencv2/core/cuda/simd_functions.hpp +0 -869
  54. package/include/opencv2/core/cuda/transform.hpp +0 -75
  55. package/include/opencv2/core/cuda/type_traits.hpp +0 -90
  56. package/include/opencv2/core/cuda/utility.hpp +0 -230
  57. package/include/opencv2/core/cuda/vec_distance.hpp +0 -232
  58. package/include/opencv2/core/cuda/vec_math.hpp +0 -923
  59. package/include/opencv2/core/cuda/vec_traits.hpp +0 -288
  60. package/include/opencv2/core/cuda/warp.hpp +0 -139
  61. package/include/opencv2/core/cuda/warp_reduce.hpp +0 -76
  62. package/include/opencv2/core/cuda/warp_shuffle.hpp +0 -162
  63. package/include/opencv2/core/cuda.hpp +0 -1279
  64. package/include/opencv2/core/cuda.inl.hpp +0 -763
  65. package/include/opencv2/core/cuda_stream_accessor.hpp +0 -86
  66. package/include/opencv2/core/cuda_types.hpp +0 -144
  67. package/include/opencv2/core/cv_cpu_dispatch.h +0 -381
  68. package/include/opencv2/core/cv_cpu_helper.h +0 -550
  69. package/include/opencv2/core/cvdef.h +0 -973
  70. package/include/opencv2/core/cvstd.hpp +0 -190
  71. package/include/opencv2/core/cvstd.inl.hpp +0 -197
  72. package/include/opencv2/core/cvstd_wrapper.hpp +0 -154
  73. package/include/opencv2/core/detail/async_promise.hpp +0 -71
  74. package/include/opencv2/core/detail/dispatch_helper.impl.hpp +0 -49
  75. package/include/opencv2/core/detail/exception_ptr.hpp +0 -27
  76. package/include/opencv2/core/directx.hpp +0 -184
  77. package/include/opencv2/core/dualquaternion.hpp +0 -979
  78. package/include/opencv2/core/dualquaternion.inl.hpp +0 -487
  79. package/include/opencv2/core/eigen.hpp +0 -402
  80. package/include/opencv2/core/fast_math.hpp +0 -433
  81. package/include/opencv2/core/hal/hal.hpp +0 -256
  82. package/include/opencv2/core/hal/interface.h +0 -190
  83. package/include/opencv2/core/hal/intrin.hpp +0 -939
  84. package/include/opencv2/core/hal/intrin_avx.hpp +0 -3177
  85. package/include/opencv2/core/hal/intrin_avx512.hpp +0 -3090
  86. package/include/opencv2/core/hal/intrin_cpp.hpp +0 -3321
  87. package/include/opencv2/core/hal/intrin_forward.hpp +0 -191
  88. package/include/opencv2/core/hal/intrin_lasx.hpp +0 -3236
  89. package/include/opencv2/core/hal/intrin_msa.hpp +0 -1887
  90. package/include/opencv2/core/hal/intrin_neon.hpp +0 -2610
  91. package/include/opencv2/core/hal/intrin_rvv.hpp +0 -3320
  92. package/include/opencv2/core/hal/intrin_rvv071.hpp +0 -2545
  93. package/include/opencv2/core/hal/intrin_rvv_scalable.hpp +0 -2080
  94. package/include/opencv2/core/hal/intrin_sse.hpp +0 -3467
  95. package/include/opencv2/core/hal/intrin_sse_em.hpp +0 -180
  96. package/include/opencv2/core/hal/intrin_vsx.hpp +0 -1608
  97. package/include/opencv2/core/hal/intrin_wasm.hpp +0 -2782
  98. package/include/opencv2/core/hal/msa_macros.h +0 -1558
  99. package/include/opencv2/core/hal/simd_utils.impl.hpp +0 -186
  100. package/include/opencv2/core/llapi/llapi.h +0 -102
  101. package/include/opencv2/core/mat.hpp +0 -3775
  102. package/include/opencv2/core/mat.inl.hpp +0 -3422
  103. package/include/opencv2/core/matx.hpp +0 -1536
  104. package/include/opencv2/core/neon_utils.hpp +0 -128
  105. package/include/opencv2/core/ocl.hpp +0 -917
  106. package/include/opencv2/core/ocl_genbase.hpp +0 -69
  107. package/include/opencv2/core/opencl/ocl_defs.hpp +0 -82
  108. package/include/opencv2/core/opencl/opencl_info.hpp +0 -212
  109. package/include/opencv2/core/opencl/opencl_svm.hpp +0 -81
  110. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_clblas.hpp +0 -602
  111. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_clfft.hpp +0 -146
  112. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp +0 -371
  113. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_core_wrappers.hpp +0 -272
  114. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp +0 -62
  115. package/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp +0 -42
  116. package/include/opencv2/core/opencl/runtime/opencl_clblas.hpp +0 -53
  117. package/include/opencv2/core/opencl/runtime/opencl_clfft.hpp +0 -53
  118. package/include/opencv2/core/opencl/runtime/opencl_core.hpp +0 -84
  119. package/include/opencv2/core/opencl/runtime/opencl_core_wrappers.hpp +0 -47
  120. package/include/opencv2/core/opencl/runtime/opencl_gl.hpp +0 -53
  121. package/include/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp +0 -47
  122. package/include/opencv2/core/opencl/runtime/opencl_svm_20.hpp +0 -48
  123. package/include/opencv2/core/opencl/runtime/opencl_svm_definitions.hpp +0 -42
  124. package/include/opencv2/core/opencl/runtime/opencl_svm_hsa_extension.hpp +0 -166
  125. package/include/opencv2/core/opengl.hpp +0 -733
  126. package/include/opencv2/core/openvx/ovx_defs.hpp +0 -48
  127. package/include/opencv2/core/operations.hpp +0 -610
  128. package/include/opencv2/core/optim.hpp +0 -302
  129. package/include/opencv2/core/ovx.hpp +0 -28
  130. package/include/opencv2/core/parallel/backend/parallel_for.openmp.hpp +0 -72
  131. package/include/opencv2/core/parallel/backend/parallel_for.tbb.hpp +0 -153
  132. package/include/opencv2/core/parallel/parallel_backend.hpp +0 -90
  133. package/include/opencv2/core/persistence.hpp +0 -1350
  134. package/include/opencv2/core/private/cv_cpu_include_simd_declarations.hpp +0 -30
  135. package/include/opencv2/core/private.cuda.hpp +0 -169
  136. package/include/opencv2/core/private.hpp +0 -896
  137. package/include/opencv2/core/quaternion.hpp +0 -1696
  138. package/include/opencv2/core/quaternion.inl.hpp +0 -1063
  139. package/include/opencv2/core/saturate.hpp +0 -180
  140. package/include/opencv2/core/simd_intrinsics.hpp +0 -87
  141. package/include/opencv2/core/softfloat.hpp +0 -514
  142. package/include/opencv2/core/sse_utils.hpp +0 -652
  143. package/include/opencv2/core/traits.hpp +0 -417
  144. package/include/opencv2/core/types.hpp +0 -2457
  145. package/include/opencv2/core/types_c.h +0 -2126
  146. package/include/opencv2/core/utility.hpp +0 -1229
  147. package/include/opencv2/core/utils/allocator_stats.hpp +0 -29
  148. package/include/opencv2/core/utils/allocator_stats.impl.hpp +0 -158
  149. package/include/opencv2/core/utils/buffer_area.private.hpp +0 -136
  150. package/include/opencv2/core/utils/configuration.private.hpp +0 -22
  151. package/include/opencv2/core/utils/filesystem.hpp +0 -82
  152. package/include/opencv2/core/utils/filesystem.private.hpp +0 -66
  153. package/include/opencv2/core/utils/fp_control.private.hpp +0 -29
  154. package/include/opencv2/core/utils/fp_control_utils.hpp +0 -69
  155. package/include/opencv2/core/utils/instrumentation.hpp +0 -125
  156. package/include/opencv2/core/utils/lock.private.hpp +0 -119
  157. package/include/opencv2/core/utils/logger.defines.hpp +0 -42
  158. package/include/opencv2/core/utils/logger.hpp +0 -218
  159. package/include/opencv2/core/utils/logtag.hpp +0 -28
  160. package/include/opencv2/core/utils/plugin_loader.private.hpp +0 -165
  161. package/include/opencv2/core/utils/tls.hpp +0 -235
  162. package/include/opencv2/core/utils/trace.hpp +0 -252
  163. package/include/opencv2/core/utils/trace.private.hpp +0 -421
  164. package/include/opencv2/core/va_intel.hpp +0 -75
  165. package/include/opencv2/core/version.hpp +0 -26
  166. package/include/opencv2/core/vsx_utils.hpp +0 -1047
  167. package/include/opencv2/core.hpp +0 -3365
  168. package/include/opencv2/imgcodecs/imgcodecs.hpp +0 -48
  169. package/include/opencv2/imgcodecs/imgcodecs_c.h +0 -1
  170. package/include/opencv2/imgcodecs/ios.h +0 -59
  171. package/include/opencv2/imgcodecs/legacy/constants_c.h +0 -54
  172. package/include/opencv2/imgcodecs/macosx.h +0 -20
  173. package/include/opencv2/imgcodecs.hpp +0 -407
  174. package/include/opencv2/imgproc/bindings.hpp +0 -34
  175. package/include/opencv2/imgproc/detail/gcgraph.hpp +0 -395
  176. package/include/opencv2/imgproc/hal/hal.hpp +0 -246
  177. package/include/opencv2/imgproc/hal/interface.h +0 -46
  178. package/include/opencv2/imgproc/imgproc.hpp +0 -48
  179. package/include/opencv2/imgproc/imgproc_c.h +0 -1177
  180. package/include/opencv2/imgproc/segmentation.hpp +0 -141
  181. package/include/opencv2/imgproc/types_c.h +0 -659
  182. package/include/opencv2/imgproc.hpp +0 -5035
  183. package/include/opencv2/opencv_modules.hpp +0 -17
  184. package/libs/libjpeg-turbo.lib +0 -0
  185. package/libs/libpng.lib +0 -0
  186. package/libs/opencv_core470.lib +0 -0
  187. package/libs/opencv_imgcodecs470.lib +0 -0
  188. package/libs/opencv_imgproc470.lib +0 -0
  189. package/libs/zlib.lib +0 -0
  190. /package/dist/{keyCodes.js → keyCodes.cjs} +0 -0
  191. /package/dist/{keyCodes.d.ts → keyCodes.d.cts} +0 -0
package/src/index.mts ADDED
@@ -0,0 +1,454 @@
1
+ import EventEmitter from 'events'
2
+ import path from "path";
3
+ import fs from "fs";
4
+
5
+
6
+ // @ts-ignore
7
+ import nodeGypBuild = require('node-gyp-build');
8
+
9
+ import {keyCodes, KeyCodeHelper} from "./keyCodes.mjs";
10
+ import { __dirnameLocal } from "./dirnameLocal.mjs";
11
+
12
+ const bindings = nodeGypBuild(path.resolve(__dirnameLocal, ".."));
13
+
14
+
15
+
16
+
17
+ /**
18
+ * Represents the data of a window.
19
+ */
20
+ export type WindowData = {
21
+ width: number;
22
+ height: number;
23
+ x: number;
24
+ y: number;
25
+ };
26
+
27
+ /**
28
+ * Represents the data of an image.
29
+ */
30
+ export type ImageData = {
31
+ width: number;
32
+ height: number;
33
+ data: Uint8Array;
34
+ };
35
+
36
+ /**
37
+ * Represents the result of a template matching operation.
38
+ */
39
+ export type MatchData = {
40
+ minValue: number;
41
+ maxValue: number;
42
+ minLocation: { x: number; y: number };
43
+ maxLocation: { x: number; y: number };
44
+ };
45
+
46
+ export type GetWindowData = (windowName: string) => WindowData;
47
+
48
+ export type CaptureWindow = (windowName: string) => Buffer;
49
+
50
+ /**
51
+ * The handler to listen to key-events.
52
+ * @param callback - The callback function to handle key-down events.
53
+ */
54
+ export type SetKeyCallback = (callback: (keyCode: number) => void) => void;
55
+
56
+ /**
57
+ * The handler to stop thread listening to key-events.
58
+ * @param callback - The callback function to handle key-down events.
59
+ */
60
+ export type UnsetKeyCallback = () => void;
61
+
62
+
63
+ /**
64
+ * Function type for moving the mouse.
65
+ */
66
+ export type MouseMove = (posX: number, posY: number) => boolean;
67
+
68
+ /**
69
+ * Function type for simulating a mouse click.
70
+ */
71
+ export type MouseClick = (button?: "left" | "middle" | "right") => boolean;
72
+
73
+ /**
74
+ * Function type for simulating typing.
75
+ */
76
+ export type TypeString = (stringToType: string, delay?: number) => boolean;
77
+
78
+ /**
79
+ * Function type for simulating key press and release.
80
+ */
81
+ export type PressKey = (keyCode: number) => boolean;
82
+
83
+ /**
84
+ * Function type for simulating a mouse drag operation.
85
+ */
86
+ export type MouseDrag = (
87
+ starX: number,
88
+ startY: number,
89
+ endX: Number,
90
+ endY: number,
91
+ speed?: number
92
+ ) => boolean;
93
+
94
+ /**
95
+ * Represents a point in a two-dimensional space.
96
+ */
97
+ export type Point = [x: number, y: number];
98
+
99
+ export type Color = [r: number, g: number, b: number];
100
+
101
+ /**
102
+ * Represents a region of interest in an image.
103
+ */
104
+ export type ROI = [x: number, y: number, width: number, height: number];
105
+
106
+ export type Imread = (path: string) => ImageData;
107
+
108
+ export type Imwrite = (image: ImageData) => Buffer;
109
+
110
+ export type MatchTemplate = (
111
+ image: ImageData,
112
+ template: ImageData,
113
+ method?: number | null,
114
+ mask?: ImageData
115
+ ) => MatchData;
116
+
117
+ export type Blur = (
118
+ image: ImageData,
119
+ sizeX: number,
120
+ sizeY: number
121
+ ) => ImageData;
122
+ export type BgrToGray = (image: ImageData) => ImageData;
123
+
124
+ export type DrawRectangle = (
125
+ image: ImageData,
126
+ start: Point,
127
+ end: Point,
128
+ rgb: Color,
129
+ thickness: number
130
+ ) => ImageData;
131
+ export type GetRegion = (image: ImageData, region: ROI) => ImageData;
132
+
133
+ export type TextRecognition = (trainedDataPath: string, dataLang: string, imagePath: string) => string;
134
+ export type CaptureScreenAsync = () => Promise<Buffer>;
135
+
136
+ const {
137
+ setKeyDownCallback,
138
+ setKeyUpCallback,
139
+ unsetKeyDownCallback,
140
+ unsetKeyUpCallback,
141
+ getWindowData,
142
+ captureWindowN,
143
+ captureScreenAsync,
144
+ mouseMove,
145
+ mouseClick,
146
+ mouseDrag,
147
+ typeString,
148
+ pressKey,
149
+ imread,
150
+ imwrite,
151
+ matchTemplate,
152
+ blur,
153
+ bgrToGray,
154
+ drawRectangle,
155
+ getRegion,
156
+ textRecognition
157
+ }: {
158
+ setKeyDownCallback: SetKeyCallback;
159
+ setKeyUpCallback: SetKeyCallback;
160
+ unsetKeyDownCallback: UnsetKeyCallback;
161
+ unsetKeyUpCallback: UnsetKeyCallback;
162
+ getWindowData: GetWindowData;
163
+ captureWindowN: CaptureWindow;
164
+ mouseMove: MouseMove;
165
+ mouseClick: MouseClick;
166
+ mouseDrag: MouseDrag;
167
+ typeString: TypeString;
168
+ pressKey: PressKey;
169
+ imread: Imread;
170
+ imwrite: Imwrite;
171
+ matchTemplate: MatchTemplate;
172
+ blur: Blur;
173
+ bgrToGray: BgrToGray;
174
+ drawRectangle: DrawRectangle;
175
+ getRegion: GetRegion;
176
+ textRecognition: TextRecognition;
177
+ captureScreenAsync: CaptureScreenAsync;
178
+ } = bindings;
179
+
180
+ const rawPressKey = pressKey;
181
+
182
+ /**
183
+ * Captures a window and saves it to a file.
184
+ * @param windowName - The name of the window to capture.
185
+ * @param path - The file path to save the captured image.
186
+ * @returns True if the capture and save operation is successful, otherwise false.
187
+ */
188
+ function captureWindow(windowName: string, path: string): boolean {
189
+ const buffer = captureWindowN(windowName);
190
+ if (!buffer) return false;
191
+ fs.writeFileSync(path, new Uint8Array(buffer));
192
+ return true;
193
+ }
194
+
195
+
196
+ /**
197
+ * Captures a screen and saves it to a file.
198
+ * @param path - The file path to save the captured image.
199
+ * @returns True if the capture and save operation is successful, otherwise false.
200
+ */
201
+ function captureScreenToFile(path: string): Promise<boolean> {
202
+ return new Promise((resolve, reject) => {
203
+ captureScreenAsync().then((buffer) => {
204
+ fs.writeFileSync(path, new Uint8Array(buffer))
205
+ resolve(true)
206
+ }).catch((err => {
207
+ reject(err)
208
+ }));
209
+
210
+
211
+ })
212
+ }
213
+
214
+
215
+
216
+ /**
217
+ * Interface representing a private keyboard listener that extends EventEmitter.
218
+ * It declares event handlers for native keyboard events, which are forwarded from
219
+ * the C++ bindings using thread-safe callbacks.
220
+ */
221
+ interface KeyboardListenerPrivate extends EventEmitter {
222
+ /**
223
+ * Registers an event handler for the 'keyDown' event.
224
+ * This event is fired when a key is pressed down. The C++ native binding calls
225
+ * this callback using a thread-safe mechanism (via Napi::ThreadSafeFunction).
226
+ * @param event - The event name ('keyDown').
227
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
228
+ * @returns The current instance for method chaining.
229
+ */
230
+ on(
231
+ event: "keyDown",
232
+ callback: (data: { keyCode: number; keyName: string }) => void
233
+ ): this;
234
+
235
+ /**
236
+ * Registers an event handler for the 'keyUp' event.
237
+ * This event is fired when a key is released. The underlying C++ code safely
238
+ * invokes this callback from a background thread using a thread-safe function.
239
+ * @param event - The event name ('keyUp').
240
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
241
+ * @returns The current instance for method chaining.
242
+ */
243
+ on(
244
+ event: "keyUp",
245
+ callback: (data: { keyCode: number; keyName: string }) => void
246
+ ): this;
247
+ }
248
+
249
+
250
+ /**
251
+ * Class that implements a private keyboard listener.
252
+ * This class leverages native C++ bindings to hook into system keyboard events.
253
+ * The C++ layer uses global ThreadSafeFunction objects to safely dispatch events
254
+ * (using a dedicated monitoring thread, mutexes, and atomic flags) to JavaScript.
255
+ * @extends EventEmitter
256
+ */
257
+ class KeyboardListenerPrivate extends EventEmitter {
258
+
259
+ /**
260
+ * Constructs the keyboard listener and sets up native callbacks.
261
+ * The callbacks (set via setKeyDownCallback and setKeyUpCallback) are defined in the
262
+ * C++ binding layer. They are responsible for invoking these JavaScript callbacks
263
+ * in a thread-safe manner once a key event is detected.
264
+ */
265
+ constructor() {
266
+ super();
267
+ // Set the callback for key down events.
268
+ setKeyDownCallback((keyCode: number) => {
269
+ // Look up the human-readable key name from a mapping.
270
+ const keyName: string | undefined = keyCodes.get(keyCode.toString());
271
+ // Emit the 'keyDown' event to all registered JavaScript listeners.
272
+ this.emit("keyDown", {
273
+ keyCode,
274
+ keyName,
275
+ });
276
+ });
277
+ // Set the callback for key up events.
278
+ setKeyUpCallback((keyCode: number) => {
279
+ // Look up the human-readable key name from a mapping.
280
+ const keyName: string | undefined = keyCodes.get(keyCode.toString());
281
+ // Emit the 'keyUp' event to all registered JavaScript listeners.
282
+ this.emit("keyUp", {
283
+ keyCode,
284
+ keyName,
285
+ });
286
+ });
287
+ }
288
+ }
289
+
290
+
291
+ /**
292
+ * A singleton manager for the KeyboardListenerPrivate instance.
293
+ * This class ensures that only one native keyboard listener is active at any time.
294
+ * When the listener is destroyed, it calls unsetKeyDownCallback and unsetKeyUpCallback
295
+ * to clean up native resources, mirroring the cleanup logic in the C++ bindings.
296
+ */
297
+ class KeyboardListener {
298
+ /**
299
+ * Holds the singleton instance of KeyboardListenerPrivate.
300
+ */
301
+ private static listenerInstance: KeyboardListenerPrivate | null = null;
302
+
303
+ /**
304
+ * Returns the singleton instance of KeyboardListenerPrivate. If not already created,
305
+ * it instantiates a new instance and sets up the native callbacks.
306
+ * @returns The active KeyboardListenerPrivate instance.
307
+ */
308
+ static listener() {
309
+ if (!this.listenerInstance) {
310
+ this.listenerInstance = new KeyboardListenerPrivate();
311
+ }
312
+ return this.listenerInstance;
313
+ }
314
+
315
+ /**
316
+ * Destroys the current KeyboardListenerPrivate instance and cleans up native callbacks.
317
+ * This method calls unsetKeyDownCallback and unsetKeyUpCallback to release any
318
+ * native resources (such as the global ThreadSafeFunctions) and stops the monitoring thread.
319
+ */
320
+ static destroy() {
321
+ this.listenerInstance = null;
322
+ unsetKeyDownCallback();
323
+ unsetKeyUpCallback();
324
+ }
325
+ }
326
+
327
+
328
+ /**
329
+ * Represents the OpenCV class that provides image processing functionality.
330
+ */
331
+ class OpenCV {
332
+ imageData: ImageData;
333
+
334
+ /**
335
+ * Represents the OpenCV class that provides image processing functionality.
336
+ */
337
+ constructor(image: string | ImageData) {
338
+ if (typeof image === "string") {
339
+ this.imageData = imread(image);
340
+ } else {
341
+ this.imageData = image;
342
+ }
343
+ }
344
+
345
+ /**
346
+ * The width of the image.
347
+ */
348
+ get width() {
349
+ return this.imageData.width;
350
+ }
351
+
352
+ /**
353
+ * The height of the image.
354
+ */
355
+ get height() {
356
+ return this.imageData.height;
357
+ }
358
+
359
+ /**
360
+ * Matches a template image within the current image.
361
+ * @param template - The template image data to search for.
362
+ * @param method - The template matching method (optional).
363
+ * @param mask - The optional mask image data to apply the operation (optional).
364
+ * @returns The result of the template matching operation.
365
+ */
366
+ matchTemplate(template: ImageData, method?: number | null, mask?: ImageData) {
367
+ return matchTemplate(this.imageData, template, method, mask);
368
+ }
369
+
370
+ /**
371
+ * Applies a blur filter to the image.
372
+ * @param sizeX - The horizontal size of the blur filter.
373
+ * @param sizeY - The vertical size of the blur filter.
374
+ * @returns A new OpenCV instance with the blurred image data.
375
+ */
376
+ blur(sizeX: number, sizeY: number) {
377
+ return new OpenCV(blur(this.imageData, sizeX, sizeY));
378
+ }
379
+
380
+ /**
381
+ * Converts the image from BGR to grayscale.
382
+ * @returns A new OpenCV instance with the grayscale image data.
383
+ */
384
+ bgrToGray() {
385
+ return new OpenCV(bgrToGray(this.imageData));
386
+ }
387
+
388
+ /**
389
+ * Draws a rectangle on the image.
390
+ * @param start - The starting point of the rectangle.
391
+ * @param end - The ending point of the rectangle.
392
+ * @param rgb - The color (RGB) of the rectangle.
393
+ * @param thickness - The thickness of the rectangle's border.
394
+ * @returns A new OpenCV instance with the image containing the drawn rectangle.
395
+ */
396
+ drawRectangle(start: Point, end: Point, rgb: Color, thickness: number) {
397
+ return new OpenCV(
398
+ drawRectangle(this.imageData, start, end, rgb, thickness)
399
+ );
400
+ }
401
+
402
+ /**
403
+ * Extracts a region of interest (ROI) from the image.
404
+ * @param region - The region of interest defined as [x, y, width, height].
405
+ * @returns A new OpenCV instance with the extracted region of interest.
406
+ */
407
+ getRegion(region: ROI) {
408
+ return new OpenCV(getRegion(this.imageData, region));
409
+ }
410
+
411
+ /**
412
+ * Writes the image data to a file.
413
+ * @param path - The file path to save the image.
414
+ */
415
+ imwrite(path: string) {
416
+ const buffer = imwrite(this.imageData);
417
+ if (!buffer) return;
418
+ fs.writeFileSync(path, new Uint8Array(buffer));
419
+ }
420
+ }
421
+ function keyPress(keyCode: number, repeat?: number): Promise<boolean> {
422
+ return new Promise((resolve, reject) => {
423
+ if(!repeat) {
424
+ let result = rawPressKey(keyCode)
425
+ if(!result) reject('Something went wrong');
426
+ return resolve(true);
427
+ }
428
+ for(let i = 0; i <= repeat; i++) {
429
+ let result = rawPressKey(keyCode);
430
+ if(!result) reject('Something went wrong');
431
+ }
432
+ return resolve(true);
433
+ })
434
+ }
435
+
436
+
437
+
438
+
439
+ export {
440
+ getWindowData,
441
+ captureWindow,
442
+ captureWindowN,
443
+ mouseMove,
444
+ mouseClick,
445
+ mouseDrag,
446
+ typeString,
447
+ keyPress,
448
+ rawPressKey,
449
+ KeyCodeHelper,
450
+ textRecognition,
451
+ captureScreenToFile,
452
+ KeyboardListener,
453
+ OpenCV
454
+ };
@@ -0,0 +1,203 @@
1
+ export const keyCodes = new Map(
2
+ Object.entries({
3
+ 8: "Backspace",
4
+ 9: "Tab",
5
+ 13: "Enter",
6
+ 16: "Shift",
7
+ 17: "Ctrl",
8
+ 18: "Alt",
9
+ 20: "CapsLock",
10
+ 27: "Escape",
11
+ 32: "Space",
12
+ 33: "PageUp",
13
+ 34: "PageDown",
14
+ 35: "End",
15
+ 36: "Home",
16
+ 37: "ArrowLeft",
17
+ 38: "ArrowUp",
18
+ 39: "ArrowRight",
19
+ 40: "ArrowDown",
20
+ 45: "Insert",
21
+ 46: "Delete",
22
+ 48: "0",
23
+ 49: "1",
24
+ 50: "2",
25
+ 51: "3",
26
+ 52: "4",
27
+ 53: "5",
28
+ 54: "6",
29
+ 55: "7",
30
+ 56: "8",
31
+ 57: "9",
32
+ 65: "A",
33
+ 66: "B",
34
+ 67: "C",
35
+ 68: "D",
36
+ 69: "E",
37
+ 70: "F",
38
+ 71: "G",
39
+ 72: "H",
40
+ 73: "I",
41
+ 74: "J",
42
+ 75: "K",
43
+ 76: "L",
44
+ 77: "M",
45
+ 78: "N",
46
+ 79: "O",
47
+ 80: "P",
48
+ 81: "Q",
49
+ 82: "R",
50
+ 83: "S",
51
+ 84: "T",
52
+ 85: "U",
53
+ 86: "V",
54
+ 87: "W",
55
+ 88: "X",
56
+ 89: "Y",
57
+ 90: "Z",
58
+ 91: "MetaLeft",
59
+ 92: "MetaRight",
60
+ 93: "ContextMenu",
61
+ 96: "Numpad0",
62
+ 97: "Numpad1",
63
+ 98: "Numpad2",
64
+ 99: "Numpad3",
65
+ 100: "Numpad4",
66
+ 101: "Numpad5",
67
+ 102: "Numpad6",
68
+ 103: "Numpad7",
69
+ 104: "Numpad8",
70
+ 105: "Numpad9",
71
+ 106: "NumpadMultiply",
72
+ 107: "NumpadAdd",
73
+ 109: "NumpadSubtract",
74
+ 110: "NumpadDecimal",
75
+ 111: "NumpadDivide",
76
+ 112: "F1",
77
+ 113: "F2",
78
+ 114: "F3",
79
+ 115: "F4",
80
+ 116: "F5",
81
+ 117: "F6",
82
+ 118: "F7",
83
+ 119: "F8",
84
+ 120: "F9",
85
+ 121: "F10",
86
+ 122: "F11",
87
+ 123: "F12",
88
+ 144: "NumLock",
89
+ 145: "ScrollLock",
90
+ 186: "Semicolon",
91
+ 187: "Equal",
92
+ 188: "Comma",
93
+ 189: "Minus",
94
+ 190: "Period",
95
+ 191: "Slash",
96
+ 192: "Backquote",
97
+ 219: "BracketLeft",
98
+ 220: "Backslash",
99
+ 221: "BracketRight",
100
+ 222: "Quote",
101
+ })
102
+ );
103
+
104
+ export enum KeyCodeHelper {
105
+ "Backspace" = 8,
106
+ "Tab" = 9,
107
+ "Enter" = 13,
108
+ "Shift" = 16,
109
+ "Ctrl" = 17,
110
+ "Alt" = 18,
111
+ "CapsLock" = 20,
112
+ "Escape" = 27,
113
+ "Space" = 32,
114
+ "PageUp"= 33,
115
+ "PageDown" = 34,
116
+ "End" = 35,
117
+ "Home" = 36,
118
+ "ArrowLeft" = 37,
119
+ "ArrowUp" = 38,
120
+ "ArrowRight" = 39,
121
+ "ArrowDown" = 40,
122
+ "Insert" = 45,
123
+ "Delete" = 46,
124
+ "Zero" = 48,
125
+ "One" = 49,
126
+ "Two" = 50,
127
+ "Three" = 51,
128
+ "Four" = 52,
129
+ "Five" = 53,
130
+ "Six" = 54,
131
+ "Seven" = 55,
132
+ "Eight" = 56,
133
+ "Nine" = 57,
134
+ "A" = 65,
135
+ "B" = 66,
136
+ "C" = 67,
137
+ "D" = 68,
138
+ "E" = 69,
139
+ "F" = 70,
140
+ "G" = 71,
141
+ "H" = 72,
142
+ "I" = 73,
143
+ "J" = 74,
144
+ "K" = 75,
145
+ "L" = 76,
146
+ "M" = 77,
147
+ "N" = 78,
148
+ "O" = 79,
149
+ "P" = 80,
150
+ "Q" = 81,
151
+ "R" = 82,
152
+ "S" = 83,
153
+ "T" = 84,
154
+ "U" = 85,
155
+ "V" = 86,
156
+ "W" = 87,
157
+ "X" = 88,
158
+ "Y" = 89,
159
+ "Z" = 90,
160
+ "MetaLeft" = 91,
161
+ "MetaRight" = 92,
162
+ "ContextMenu" = 93,
163
+ "Numpad0" = 96,
164
+ "Numpad1" = 97,
165
+ "Numpad2" = 98,
166
+ "Numpad3" = 99,
167
+ "Numpad4" = 100,
168
+ "Numpad5" = 101,
169
+ "Numpad6" = 102,
170
+ "Numpad7" = 103,
171
+ "Numpad8" = 104,
172
+ "Numpad9" = 105,
173
+ "NumpadMultiply" = 106,
174
+ "NumpadAdd" = 107,
175
+ "NumpadSubtract" = 109,
176
+ "NumpadDecimal" = 110,
177
+ "NumpadDivide" = 111,
178
+ "F1" = 112,
179
+ "F2" = 113,
180
+ "F3" = 114,
181
+ "F4" = 115,
182
+ "F5" = 116,
183
+ "F6" = 117,
184
+ "F7" = 118,
185
+ "F8" = 119,
186
+ "F9" = 120,
187
+ "F10" = 121,
188
+ "F11" = 122,
189
+ "F12" = 123,
190
+ "NumLock" = 144,
191
+ "ScrollLock" = 145,
192
+ "Semicolon" = 186,
193
+ "Equal" = 187,
194
+ "Comma" = 188,
195
+ "Minus" = 189,
196
+ "Period" = 190,
197
+ "Slash" = 191,
198
+ "Backquote" = 192,
199
+ "BracketLeft" = 219,
200
+ "Backslash" = 220,
201
+ "BracketRight" = 221,
202
+ "Quote" = 222,
203
+ }