-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathnative.adapter.class.ts
209 lines (189 loc) · 5.6 KB
/
native.adapter.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { Button } from "../button.enum";
import { Key } from "../key.enum";
import { Point } from "../point.class";
import { ClipboardActionProvider } from "../provider/native/clipboard-action-provider.interface";
import { ClipboardAction } from "../provider/native/clipboardy-clipboard-action.class";
import { KeyboardActionProvider } from "../provider/native/keyboard-action-provider.interface";
import { MouseActionInterface } from "../provider/native/mouse-action-provider.interface";
import { KeyboardAction } from "../provider/native/robotjs-keyboard-action.class";
import { MouseAction } from "../provider/native/robotjs-mouse-action.class";
/**
* NativeAdapter serves as an abstraction layer for all OS level interactions.
*
* This allows to provide a high level interface for native actions,
* whithout having to spread (possibly) multiple dependencies all over the code.
* All actions which involve the OS are bundled in this adapter.
*/
export class NativeAdapter {
constructor(
private clipboard: ClipboardActionProvider = new ClipboardAction(),
private keyboard: KeyboardActionProvider = new KeyboardAction(),
private mouse: MouseActionInterface = new MouseAction(),
) {}
/**
* setMouseDelay configures mouse speed for movement
*
* @param {number} delay The delay
* @memberof NativeAdapter
*/
public setMouseDelay(delay: number): void {
this.mouse.setMouseDelay(delay);
}
/**
* setKeyboardDelay configures keyboard delay between key presses
*
* @param {number} delay The delay
* @memberof NativeAdapter
*/
public setKeyboardDelay(delay: number): void {
this.keyboard.setKeyboardDelay(delay);
}
/**
* setMousePosition changes the current mouse cursor position to a given point
*
* @param {Point} p The new cursor position
* @memberof NativeAdapter
*/
public setMousePosition(p: Point): Promise<void> {
return this.mouse.setMousePosition(p);
}
/**
* getMousePosition returns the current mouse position
*
* @returns {Promise<Point>} Current cursor position
* @memberof NativeAdapter
*/
public currentMousePosition(): Promise<Point> {
return this.mouse.currentMousePosition();
}
/**
* leftClick triggers a native left-click event via OS API
*
* @memberof NativeAdapter
*/
public leftClick(): Promise<void> {
return this.mouse.leftClick();
}
/**
* rightClick triggers a native right-click event via OS API
*
* @memberof NativeAdapter
*/
public rightClick(): Promise<void> {
return this.mouse.rightClick();
}
/**
* middleClick triggers a native middle-click event via OS API
*/
public middleClick(): Promise<void> {
return this.mouse.middleClick();
}
/**
* pressButton presses and holds a mouse button
*
* @param {Button} btn The mouse button to press
* @memberof NativeAdapter
*/
public pressButton(btn: Button): Promise<void> {
return this.mouse.pressButton(btn);
}
/**
* releaseButton releases a mouse button previously clicked via pressButton
*
* @param {Button} btn The mouse button to release
* @memberof NativeAdapter
*/
public releaseButton(btn: Button): Promise<void> {
return this.mouse.releaseButton(btn);
}
/**
* type types a given string via native keyboard events
*
* @param {string} input The text to type
* @memberof NativeAdapter
*/
public type(input: string): Promise<void> {
return this.keyboard.type(input);
}
/**
* click clicks a single Key via native keyboard event
*
* @param {Key[]} keys The keys to click
* @memberof NativeAdapter
*/
public click(...keys: Key[]): Promise<void> {
return this.keyboard.click(...keys);
}
/**
* pressKey presses and holds a given Key
*
* @param {Key[]} keys The Keys to press and hold
* @memberof NativeAdapter
*/
public pressKey(...keys: Key[]): Promise<void> {
return this.keyboard.pressKey(...keys);
}
/**
* releaseKey releases a Key previously presses via pressKey
*
* @param {Key[]} keys The Keys to release
* @memberof NativeAdapter
*/
public releaseKey(...keys: Key[]): Promise<void> {
return this.keyboard.releaseKey(...keys);
}
/**
* scrollUp triggers an upwards mouse wheel scroll
*
* @param {number} amount The amount of 'ticks' to scroll
* @memberof NativeAdapter
*/
public scrollUp(amount: number): Promise<void> {
return this.mouse.scrollUp(amount);
}
/**
* scrollDown triggers a downward mouse wheel scroll
*
* @param {number} amount The amount of 'ticks' to scroll
* @memberof NativeAdapter
*/
public scrollDown(amount: number): Promise<void> {
return this.mouse.scrollDown(amount);
}
/**
* scrollLeft triggers a left mouse scroll
*
* @param {number} amount The amount of 'ticks' to scroll
* @memberof NativeAdapter
*/
public scrollLeft(amount: number): Promise<void> {
return this.mouse.scrollLeft(amount);
}
/**
* scrollRight triggers a right mouse scroll
*
* @param {number} amount The amount of 'ticks' to scroll
* @memberof NativeAdapter
*/
public scrollRight(amount: number): Promise<void> {
return this.mouse.scrollRight(amount);
}
/**
* copy copies a given text to the system clipboard
*
* @param {string} text The text to copy
* @memberof NativeAdapter
*/
public copy(text: string): Promise<void> {
return this.clipboard.copy(text);
}
/**
* paste pastes the current text on the system clipboard
*
* @returns {Promise<string>} The clipboard text
* @memberof NativeAdapter
*/
public paste(): Promise<string> {
return this.clipboard.paste();
}
}