node-native-win-utils 1.3.0 → 1.3.2-alpha.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.
@@ -1,205 +1,242 @@
1
- #include <napi.h>
2
- #include <Windows.h>
3
- #include <thread>
4
- #include <iostream>
5
-
6
- // Global variable to store the JavaScript callback function
7
- Napi::ThreadSafeFunction globalJsCallbackKeyDown;
8
- Napi::ThreadSafeFunction globalJsCallbackKeyUp;
9
-
10
- // Global variable to store the previous key state
11
- bool isKeyPressed = false;
12
- bool handleKeyUp = false;
13
- bool handleKeyDown = false;
14
- bool monitorThreadRunning = false;
15
- int previousKeyState;
16
- LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
17
- {
18
- if (nCode >= 0)
19
- {
20
- if (wParam == WM_KEYDOWN)
21
- {
22
- KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
23
- int keyCode = kbdStruct->vkCode;
24
-
25
- // Handle the keyCode here or call your JavaScript callback
26
- // std::cout << "Key Code: " << keyCode << std::endl;
27
-
28
- if (keyCode != previousKeyState || !isKeyPressed)
29
- {
30
- isKeyPressed = true;
31
- previousKeyState = keyCode;
32
- if (handleKeyDown)
33
- {
34
- int *keyCodeCopy = new int(keyCode); // Create a copy of the keyCode value
35
-
36
- napi_status status = globalJsCallbackKeyDown.BlockingCall(
37
- keyCodeCopy,
38
- [](Napi::Env env, Napi::Function jsCallback, int *keyCodePtr)
39
- {
40
- Napi::HandleScope scope(env);
41
- int keyCode = *keyCodePtr;
42
- jsCallback.Call({Napi::Number::New(env, keyCode)});
43
- delete keyCodePtr; // Clean up the dynamically allocated keyCode copy
44
- });
45
-
46
- if (status != napi_ok)
47
- {
48
- // Handle the error
49
- }
50
- }
51
- }
52
- }
53
- else if (wParam == WM_KEYUP)
54
- {
55
- KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
56
- int keyCode = kbdStruct->vkCode;
57
-
58
- if (keyCode == previousKeyState)
59
- {
60
- isKeyPressed = false;
61
- if (handleKeyUp)
62
- {
63
- int *keyCodeCopy = new int(keyCode); // Create a copy of the keyCode value
64
- napi_status status = globalJsCallbackKeyUp.BlockingCall(
65
- keyCodeCopy,
66
- [](Napi::Env env, Napi::Function jsCallback, int *keyCodePtr)
67
- {
68
- Napi::HandleScope scope(env);
69
- int keyCode = *keyCodePtr;
70
- jsCallback.Call({Napi::Number::New(env, keyCode)});
71
- delete keyCodePtr; // Clean up the dynamically allocated keyCode copy
72
- });
73
-
74
- if (status != napi_ok)
75
- {
76
- // Handle the error
77
- }
78
- }
79
- }
80
- }
81
- }
82
-
83
- return CallNextHookEx(NULL, nCode, wParam, lParam);
84
- }
85
-
86
- // Function to monitor keyboard events
87
- void MonitorKeyboardEvents()
88
- {
89
- HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
90
- if (keyboardHook == NULL)
91
- {
92
- // Error setting hook
93
- return;
94
- }
95
-
96
- // Main loop to process keyboard events
97
- MSG message;
98
- while (GetMessage(&message, NULL, 0, 0) > 0)
99
- {
100
- TranslateMessage(&message);
101
- DispatchMessage(&message);
102
- }
103
-
104
- UnhookWindowsHookEx(keyboardHook);
105
- }
106
-
107
- // Function called from JavaScript to set the callback function
108
- Napi::Value SetKeyDownCallback(const Napi::CallbackInfo &info)
109
- {
110
- Napi::Env env = info.Env();
111
-
112
- // Get the callback function from the arguments
113
- Napi::Function jsCallback = info[0].As<Napi::Function>();
114
-
115
- // Create a ThreadSafeFunction with the JavaScript callback
116
- globalJsCallbackKeyDown = Napi::ThreadSafeFunction::New(
117
- env,
118
- jsCallback,
119
- "KeyDownCallback",
120
- 0,
121
- 1,
122
- [](Napi::Env)
123
- {
124
- // Finalizer callback (optional)
125
- });
126
- handleKeyDown = true;
127
- if (!monitorThreadRunning)
128
- {
129
- std::thread monitorThread(MonitorKeyboardEvents);
130
- monitorThread.detach();
131
-
132
- monitorThreadRunning = true;
133
- }
134
-
135
- return env.Undefined();
136
- }
137
-
138
- // Function called from JavaScript to set the callback function
139
- Napi::Value SetKeyUpCallback(const Napi::CallbackInfo &info)
140
- {
141
- Napi::Env env = info.Env();
142
-
143
- // Get the callback function from the arguments
144
- Napi::Function jsCallback = info[0].As<Napi::Function>();
145
-
146
- // Create a ThreadSafeFunction with the JavaScript callback
147
- globalJsCallbackKeyUp = Napi::ThreadSafeFunction::New(
148
- env,
149
- jsCallback,
150
- "KeyUpCallback",
151
- 0,
152
- 1,
153
- [](Napi::Env)
154
- {
155
- // Finalizer callback (optional)
156
- });
157
- handleKeyUp = true;
158
- if (!monitorThreadRunning)
159
- {
160
- std::thread monitorThread(MonitorKeyboardEvents);
161
- monitorThread.detach();
162
- monitorThreadRunning = true;
163
- }
164
- return env.Undefined();
165
- }
166
-
167
- Napi::Value TypeString(const Napi::CallbackInfo &info)
168
- {
169
- Napi::Env env = info.Env();
170
-
171
- if (info.Length() < 1 || !info[0].IsString())
172
- {
173
- Napi::TypeError::New(env, "You should provide a string to type").ThrowAsJavaScriptException();
174
- return env.Null();
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());
184
-
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
194
-
195
- SendInput(1, &keyboardInput, sizeof(keyboardInput));
196
-
197
- keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
198
- SendInput(1, &keyboardInput, sizeof(keyboardInput));
199
-
200
- // Sleep for 15 milliseconds between each character input
201
- Sleep(delay);
202
- }
203
-
204
- return Napi::Boolean::New(env, true);
1
+ #include <napi.h>
2
+ #include <Windows.h>
3
+ #include <thread>
4
+ #include <iostream>
5
+
6
+ // Global variable to store the JavaScript callback function
7
+ Napi::ThreadSafeFunction globalJsCallbackKeyDown;
8
+ Napi::ThreadSafeFunction globalJsCallbackKeyUp;
9
+
10
+ // Global variable to store the previous key state
11
+ bool isKeyPressed = false;
12
+ bool handleKeyUp = false;
13
+ bool handleKeyDown = false;
14
+ bool monitorThreadRunning = false;
15
+ int previousKeyState;
16
+ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
17
+ {
18
+ if (nCode >= 0)
19
+ {
20
+ if (wParam == WM_KEYDOWN)
21
+ {
22
+ KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
23
+ int keyCode = kbdStruct->vkCode;
24
+
25
+ // Handle the keyCode here or call your JavaScript callback
26
+ // std::cout << "Key Code: " << keyCode << std::endl;
27
+
28
+ if (keyCode != previousKeyState || !isKeyPressed)
29
+ {
30
+ isKeyPressed = true;
31
+ previousKeyState = keyCode;
32
+ if (handleKeyDown)
33
+ {
34
+ int *keyCodeCopy = new int(keyCode); // Create a copy of the keyCode value
35
+
36
+ napi_status status = globalJsCallbackKeyDown.BlockingCall(
37
+ keyCodeCopy,
38
+ [](Napi::Env env, Napi::Function jsCallback, int *keyCodePtr)
39
+ {
40
+ Napi::HandleScope scope(env);
41
+ int keyCode = *keyCodePtr;
42
+ jsCallback.Call({Napi::Number::New(env, keyCode)});
43
+ delete keyCodePtr; // Clean up the dynamically allocated keyCode copy
44
+ });
45
+
46
+ if (status != napi_ok)
47
+ {
48
+ // Handle the error
49
+ }
50
+ }
51
+ }
52
+ }
53
+ else if (wParam == WM_KEYUP)
54
+ {
55
+ KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
56
+ int keyCode = kbdStruct->vkCode;
57
+
58
+ if (keyCode == previousKeyState)
59
+ {
60
+ isKeyPressed = false;
61
+ if (handleKeyUp)
62
+ {
63
+ int *keyCodeCopy = new int(keyCode); // Create a copy of the keyCode value
64
+ napi_status status = globalJsCallbackKeyUp.BlockingCall(
65
+ keyCodeCopy,
66
+ [](Napi::Env env, Napi::Function jsCallback, int *keyCodePtr)
67
+ {
68
+ Napi::HandleScope scope(env);
69
+ int keyCode = *keyCodePtr;
70
+ jsCallback.Call({Napi::Number::New(env, keyCode)});
71
+ delete keyCodePtr; // Clean up the dynamically allocated keyCode copy
72
+ });
73
+
74
+ if (status != napi_ok)
75
+ {
76
+ // Handle the error
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ return CallNextHookEx(NULL, nCode, wParam, lParam);
84
+ }
85
+
86
+ // Function to monitor keyboard events
87
+ void MonitorKeyboardEvents()
88
+ {
89
+ HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
90
+ if (keyboardHook == NULL)
91
+ {
92
+ // Error setting hook
93
+ return;
94
+ }
95
+
96
+ // Main loop to process keyboard events
97
+ MSG message;
98
+ while (GetMessage(&message, NULL, 0, 0) > 0)
99
+ {
100
+ TranslateMessage(&message);
101
+ DispatchMessage(&message);
102
+ }
103
+
104
+ UnhookWindowsHookEx(keyboardHook);
105
+ }
106
+
107
+ // Function called from JavaScript to set the callback function
108
+ Napi::Value SetKeyDownCallback(const Napi::CallbackInfo &info)
109
+ {
110
+ Napi::Env env = info.Env();
111
+
112
+ // Get the callback function from the arguments
113
+ Napi::Function jsCallback = info[0].As<Napi::Function>();
114
+
115
+ // Create a ThreadSafeFunction with the JavaScript callback
116
+ globalJsCallbackKeyDown = Napi::ThreadSafeFunction::New(
117
+ env,
118
+ jsCallback,
119
+ "KeyDownCallback",
120
+ 0,
121
+ 1,
122
+ [](Napi::Env)
123
+ {
124
+ // Finalizer callback (optional)
125
+ });
126
+ handleKeyDown = true;
127
+ if (!monitorThreadRunning)
128
+ {
129
+ std::thread monitorThread(MonitorKeyboardEvents);
130
+ monitorThread.detach();
131
+
132
+ monitorThreadRunning = true;
133
+ }
134
+
135
+ return env.Undefined();
136
+ }
137
+
138
+ // Function called from JavaScript to set the callback function
139
+ Napi::Value SetKeyUpCallback(const Napi::CallbackInfo &info)
140
+ {
141
+ Napi::Env env = info.Env();
142
+
143
+ // Get the callback function from the arguments
144
+ Napi::Function jsCallback = info[0].As<Napi::Function>();
145
+
146
+ // Create a ThreadSafeFunction with the JavaScript callback
147
+ globalJsCallbackKeyUp = Napi::ThreadSafeFunction::New(
148
+ env,
149
+ jsCallback,
150
+ "KeyUpCallback",
151
+ 0,
152
+ 1,
153
+ [](Napi::Env)
154
+ {
155
+ // Finalizer callback (optional)
156
+ });
157
+ handleKeyUp = true;
158
+ if (!monitorThreadRunning)
159
+ {
160
+ std::thread monitorThread(MonitorKeyboardEvents);
161
+ monitorThread.detach();
162
+ monitorThreadRunning = true;
163
+ }
164
+ return env.Undefined();
165
+ }
166
+
167
+ Napi::Value TypeString(const Napi::CallbackInfo &info)
168
+ {
169
+ Napi::Env env = info.Env();
170
+
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());
184
+
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
194
+
195
+ SendInput(1, &keyboardInput, sizeof(keyboardInput));
196
+
197
+ keyboardInput.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
198
+ SendInput(1, &keyboardInput, sizeof(keyboardInput));
199
+
200
+ // Sleep for 15 milliseconds between each character input
201
+ Sleep(delay);
202
+ }
203
+
204
+ return Napi::Boolean::New(env, true);
205
+ }
206
+
207
+ // Function to simulate key press and release using provided key code
208
+ Napi::Value PressKey(const Napi::CallbackInfo &info)
209
+ {
210
+ Napi::Env env = info.Env();
211
+
212
+ if (info.Length() < 1 || !info[0].IsNumber())
213
+ {
214
+ Napi::TypeError::New(env, "You should provide a key code to type").ThrowAsJavaScriptException();
215
+ return Napi::Boolean::New(env, false);;
216
+ }
217
+
218
+ int keyCode = info[0].As<Napi::Number>().Int32Value();
219
+ int delay = 15;
220
+
221
+ if (info.Length() > 1 && info[1].IsNumber())
222
+ {
223
+ delay = info[1].As<Napi::Number>().Int32Value();
224
+ }
225
+
226
+ // Simulate key press
227
+ INPUT keyboardInput = {0};
228
+ keyboardInput.type = INPUT_KEYBOARD;
229
+ keyboardInput.ki.wVk = keyCode;
230
+ keyboardInput.ki.dwFlags = 0;
231
+ keyboardInput.ki.time = 0;
232
+
233
+ SendInput(1, &keyboardInput, sizeof(keyboardInput));
234
+
235
+ Sleep(delay);
236
+
237
+ // Simulate key release
238
+ keyboardInput.ki.dwFlags = KEYEVENTF_KEYUP;
239
+ SendInput(1, &keyboardInput, sizeof(keyboardInput));
240
+
241
+ return Napi::Boolean::New(env, true);
205
242
  }
package/src/cpp/main.cpp CHANGED
@@ -1,29 +1,30 @@
1
- #include <napi.h>
2
- #include <helpers.cpp>
3
- #include <captureWindow.cpp>
4
- #include <getWindowData.cpp>
5
- #include <keyboard.cpp>
6
- #include <mouse.cpp>
7
- #include <opencv.cpp>
8
-
9
- Napi::Object Init(Napi::Env env, Napi::Object exports)
10
- {
11
- exports.Set("getWindowData", Napi::Function::New(env, GetWindowData));
12
- exports.Set("captureWindowN", Napi::Function::New(env, CaptureWindow));
13
- exports.Set("keyDownHandler", Napi::Function::New(env, SetKeyDownCallback));
14
- exports.Set("keyUpHandler", Napi::Function::New(env, SetKeyUpCallback));
15
- exports.Set("mouseMove", Napi::Function::New(env, MoveMouse));
16
- exports.Set("mouseClick", Napi::Function::New(env, ClickMouse));
17
- exports.Set("mouseDrag", Napi::Function::New(env, DragMouse));
18
- exports.Set("typeString", Napi::Function::New(env, TypeString));
19
- exports.Set("imread", Napi::Function::New(env, Imread));
20
- exports.Set("imwrite", Napi::Function::New(env, Imwrite));
21
- exports.Set("matchTemplate", Napi::Function::New(env, MatchTemplate));
22
- exports.Set("blur", Napi::Function::New(env, Blur));
23
- exports.Set("bgrToGray", Napi::Function::New(env, BgrToGray));
24
- exports.Set("drawRectangle", Napi::Function::New(env, DrawRectangle));
25
- exports.Set("getRegion", Napi::Function::New(env, GetRegion));
26
- return exports;
27
- }
28
-
29
- NODE_API_MODULE(addon, Init)
1
+ #include <napi.h>
2
+ #include <helpers.cpp>
3
+ #include <captureWindow.cpp>
4
+ #include <getWindowData.cpp>
5
+ #include <keyboard.cpp>
6
+ #include <mouse.cpp>
7
+ #include <opencv.cpp>
8
+
9
+ Napi::Object Init(Napi::Env env, Napi::Object exports)
10
+ {
11
+ exports.Set("getWindowData", Napi::Function::New(env, GetWindowData));
12
+ exports.Set("captureWindowN", Napi::Function::New(env, CaptureWindow));
13
+ exports.Set("keyDownHandler", Napi::Function::New(env, SetKeyDownCallback));
14
+ exports.Set("keyUpHandler", Napi::Function::New(env, SetKeyUpCallback));
15
+ exports.Set("mouseMove", Napi::Function::New(env, MoveMouse));
16
+ exports.Set("mouseClick", Napi::Function::New(env, ClickMouse));
17
+ exports.Set("mouseDrag", Napi::Function::New(env, DragMouse));
18
+ exports.Set("typeString", Napi::Function::New(env, TypeString));
19
+ exports.Set("pressKey", Napi::Function::New(env, PressKey));
20
+ exports.Set("imread", Napi::Function::New(env, Imread));
21
+ exports.Set("imwrite", Napi::Function::New(env, Imwrite));
22
+ exports.Set("matchTemplate", Napi::Function::New(env, MatchTemplate));
23
+ exports.Set("blur", Napi::Function::New(env, Blur));
24
+ exports.Set("bgrToGray", Napi::Function::New(env, BgrToGray));
25
+ exports.Set("drawRectangle", Napi::Function::New(env, DrawRectangle));
26
+ exports.Set("getRegion", Napi::Function::New(env, GetRegion));
27
+ return exports;
28
+ }
29
+
30
+ NODE_API_MODULE(addon, Init)