Skip to content

Commit 6b21ad4

Browse files
committed
add type ansi in window
1 parent 39cd31f commit 6b21ad4

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

README-zh_CN.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export function keyToggle(key: string, down: string, modifier?: string | string[
6363
6464
export function typeString(string: string): void
6565
66+
//win only ansi
67+
export function typeKeyCodeStringInWin(string: string): void;
68+
6669
//mouse
6770
6871
export function dragMouse(x: number, y: number): void
@@ -185,3 +188,9 @@ $ sudo yum install xorg-x11\*
185188
## 结语
186189

187190
有问题可以提 issue,一起进步
191+
192+
## 更新日志
193+
194+
2024-07-25 更新
195+
196+
1. 增加 typeKeyCodeStringInWin 接口,支持 qq 密码输入

index.d.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
export type Callback = (err: string) => void
1+
export type Callback = (err: string) => void;
22

3-
export type MouseButtonString = 'left' | 'right' | 'middle'
3+
export type MouseButtonString = "left" | "right" | "middle";
44

55
//other
6-
export function setKeyboardDelay(ms: number): void
7-
export function setMouseDelay(delay: number): void
8-
export function getScreenSize(): { width: number; height: number }
9-
export function updateScreenMetrics(): void
6+
export function setKeyboardDelay(ms: number): void;
7+
export function setMouseDelay(delay: number): void;
8+
export function getScreenSize(): { width: number; height: number };
9+
export function updateScreenMetrics(): void;
1010

1111
//keyboard
12-
export function keyTap(key: string, modifier?: string | string[]): void
13-
export function keyToggle(key: string, down: string, modifier?: string | string[]): void
14-
export function typeString(string: string): void
12+
export function keyTap(key: string, modifier?: string | string[]): void;
13+
export function keyToggle(key: string, down: string, modifier?: string | string[]): void;
14+
export function typeString(string: string): void;
1515

1616
//mouse
17-
export function dragMouse(x: number, y: number): void
18-
export function moveMouse(x: number, y: number): void
19-
export function moveMouseSmooth(x: number, y: number, speed?: number): void
20-
export function mouseClick(button?: MouseButton, double?: boolean): void
21-
export function mouseToggle(down?: boolean, button?: MouseButton): void
22-
export function getMousePos(): { x: number; y: number }
17+
export function dragMouse(x: number, y: number): void;
18+
export function moveMouse(x: number, y: number): void;
19+
export function moveMouseSmooth(x: number, y: number, speed?: number): void;
20+
export function mouseClick(button?: MouseButtonString, double?: boolean): void;
21+
export function mouseToggle(down?: boolean, button?: MouseButtonString): void;
22+
export function getMousePos(): { x: number; y: number };
23+
24+
//win only ansi
25+
export function typeKeyCodeStringInWin(string: string): void;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "robotjs_addon",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "Node.js Desktop Automation.",
55
"main": "index.js",
66
"typings": "index.d.ts",

src/keypress.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,41 @@ void unicodeTap(const unsigned value)
314314
#endif
315315
}
316316

317+
void typeKeycodeInWin(const char value)
318+
{
319+
#if defined(IS_WINDOWS)
320+
INPUT ip;
321+
ip.type = INPUT_KEYBOARD;
322+
ip.ki.wScan = 0;
323+
ip.ki.time = 0;
324+
ip.ki.dwExtraInfo = 0;
325+
326+
// Press the key
327+
ip.ki.wVk = VkKeyScan(value);
328+
ip.ki.dwFlags = 0; // 0 for key press
329+
SendInput(1, &ip, sizeof(INPUT));
330+
331+
// Release the key
332+
ip.ki.dwFlags = KEYEVENTF_KEYUP;
333+
SendInput(1, &ip, sizeof(INPUT));
334+
#endif
335+
}
336+
337+
void typeKeyCodeStrInWin(const char *value, const unsigned mspc)
338+
{
339+
for (int i = 0; i < strlen(value); i++)
340+
{
341+
typeKeycodeInWin(value[i]);
342+
if (mspc > 0)
343+
{
344+
microsleep(mspc);
345+
}
346+
}
347+
}
348+
317349
void typeStringDelayed(const char *str, const unsigned mspc)
318350
{
319-
unsigned long n=0;
351+
unsigned long n = 0;
320352
unsigned short c;
321353
unsigned short c1;
322354
unsigned short c2;

src/keypress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ typedef unsigned int MMKeyFlags;
7474
/* Sends a Unicode character without modifiers. */
7575
void unicodeTap(const unsigned value);
7676

77+
void typeKeycodeInWin(const char value);
78+
void typeKeyCodeStrInWin(const char *value, const unsigned mspc);
79+
7780
/* Macro to convert WPM to CPM integers.
7881
* (the average English word length is 5.1 characters.) */
7982
#define WPM_TO_CPM(WPM) (unsigned)(5.1 * WPM)

src/robotjs.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ void node_typeString(const Napi::CallbackInfo &info)
481481
CheckCallback(info);
482482
}
483483

484+
void node_typeKeyCodeStringInWin(const Napi::CallbackInfo &info)
485+
{
486+
std::string type_str;
487+
GetStrByArg(info, 0, type_str);
488+
typeKeyCodeStrInWin(type_str.c_str(), 10);
489+
CheckCallback(info);
490+
}
491+
484492
void node_SetKeyboardDelay(const Napi::CallbackInfo &info)
485493
{
486494
Napi::Env env = info.Env();
@@ -523,6 +531,7 @@ Napi::Object InitAll(Napi::Env env, Napi::Object exports)
523531
exports.Set(Napi::String::New(env, "typeString"), Napi::Function::New(env, node_typeString));
524532
exports.Set(Napi::String::New(env, "setKeyboardDelay"), Napi::Function::New(env, node_SetKeyboardDelay));
525533
exports.Set(Napi::String::New(env, "getScreenSize"), Napi::Function::New(env, node_getScreenSize));
534+
exports.Set(Napi::String::New(env, "typeKeyCodeStringInWin"), Napi::Function::New(env, node_typeKeyCodeStringInWin));
526535
return exports;
527536
}
528537
NODE_API_MODULE(robotjs, InitAll)

test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ var robot = require("./index");
22

33
//test mouse
44
// robot.dragMouse(200, 100);
5-
// robot.moveMouse(337, 645);
6-
// robot.mouseClick("left", true);
5+
robot.moveMouse(365, 416);
6+
robot.mouseClick("left", true);
77
// console.log(robot.getMousePos());
88

99
//test keyboard
1010
// robot.typeString("Hello World");
1111
// robot.keyTap("enter");
1212
// robot.keyTap("space", "control");
13-
robot.typeString("你好");
13+
// robot.typeString("你好");
14+
robot.typeKeyCodeStringInWin("124556asdjfkb");

0 commit comments

Comments
 (0)