Skip to content

Commit 224dd3d

Browse files
committed
Updated delay handling with mouse and keyboard input
1 parent 46a35c8 commit 224dd3d

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

lib/keyboard.class.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ beforeEach(() => {
99
});
1010

1111
describe("Keyboard", () => {
12-
it("should have a default delay of 20 ms", () => {
12+
it("should have a default delay of 500 ms", () => {
1313
// GIVEN
1414
const adapterMock = new NativeAdapter();
1515
const SUT = new Keyboard(adapterMock);
1616

1717
// WHEN
1818

1919
// THEN
20-
expect(SUT.config.autoDelayMs).toEqual(20);
20+
expect(SUT.config.autoDelayMs).toEqual(500);
2121
});
2222

2323
it("should pass input strings down to the type call.", () => {
@@ -30,8 +30,10 @@ describe("Keyboard", () => {
3030
SUT.type(payload);
3131

3232
// THEN
33-
expect(adapterMock.type).toHaveBeenCalledTimes(1);
34-
expect(adapterMock.type).toHaveBeenCalledWith(payload);
33+
expect(adapterMock.type).toHaveBeenCalledTimes(payload.length);
34+
for (const char of payload.split("")) {
35+
expect(adapterMock.type).toHaveBeenCalledWith(char);
36+
}
3537
});
3638

3739
it("should pass multiple input strings down to the type call.", () => {
@@ -44,8 +46,10 @@ describe("Keyboard", () => {
4446
SUT.type(...payload);
4547

4648
// THEN
47-
expect(adapterMock.type).toHaveBeenCalledTimes(1);
48-
expect(adapterMock.type).toHaveBeenCalledWith(payload.join(" "));
49+
expect(adapterMock.type).toHaveBeenCalledTimes(payload.join(" ").length);
50+
for (const char of payload.join(" ").split("")) {
51+
expect(adapterMock.type).toHaveBeenCalledWith(char);
52+
}
4953
});
5054

5155
it("should pass input keys down to the click call.", () => {

lib/keyboard.class.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ export class Keyboard {
88
}
99

1010
public config = {
11-
autoDelayMs: 20,
11+
autoDelayMs: 500,
1212
};
1313

14+
private lastAction: number;
15+
1416
constructor(private nativeAdapter: NativeAdapter) {
1517
this.nativeAdapter.setKeyboardDelay(this.config.autoDelayMs);
18+
this.lastAction = Date.now();
1619
}
1720

1821
public type(...input: string[] | Key[]): Keyboard {
1922
if (Keyboard.inputIsString(input)) {
20-
this.nativeAdapter.type(input.join(" "));
23+
for (const char of input.join(" ").split("")) {
24+
this.waitForNextTick();
25+
this.nativeAdapter.type(char);
26+
this.updateTick();
27+
}
2128
} else {
2229
this.nativeAdapter.click(...input as Key[]);
2330
}
@@ -33,4 +40,15 @@ export class Keyboard {
3340
this.nativeAdapter.releaseKey(...keys);
3441
return this;
3542
}
43+
44+
private updateTick() {
45+
this.lastAction = Date.now();
46+
}
47+
48+
private waitForNextTick() {
49+
let current = Date.now();
50+
while (current - this.lastAction < this.config.autoDelayMs) {
51+
current = Date.now();
52+
}
53+
}
3654
}

lib/mouse.class.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ beforeEach(() => {
1313
const linehelper = new LineHelper();
1414

1515
describe("Mouse class", () => {
16+
it("should have a default delay of 500 ms", () => {
17+
// GIVEN
18+
const adapterMock = new NativeAdapter();
19+
const SUT = new Mouse(adapterMock);
20+
21+
// WHEN
22+
23+
// THEN
24+
expect(SUT.config.autoDelayMs).toEqual(100);
25+
});
26+
1627
it("should forward scrollLeft to the native adapter class", () => {
1728
// GIVEN
1829
const nativeAdapterMock = new NativeAdapter();

lib/mouse.class.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import { Point } from "./point.class";
55

66
export class Mouse {
77
public config = {
8-
autoDelayMs: 0,
8+
autoDelayMs: 100,
99
mouseSpeed: 1000,
1010
};
1111

12+
private lastAction: number;
13+
1214
constructor(private native: NativeAdapter) {
13-
this.native.setMouseDelay(this.config.autoDelayMs);
15+
this.native.setMouseDelay(0);
16+
this.lastAction = Date.now();
1417
}
1518

1619
public setPosition(target: Point): Mouse {
@@ -27,50 +30,72 @@ export class Mouse {
2730
for (let idx = 0; idx < path.length; ++idx) {
2831
const node = path[idx];
2932
const minTime = timeSteps[idx];
30-
const previous = Date.now();
31-
let current = Date.now();
32-
while (current - previous < minTime) {
33-
current = Date.now();
34-
}
33+
this.waitForNextTick(minTime);
3534
this.native.setMousePosition(node);
35+
this.updateTick();
3636
}
3737
return this;
3838
}
3939

4040
public leftClick(): Mouse {
41+
this.waitForNextTick(this.config.autoDelayMs);
4142
this.native.leftClick();
43+
this.updateTick();
4244
return this;
4345
}
4446

4547
public rightClick(): Mouse {
48+
this.waitForNextTick(this.config.autoDelayMs);
4649
this.native.rightClick();
50+
this.updateTick();
4751
return this;
4852
}
4953

5054
public scrollDown(amount: number): Mouse {
55+
this.waitForNextTick(this.config.autoDelayMs);
5156
this.native.scrollDown(amount);
57+
this.updateTick();
5258
return this;
5359
}
5460

5561
public scrollUp(amount: number): Mouse {
62+
this.waitForNextTick(this.config.autoDelayMs);
5663
this.native.scrollUp(amount);
64+
this.updateTick();
5765
return this;
5866
}
5967

6068
public scrollLeft(amount: number): Mouse {
69+
this.waitForNextTick(this.config.autoDelayMs);
6170
this.native.scrollLeft(amount);
71+
this.updateTick();
6272
return this;
6373
}
6474

6575
public scrollRight(amount: number): Mouse {
76+
this.waitForNextTick(this.config.autoDelayMs);
6677
this.native.scrollRight(amount);
78+
this.updateTick();
6779
return this;
6880
}
6981

7082
public drag(path: Point[]): Mouse {
83+
this.waitForNextTick(this.config.autoDelayMs);
7184
this.native.pressButton(Button.LEFT);
7285
this.move(path);
7386
this.native.releaseButton(Button.LEFT);
87+
this.updateTick();
7488
return this;
7589
}
90+
91+
private updateTick() {
92+
this.lastAction = Date.now();
93+
}
94+
95+
private waitForNextTick(minTime: number) {
96+
let current = Date.now();
97+
while (current - this.lastAction < minTime) {
98+
current = Date.now();
99+
}
100+
}
76101
}

lib/provider/native/robotjs-keyboard-action.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class KeyboardAction implements KeyboardActionProvider {
141141
}
142142

143143
public type(input: string): void {
144-
robot.typeStringDelayed(input, 200);
144+
robot.typeString(input);
145145
}
146146

147147
public click(...keys: Key[]): void {

0 commit comments

Comments
 (0)