node-native-win-utils 2.1.0 → 2.1.1

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/dist/index.cjs CHANGED
@@ -3,7 +3,7 @@ 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.OpenCV = exports.KeyboardListener = exports.textRecognition = exports.KeyCodeHelper = exports.rawPressKey = exports.typeString = exports.mouseDrag = exports.mouseClick = exports.mouseMove = exports.captureWindowN = exports.getWindowData = void 0;
6
+ exports.OpenCV = exports.KeyboardListener = exports.captureScreenAsync = exports.textRecognition = exports.KeyCodeHelper = exports.rawPressKey = exports.typeString = exports.mouseDrag = exports.mouseClick = exports.mouseMove = exports.captureWindowN = exports.getWindowData = void 0;
7
7
  exports.captureWindow = captureWindow;
8
8
  exports.keyPress = keyPress;
9
9
  exports.captureScreenToFile = captureScreenToFile;
@@ -20,6 +20,7 @@ const bindings = nodeGypBuild(path_1.default.resolve(dirnameLocal_cjs_1.__dirnam
20
20
  const { setKeyDownCallback, setKeyUpCallback, unsetKeyDownCallback, unsetKeyUpCallback, getWindowData, captureWindowN, captureScreenAsync, mouseMove, mouseClick, mouseDrag, typeString, pressKey, imread, imwrite, matchTemplate, blur, bgrToGray, drawRectangle, getRegion, textRecognition } = bindings;
21
21
  exports.getWindowData = getWindowData;
22
22
  exports.captureWindowN = captureWindowN;
23
+ exports.captureScreenAsync = captureScreenAsync;
23
24
  exports.mouseMove = mouseMove;
24
25
  exports.mouseClick = mouseClick;
25
26
  exports.mouseDrag = mouseDrag;
@@ -56,21 +57,36 @@ function captureScreenToFile(path) {
56
57
  });
57
58
  }
58
59
  /**
59
- * Represents a class to listen to keyboard events.
60
+ * Class that implements a private keyboard listener.
61
+ * This class leverages native C++ bindings to hook into system keyboard events.
62
+ * The C++ layer uses global ThreadSafeFunction objects to safely dispatch events
63
+ * (using a dedicated monitoring thread, mutexes, and atomic flags) to JavaScript.
60
64
  * @extends EventEmitter
61
65
  */
62
66
  class KeyboardListenerPrivate extends events_1.default {
67
+ /**
68
+ * Constructs the keyboard listener and sets up native callbacks.
69
+ * The callbacks (set via setKeyDownCallback and setKeyUpCallback) are defined in the
70
+ * C++ binding layer. They are responsible for invoking these JavaScript callbacks
71
+ * in a thread-safe manner once a key event is detected.
72
+ */
63
73
  constructor() {
64
74
  super();
75
+ // Set the callback for key down events.
65
76
  setKeyDownCallback((keyCode) => {
77
+ // Look up the human-readable key name from a mapping.
66
78
  const keyName = keyCodes_cjs_1.keyCodes.get(keyCode.toString());
79
+ // Emit the 'keyDown' event to all registered JavaScript listeners.
67
80
  this.emit("keyDown", {
68
81
  keyCode,
69
82
  keyName,
70
83
  });
71
84
  });
85
+ // Set the callback for key up events.
72
86
  setKeyUpCallback((keyCode) => {
87
+ // Look up the human-readable key name from a mapping.
73
88
  const keyName = keyCodes_cjs_1.keyCodes.get(keyCode.toString());
89
+ // Emit the 'keyUp' event to all registered JavaScript listeners.
74
90
  this.emit("keyUp", {
75
91
  keyCode,
76
92
  keyName,
@@ -78,13 +94,33 @@ class KeyboardListenerPrivate extends events_1.default {
78
94
  });
79
95
  }
80
96
  }
97
+ /**
98
+ * A singleton manager for the KeyboardListenerPrivate instance.
99
+ * This class ensures that only one native keyboard listener is active at any time.
100
+ * When the listener is destroyed, it calls unsetKeyDownCallback and unsetKeyUpCallback
101
+ * to clean up native resources, mirroring the cleanup logic in the C++ bindings.
102
+ */
81
103
  class KeyboardListener {
104
+ /**
105
+ * Holds the singleton instance of KeyboardListenerPrivate.
106
+ */
82
107
  static listenerInstance = null;
108
+ /**
109
+ * Returns the singleton instance of KeyboardListenerPrivate. If not already created,
110
+ * it instantiates a new instance and sets up the native callbacks.
111
+ * @returns The active KeyboardListenerPrivate instance.
112
+ */
83
113
  static listener() {
84
- if (!this.listenerInstance)
114
+ if (!this.listenerInstance) {
85
115
  this.listenerInstance = new KeyboardListenerPrivate();
116
+ }
86
117
  return this.listenerInstance;
87
118
  }
119
+ /**
120
+ * Destroys the current KeyboardListenerPrivate instance and cleans up native callbacks.
121
+ * This method calls unsetKeyDownCallback and unsetKeyUpCallback to release any
122
+ * native resources (such as the global ThreadSafeFunctions) and stops the monitoring thread.
123
+ */
88
124
  static destroy() {
89
125
  this.listenerInstance = null;
90
126
  unsetKeyDownCallback();
package/dist/index.d.cts CHANGED
@@ -82,7 +82,7 @@ export type DrawRectangle = (image: ImageData, start: Point, end: Point, rgb: Co
82
82
  export type GetRegion = (image: ImageData, region: ROI) => ImageData;
83
83
  export type TextRecognition = (trainedDataPath: string, dataLang: string, imagePath: string) => string;
84
84
  export type CaptureScreenAsync = () => Promise<Buffer>;
85
- declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition;
85
+ declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, captureScreenAsync: CaptureScreenAsync, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition;
86
86
  declare const rawPressKey: PressKey;
87
87
  /**
88
88
  * Captures a window and saves it to a file.
@@ -97,20 +97,31 @@ declare function captureWindow(windowName: string, path: string): boolean;
97
97
  * @returns True if the capture and save operation is successful, otherwise false.
98
98
  */
99
99
  declare function captureScreenToFile(path: string): Promise<boolean>;
100
+ /**
101
+ * Interface representing a private keyboard listener that extends EventEmitter.
102
+ * It declares event handlers for native keyboard events, which are forwarded from
103
+ * the C++ bindings using thread-safe callbacks.
104
+ */
100
105
  interface KeyboardListenerPrivate extends EventEmitter {
101
106
  /**
102
- * Event: Fires when a key is pressed down.
107
+ * Registers an event handler for the 'keyDown' event.
108
+ * This event is fired when a key is pressed down. The C++ native binding calls
109
+ * this callback using a thread-safe mechanism (via Napi::ThreadSafeFunction).
103
110
  * @param event - The event name ('keyDown').
104
- * @param callback - The callback function to handle the event.
111
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
112
+ * @returns The current instance for method chaining.
105
113
  */
106
114
  on(event: "keyDown", callback: (data: {
107
115
  keyCode: number;
108
116
  keyName: string;
109
117
  }) => void): this;
110
118
  /**
111
- * Event: Fires when a key is released.
119
+ * Registers an event handler for the 'keyUp' event.
120
+ * This event is fired when a key is released. The underlying C++ code safely
121
+ * invokes this callback from a background thread using a thread-safe function.
112
122
  * @param event - The event name ('keyUp').
113
- * @param callback - The callback function to handle the event.
123
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
124
+ * @returns The current instance for method chaining.
114
125
  */
115
126
  on(event: "keyUp", callback: (data: {
116
127
  keyCode: number;
@@ -118,15 +129,43 @@ interface KeyboardListenerPrivate extends EventEmitter {
118
129
  }) => void): this;
119
130
  }
120
131
  /**
121
- * Represents a class to listen to keyboard events.
132
+ * Class that implements a private keyboard listener.
133
+ * This class leverages native C++ bindings to hook into system keyboard events.
134
+ * The C++ layer uses global ThreadSafeFunction objects to safely dispatch events
135
+ * (using a dedicated monitoring thread, mutexes, and atomic flags) to JavaScript.
122
136
  * @extends EventEmitter
123
137
  */
124
138
  declare class KeyboardListenerPrivate extends EventEmitter {
139
+ /**
140
+ * Constructs the keyboard listener and sets up native callbacks.
141
+ * The callbacks (set via setKeyDownCallback and setKeyUpCallback) are defined in the
142
+ * C++ binding layer. They are responsible for invoking these JavaScript callbacks
143
+ * in a thread-safe manner once a key event is detected.
144
+ */
125
145
  constructor();
126
146
  }
147
+ /**
148
+ * A singleton manager for the KeyboardListenerPrivate instance.
149
+ * This class ensures that only one native keyboard listener is active at any time.
150
+ * When the listener is destroyed, it calls unsetKeyDownCallback and unsetKeyUpCallback
151
+ * to clean up native resources, mirroring the cleanup logic in the C++ bindings.
152
+ */
127
153
  declare class KeyboardListener {
154
+ /**
155
+ * Holds the singleton instance of KeyboardListenerPrivate.
156
+ */
128
157
  private static listenerInstance;
158
+ /**
159
+ * Returns the singleton instance of KeyboardListenerPrivate. If not already created,
160
+ * it instantiates a new instance and sets up the native callbacks.
161
+ * @returns The active KeyboardListenerPrivate instance.
162
+ */
129
163
  static listener(): KeyboardListenerPrivate;
164
+ /**
165
+ * Destroys the current KeyboardListenerPrivate instance and cleans up native callbacks.
166
+ * This method calls unsetKeyDownCallback and unsetKeyUpCallback to release any
167
+ * native resources (such as the global ThreadSafeFunctions) and stops the monitoring thread.
168
+ */
130
169
  static destroy(): void;
131
170
  }
132
171
  /**
@@ -188,4 +227,4 @@ declare class OpenCV {
188
227
  imwrite(path: string): void;
189
228
  }
190
229
  declare function keyPress(keyCode: number, repeat?: number): Promise<boolean>;
191
- export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, KeyboardListener, OpenCV };
230
+ export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, KeyboardListener, OpenCV };
package/dist/index.d.mts CHANGED
@@ -82,7 +82,7 @@ export type DrawRectangle = (image: ImageData, start: Point, end: Point, rgb: Co
82
82
  export type GetRegion = (image: ImageData, region: ROI) => ImageData;
83
83
  export type TextRecognition = (trainedDataPath: string, dataLang: string, imagePath: string) => string;
84
84
  export type CaptureScreenAsync = () => Promise<Buffer>;
85
- declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition;
85
+ declare const getWindowData: GetWindowData, captureWindowN: CaptureWindow, captureScreenAsync: CaptureScreenAsync, mouseMove: MouseMove, mouseClick: MouseClick, mouseDrag: MouseDrag, typeString: TypeString, textRecognition: TextRecognition;
86
86
  declare const rawPressKey: PressKey;
87
87
  /**
88
88
  * Captures a window and saves it to a file.
@@ -97,20 +97,31 @@ declare function captureWindow(windowName: string, path: string): boolean;
97
97
  * @returns True if the capture and save operation is successful, otherwise false.
98
98
  */
99
99
  declare function captureScreenToFile(path: string): Promise<boolean>;
100
+ /**
101
+ * Interface representing a private keyboard listener that extends EventEmitter.
102
+ * It declares event handlers for native keyboard events, which are forwarded from
103
+ * the C++ bindings using thread-safe callbacks.
104
+ */
100
105
  interface KeyboardListenerPrivate extends EventEmitter {
101
106
  /**
102
- * Event: Fires when a key is pressed down.
107
+ * Registers an event handler for the 'keyDown' event.
108
+ * This event is fired when a key is pressed down. The C++ native binding calls
109
+ * this callback using a thread-safe mechanism (via Napi::ThreadSafeFunction).
103
110
  * @param event - The event name ('keyDown').
104
- * @param callback - The callback function to handle the event.
111
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
112
+ * @returns The current instance for method chaining.
105
113
  */
106
114
  on(event: "keyDown", callback: (data: {
107
115
  keyCode: number;
108
116
  keyName: string;
109
117
  }) => void): this;
110
118
  /**
111
- * Event: Fires when a key is released.
119
+ * Registers an event handler for the 'keyUp' event.
120
+ * This event is fired when a key is released. The underlying C++ code safely
121
+ * invokes this callback from a background thread using a thread-safe function.
112
122
  * @param event - The event name ('keyUp').
113
- * @param callback - The callback function to handle the event.
123
+ * @param callback - Function invoked with an object containing the keyCode and keyName.
124
+ * @returns The current instance for method chaining.
114
125
  */
115
126
  on(event: "keyUp", callback: (data: {
116
127
  keyCode: number;
@@ -118,15 +129,43 @@ interface KeyboardListenerPrivate extends EventEmitter {
118
129
  }) => void): this;
119
130
  }
120
131
  /**
121
- * Represents a class to listen to keyboard events.
132
+ * Class that implements a private keyboard listener.
133
+ * This class leverages native C++ bindings to hook into system keyboard events.
134
+ * The C++ layer uses global ThreadSafeFunction objects to safely dispatch events
135
+ * (using a dedicated monitoring thread, mutexes, and atomic flags) to JavaScript.
122
136
  * @extends EventEmitter
123
137
  */
124
138
  declare class KeyboardListenerPrivate extends EventEmitter {
139
+ /**
140
+ * Constructs the keyboard listener and sets up native callbacks.
141
+ * The callbacks (set via setKeyDownCallback and setKeyUpCallback) are defined in the
142
+ * C++ binding layer. They are responsible for invoking these JavaScript callbacks
143
+ * in a thread-safe manner once a key event is detected.
144
+ */
125
145
  constructor();
126
146
  }
147
+ /**
148
+ * A singleton manager for the KeyboardListenerPrivate instance.
149
+ * This class ensures that only one native keyboard listener is active at any time.
150
+ * When the listener is destroyed, it calls unsetKeyDownCallback and unsetKeyUpCallback
151
+ * to clean up native resources, mirroring the cleanup logic in the C++ bindings.
152
+ */
127
153
  declare class KeyboardListener {
154
+ /**
155
+ * Holds the singleton instance of KeyboardListenerPrivate.
156
+ */
128
157
  private static listenerInstance;
158
+ /**
159
+ * Returns the singleton instance of KeyboardListenerPrivate. If not already created,
160
+ * it instantiates a new instance and sets up the native callbacks.
161
+ * @returns The active KeyboardListenerPrivate instance.
162
+ */
129
163
  static listener(): KeyboardListenerPrivate;
164
+ /**
165
+ * Destroys the current KeyboardListenerPrivate instance and cleans up native callbacks.
166
+ * This method calls unsetKeyDownCallback and unsetKeyUpCallback to release any
167
+ * native resources (such as the global ThreadSafeFunctions) and stops the monitoring thread.
168
+ */
130
169
  static destroy(): void;
131
170
  }
132
171
  /**
@@ -188,4 +227,4 @@ declare class OpenCV {
188
227
  imwrite(path: string): void;
189
228
  }
190
229
  declare function keyPress(keyCode: number, repeat?: number): Promise<boolean>;
191
- export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, KeyboardListener, OpenCV };
230
+ export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, KeyboardListener, OpenCV };
package/dist/index.mjs CHANGED
@@ -39,21 +39,36 @@ function captureScreenToFile(path) {
39
39
  });
40
40
  }
41
41
  /**
42
- * Represents a class to listen to keyboard events.
42
+ * Class that implements a private keyboard listener.
43
+ * This class leverages native C++ bindings to hook into system keyboard events.
44
+ * The C++ layer uses global ThreadSafeFunction objects to safely dispatch events
45
+ * (using a dedicated monitoring thread, mutexes, and atomic flags) to JavaScript.
43
46
  * @extends EventEmitter
44
47
  */
45
48
  class KeyboardListenerPrivate extends EventEmitter {
49
+ /**
50
+ * Constructs the keyboard listener and sets up native callbacks.
51
+ * The callbacks (set via setKeyDownCallback and setKeyUpCallback) are defined in the
52
+ * C++ binding layer. They are responsible for invoking these JavaScript callbacks
53
+ * in a thread-safe manner once a key event is detected.
54
+ */
46
55
  constructor() {
47
56
  super();
57
+ // Set the callback for key down events.
48
58
  setKeyDownCallback((keyCode) => {
59
+ // Look up the human-readable key name from a mapping.
49
60
  const keyName = keyCodes.get(keyCode.toString());
61
+ // Emit the 'keyDown' event to all registered JavaScript listeners.
50
62
  this.emit("keyDown", {
51
63
  keyCode,
52
64
  keyName,
53
65
  });
54
66
  });
67
+ // Set the callback for key up events.
55
68
  setKeyUpCallback((keyCode) => {
69
+ // Look up the human-readable key name from a mapping.
56
70
  const keyName = keyCodes.get(keyCode.toString());
71
+ // Emit the 'keyUp' event to all registered JavaScript listeners.
57
72
  this.emit("keyUp", {
58
73
  keyCode,
59
74
  keyName,
@@ -61,13 +76,33 @@ class KeyboardListenerPrivate extends EventEmitter {
61
76
  });
62
77
  }
63
78
  }
79
+ /**
80
+ * A singleton manager for the KeyboardListenerPrivate instance.
81
+ * This class ensures that only one native keyboard listener is active at any time.
82
+ * When the listener is destroyed, it calls unsetKeyDownCallback and unsetKeyUpCallback
83
+ * to clean up native resources, mirroring the cleanup logic in the C++ bindings.
84
+ */
64
85
  class KeyboardListener {
86
+ /**
87
+ * Holds the singleton instance of KeyboardListenerPrivate.
88
+ */
65
89
  static listenerInstance = null;
90
+ /**
91
+ * Returns the singleton instance of KeyboardListenerPrivate. If not already created,
92
+ * it instantiates a new instance and sets up the native callbacks.
93
+ * @returns The active KeyboardListenerPrivate instance.
94
+ */
66
95
  static listener() {
67
- if (!this.listenerInstance)
96
+ if (!this.listenerInstance) {
68
97
  this.listenerInstance = new KeyboardListenerPrivate();
98
+ }
69
99
  return this.listenerInstance;
70
100
  }
101
+ /**
102
+ * Destroys the current KeyboardListenerPrivate instance and cleans up native callbacks.
103
+ * This method calls unsetKeyDownCallback and unsetKeyUpCallback to release any
104
+ * native resources (such as the global ThreadSafeFunctions) and stops the monitoring thread.
105
+ */
71
106
  static destroy() {
72
107
  this.listenerInstance = null;
73
108
  unsetKeyDownCallback();
@@ -174,4 +209,4 @@ function keyPress(keyCode, repeat) {
174
209
  return resolve(true);
175
210
  });
176
211
  }
177
- export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, KeyboardListener, OpenCV };
212
+ export { getWindowData, captureWindow, captureWindowN, mouseMove, mouseClick, mouseDrag, typeString, keyPress, rawPressKey, KeyCodeHelper, textRecognition, captureScreenToFile, captureScreenAsync, KeyboardListener, OpenCV };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-native-win-utils",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "author": "Andrew K.",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/T-Rumibul/node-native-win-utils.git",
package/src/index.mts CHANGED
@@ -449,6 +449,7 @@ export {
449
449
  KeyCodeHelper,
450
450
  textRecognition,
451
451
  captureScreenToFile,
452
+ captureScreenAsync,
452
453
  KeyboardListener,
453
454
  OpenCV
454
455
  };