node-native-win-utils 1.3.1 → 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.
package/src/cpp/mouse.cpp CHANGED
@@ -1,175 +1,175 @@
1
- // patrially used code from https://github.com/octalmage/robotjs witch is under MIT License Copyright (c) 2014 Jason Stallings
2
- #include <napi.h>
3
- #include <windows.h>
4
- /**
5
- * Move the mouse to a specific point.
6
- * @param point The coordinates to move the mouse to (x, y).
7
- */
8
-
9
- Napi::Value MoveMouse(const Napi::CallbackInfo &info)
10
- {
11
- Napi::Env env = info.Env();
12
-
13
- if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber())
14
- {
15
- Napi::TypeError::New(env, "You should provide x and y position of type number").ThrowAsJavaScriptException();
16
- return env.Null();
17
- }
18
-
19
- int posX = info[0].As<Napi::Number>();
20
- int posY = info[1].As<Napi::Number>();
21
-
22
- // Get the screen metrics
23
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
24
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
25
-
26
- // Convert coordinates to absolute values
27
- int absoluteX = static_cast<int>((65536 * posX) / screenWidth);
28
- int absoluteY = static_cast<int>((65536 * posY) / screenHeight);
29
-
30
- // Move the mouse
31
- INPUT mouseInput = {0};
32
- mouseInput.type = INPUT_MOUSE;
33
- mouseInput.mi.dx = absoluteX;
34
- mouseInput.mi.dy = absoluteY;
35
- mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
36
- mouseInput.mi.time = 0; // System will provide the timestamp
37
-
38
- SendInput(1, &mouseInput, sizeof(mouseInput));
39
-
40
- return Napi::Boolean::New(env, true);
41
- }
42
-
43
- Napi::Value ClickMouse(const Napi::CallbackInfo &info)
44
- {
45
- Napi::Env env = info.Env();
46
- std::string button;
47
-
48
- if (info.Length() < 1 || !info[0].IsString())
49
- button = "left";
50
- else
51
- button = info[0].As<Napi::String>();
52
-
53
- WORD mouseEvent = 0;
54
-
55
- if (button == "left")
56
- {
57
- mouseEvent = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
58
- }
59
- else if (button == "right")
60
- {
61
- mouseEvent = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;
62
- }
63
- else if (button == "middle")
64
- {
65
- mouseEvent = MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP;
66
- }
67
- else
68
- {
69
- Napi::TypeError::New(env, "Invalid button name").ThrowAsJavaScriptException();
70
- return env.Null();
71
- }
72
-
73
- // Perform the mouse click
74
- INPUT mouseInput = {0};
75
- mouseInput.type = INPUT_MOUSE;
76
- mouseInput.mi.dwFlags = mouseEvent;
77
- mouseInput.mi.time = 0; // System will provide the timestamp
78
-
79
- SendInput(1, &mouseInput, sizeof(mouseInput));
80
-
81
- return Napi::Boolean::New(env, true);
82
- }
83
-
84
- Napi::Value DragMouse(const Napi::CallbackInfo &info)
85
- {
86
- Napi::Env env = info.Env();
87
-
88
- if (info.Length() < 4 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber())
89
- {
90
- Napi::TypeError::New(env, "You should provide startX, startY, endX, endY").ThrowAsJavaScriptException();
91
- return env.Null();
92
- }
93
-
94
- int startX = info[0].As<Napi::Number>();
95
- int startY = info[1].As<Napi::Number>();
96
- int endX = info[2].As<Napi::Number>();
97
- int endY = info[3].As<Napi::Number>();
98
- int speed = 100;
99
- if (info.Length() > 4 && info[4].IsNumber())
100
- {
101
- speed = info[4].As<Napi::Number>();
102
- }
103
-
104
- // Get the screen metrics
105
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
106
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
107
-
108
- // Convert coordinates to absolute values
109
- int absoluteStartX = static_cast<int>((65536 * startX) / screenWidth);
110
- int absoluteStartY = static_cast<int>((65536 * startY) / screenHeight);
111
- int absoluteEndX = static_cast<int>((65536 * endX) / screenWidth);
112
- int absoluteEndY = static_cast<int>((65536 * endY) / screenHeight);
113
-
114
- // Calculate the distance and duration based on speed
115
- double distanceX = absoluteEndX - absoluteStartX;
116
- double distanceY = absoluteEndY - absoluteStartY;
117
- double distance = sqrt(distanceX * distanceX + distanceY * distanceY);
118
- double duration = distance / speed;
119
-
120
- // Move the mouse to the starting position
121
- INPUT startMouseInput = {0};
122
- startMouseInput.type = INPUT_MOUSE;
123
- startMouseInput.mi.dx = absoluteStartX;
124
- startMouseInput.mi.dy = absoluteStartY;
125
- startMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
126
- startMouseInput.mi.time = 0; // System will provide the timestamp
127
-
128
- SendInput(1, &startMouseInput, sizeof(startMouseInput));
129
-
130
- // Perform mouse button down event
131
- INPUT mouseDownInput = {0};
132
- mouseDownInput.type = INPUT_MOUSE;
133
- mouseDownInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
134
- mouseDownInput.mi.time = 0; // System will provide the timestamp
135
-
136
- SendInput(1, &mouseDownInput, sizeof(mouseDownInput));
137
-
138
- // Calculate the number of steps based on the duration and desired speed
139
- const int steps = 100; // Adjust the number of steps for smoother movement
140
-
141
- // Calculate the incremental values for each step
142
- double stepX = distanceX / steps;
143
- double stepY = distanceY / steps;
144
-
145
- // Move the mouse in increments to simulate dragging with speed control
146
- for (int i = 0; i < steps; ++i)
147
- {
148
- // Calculate the position for the current step
149
- int currentX = static_cast<int>(absoluteStartX + (stepX * i));
150
- int currentY = static_cast<int>(absoluteStartY + (stepY * i));
151
-
152
- // Move the mouse to the current position
153
- INPUT mouseMoveInput = {0};
154
- mouseMoveInput.type = INPUT_MOUSE;
155
- mouseMoveInput.mi.dx = currentX;
156
- mouseMoveInput.mi.dy = currentY;
157
- mouseMoveInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
158
- mouseMoveInput.mi.time = 0; // System will provide the timestamp
159
-
160
- SendInput(1, &mouseMoveInput, sizeof(mouseMoveInput));
161
-
162
- // Sleep for a short duration to control the speed
163
- Sleep(static_cast<DWORD>(duration / steps));
164
- }
165
-
166
- // Perform mouse button up event
167
- INPUT mouseUpInput = {0};
168
- mouseUpInput.type = INPUT_MOUSE;
169
- mouseUpInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;
170
- mouseUpInput.mi.time = 0; // System will provide the timestamp
171
-
172
- SendInput(1, &mouseUpInput, sizeof(mouseUpInput));
173
-
174
- return Napi::Boolean::New(env, true);
175
- }
1
+ // patrially used code from https://github.com/octalmage/robotjs witch is under MIT License Copyright (c) 2014 Jason Stallings
2
+ #include <napi.h>
3
+ #include <windows.h>
4
+ /**
5
+ * Move the mouse to a specific point.
6
+ * @param point The coordinates to move the mouse to (x, y).
7
+ */
8
+
9
+ Napi::Value MoveMouse(const Napi::CallbackInfo &info)
10
+ {
11
+ Napi::Env env = info.Env();
12
+
13
+ if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber())
14
+ {
15
+ Napi::TypeError::New(env, "You should provide x and y position of type number").ThrowAsJavaScriptException();
16
+ return env.Null();
17
+ }
18
+
19
+ int posX = info[0].As<Napi::Number>();
20
+ int posY = info[1].As<Napi::Number>();
21
+
22
+ // Get the screen metrics
23
+ int screenWidth = GetSystemMetrics(SM_CXSCREEN);
24
+ int screenHeight = GetSystemMetrics(SM_CYSCREEN);
25
+
26
+ // Convert coordinates to absolute values
27
+ int absoluteX = static_cast<int>((65536 * posX) / screenWidth);
28
+ int absoluteY = static_cast<int>((65536 * posY) / screenHeight);
29
+
30
+ // Move the mouse
31
+ INPUT mouseInput = {0};
32
+ mouseInput.type = INPUT_MOUSE;
33
+ mouseInput.mi.dx = absoluteX;
34
+ mouseInput.mi.dy = absoluteY;
35
+ mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
36
+ mouseInput.mi.time = 0; // System will provide the timestamp
37
+
38
+ SendInput(1, &mouseInput, sizeof(mouseInput));
39
+
40
+ return Napi::Boolean::New(env, true);
41
+ }
42
+
43
+ Napi::Value ClickMouse(const Napi::CallbackInfo &info)
44
+ {
45
+ Napi::Env env = info.Env();
46
+ std::string button;
47
+
48
+ if (info.Length() < 1 || !info[0].IsString())
49
+ button = "left";
50
+ else
51
+ button = info[0].As<Napi::String>();
52
+
53
+ WORD mouseEvent = 0;
54
+
55
+ if (button == "left")
56
+ {
57
+ mouseEvent = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
58
+ }
59
+ else if (button == "right")
60
+ {
61
+ mouseEvent = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;
62
+ }
63
+ else if (button == "middle")
64
+ {
65
+ mouseEvent = MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP;
66
+ }
67
+ else
68
+ {
69
+ Napi::TypeError::New(env, "Invalid button name").ThrowAsJavaScriptException();
70
+ return env.Null();
71
+ }
72
+
73
+ // Perform the mouse click
74
+ INPUT mouseInput = {0};
75
+ mouseInput.type = INPUT_MOUSE;
76
+ mouseInput.mi.dwFlags = mouseEvent;
77
+ mouseInput.mi.time = 0; // System will provide the timestamp
78
+
79
+ SendInput(1, &mouseInput, sizeof(mouseInput));
80
+
81
+ return Napi::Boolean::New(env, true);
82
+ }
83
+
84
+ Napi::Value DragMouse(const Napi::CallbackInfo &info)
85
+ {
86
+ Napi::Env env = info.Env();
87
+
88
+ if (info.Length() < 4 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber())
89
+ {
90
+ Napi::TypeError::New(env, "You should provide startX, startY, endX, endY").ThrowAsJavaScriptException();
91
+ return env.Null();
92
+ }
93
+
94
+ int startX = info[0].As<Napi::Number>();
95
+ int startY = info[1].As<Napi::Number>();
96
+ int endX = info[2].As<Napi::Number>();
97
+ int endY = info[3].As<Napi::Number>();
98
+ int speed = 100;
99
+ if (info.Length() > 4 && info[4].IsNumber())
100
+ {
101
+ speed = info[4].As<Napi::Number>();
102
+ }
103
+
104
+ // Get the screen metrics
105
+ int screenWidth = GetSystemMetrics(SM_CXSCREEN);
106
+ int screenHeight = GetSystemMetrics(SM_CYSCREEN);
107
+
108
+ // Convert coordinates to absolute values
109
+ int absoluteStartX = static_cast<int>((65536 * startX) / screenWidth);
110
+ int absoluteStartY = static_cast<int>((65536 * startY) / screenHeight);
111
+ int absoluteEndX = static_cast<int>((65536 * endX) / screenWidth);
112
+ int absoluteEndY = static_cast<int>((65536 * endY) / screenHeight);
113
+
114
+ // Calculate the distance and duration based on speed
115
+ double distanceX = absoluteEndX - absoluteStartX;
116
+ double distanceY = absoluteEndY - absoluteStartY;
117
+ double distance = sqrt(distanceX * distanceX + distanceY * distanceY);
118
+ double duration = distance / speed;
119
+
120
+ // Move the mouse to the starting position
121
+ INPUT startMouseInput = {0};
122
+ startMouseInput.type = INPUT_MOUSE;
123
+ startMouseInput.mi.dx = absoluteStartX;
124
+ startMouseInput.mi.dy = absoluteStartY;
125
+ startMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
126
+ startMouseInput.mi.time = 0; // System will provide the timestamp
127
+
128
+ SendInput(1, &startMouseInput, sizeof(startMouseInput));
129
+
130
+ // Perform mouse button down event
131
+ INPUT mouseDownInput = {0};
132
+ mouseDownInput.type = INPUT_MOUSE;
133
+ mouseDownInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
134
+ mouseDownInput.mi.time = 0; // System will provide the timestamp
135
+
136
+ SendInput(1, &mouseDownInput, sizeof(mouseDownInput));
137
+
138
+ // Calculate the number of steps based on the duration and desired speed
139
+ const int steps = 100; // Adjust the number of steps for smoother movement
140
+
141
+ // Calculate the incremental values for each step
142
+ double stepX = distanceX / steps;
143
+ double stepY = distanceY / steps;
144
+
145
+ // Move the mouse in increments to simulate dragging with speed control
146
+ for (int i = 0; i < steps; ++i)
147
+ {
148
+ // Calculate the position for the current step
149
+ int currentX = static_cast<int>(absoluteStartX + (stepX * i));
150
+ int currentY = static_cast<int>(absoluteStartY + (stepY * i));
151
+
152
+ // Move the mouse to the current position
153
+ INPUT mouseMoveInput = {0};
154
+ mouseMoveInput.type = INPUT_MOUSE;
155
+ mouseMoveInput.mi.dx = currentX;
156
+ mouseMoveInput.mi.dy = currentY;
157
+ mouseMoveInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
158
+ mouseMoveInput.mi.time = 0; // System will provide the timestamp
159
+
160
+ SendInput(1, &mouseMoveInput, sizeof(mouseMoveInput));
161
+
162
+ // Sleep for a short duration to control the speed
163
+ Sleep(static_cast<DWORD>(duration / steps));
164
+ }
165
+
166
+ // Perform mouse button up event
167
+ INPUT mouseUpInput = {0};
168
+ mouseUpInput.type = INPUT_MOUSE;
169
+ mouseUpInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;
170
+ mouseUpInput.mi.time = 0; // System will provide the timestamp
171
+
172
+ SendInput(1, &mouseUpInput, sizeof(mouseUpInput));
173
+
174
+ return Napi::Boolean::New(env, true);
175
+ }