generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
360 lines (330 loc) · 8.67 KB
/
lib.rs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#![deny(clippy::all)]
use napi::bindgen_prelude::Uint16Array;
use napi_derive::napi;
use enigo::{
Axis, Button, Coordinate,
Direction::{Click, Press, Release},
Enigo, Key, Keyboard, Mouse, Settings,
};
impl Default for EnigoJs {
fn default() -> Self {
Self::new()
}
}
#[napi(js_name = "Enigo")]
pub struct EnigoJs {
enigo: Enigo,
}
#[napi]
impl EnigoJs {
#[napi(constructor)]
pub fn new() -> Self {
EnigoJs {
enigo: Enigo::new(&Settings::default()).unwrap(),
}
}
#[napi]
/// Returns a list of all currently pressed keys.
///
/// ### Example
/// ```js
/// import { Enigo } from '@enigo-js/core'
///
/// const enigo = new Enigo();
/// const keys = enigo.held();
/// console.log(keys);
/// ```
///
/// @returns {Array<Key>} keys - The list of currently pressed keys.
pub fn held(&mut self) -> Uint16Array {
let (_keys, platform_keys) = self.enigo.held();
Uint16Array::new(platform_keys)
}
#[napi]
pub fn get_mouse_position(&mut self) -> Result<Point, napi::Error> {
let (x, y) = self
.enigo
.location()
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
Ok(Point::new(x, y))
}
#[napi]
pub fn set_mouse_position(&mut self, x: i32, y: i32) -> Result<(), napi::Error> {
self
.enigo
.move_mouse(x, y, Coordinate::Abs)
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
#[napi]
pub fn set_button_click(&mut self, button: MouseButton) -> Result<(), napi::Error> {
let button = match button {
MouseButton::Left => Button::Left,
MouseButton::Middle => Button::Middle,
MouseButton::Right => Button::Right,
};
self
.enigo
.button(button, Click)
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
#[napi]
pub fn set_button_toggle(&mut self, button: MouseButton, down: bool) -> Result<(), napi::Error> {
let button = match button {
MouseButton::Left => Button::Left,
MouseButton::Middle => Button::Middle,
MouseButton::Right => Button::Right,
};
let direction = if down { Press } else { Release };
self
.enigo
.button(button, direction)
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
#[napi]
pub fn set_mouse_scroll(
&mut self,
direction: ScrollDirection,
clicks: i32,
) -> Result<(), napi::Error> {
let length = match direction {
ScrollDirection::Down => clicks,
ScrollDirection::Up => -clicks,
};
self
.enigo
.scroll(length, Axis::Vertical) // Fix: Use Axis enum
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
// Fix: Remove GetColorFromPosition and GetCurrentPositionColor as they are unrelated to Enigo
// ...
#[napi]
pub fn type_text(&mut self, content: String) -> Result<(), napi::Error> {
self
.enigo
.text(content.as_str())
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
#[napi]
pub fn press_key(&mut self, keys: Vec<KeyboardKey>) -> Result<(), napi::Error> {
for key in keys {
let key = transform_key(key); // Fix: use snake_case for function name
self
.enigo
.key(key, Press)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
}
Ok(())
}
// Release Key
#[napi]
pub fn release_key(&mut self, keys: Vec<KeyboardKey>) -> Result<(), napi::Error> {
for key in keys {
let key = transform_key(key);
self
.enigo
.key(key, Release)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
}
Ok(())
}
// Press then Release Key
#[napi]
pub fn press_then_release_key(&mut self, keys: Vec<KeyboardKey>) -> Result<(), napi::Error> {
self.press_key(keys.clone())?;
self.release_key(keys.clone())?;
Ok(())
}
fn get_os_specific_keys(&self, key: KeyboardKey) -> Vec<KeyboardKey> {
#[cfg(target_os = "macos")]
{
vec![KeyboardKey::Meta, key]
}
#[cfg(not(target_os = "macos"))]
{
vec![KeyboardKey::Control, key]
}
}
// General function to trigger a key event
fn trigger_key_event(&mut self, key: KeyboardKey) -> Result<(), napi::Error> {
let keys = self.get_os_specific_keys(key);
self
.press_then_release_key(keys)
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
// Trigger a "copy" event
#[napi]
pub fn copy(&mut self) -> Result<(), napi::Error> {
self.trigger_key_event(KeyboardKey::C)
}
// Trigger a "paste" event
#[napi]
pub fn paste(&mut self) -> Result<(), napi::Error> {
self.trigger_key_event(KeyboardKey::V)
}
// Trigger a "cut" event
#[napi]
pub fn cut(&mut self) -> Result<(), napi::Error> {
self.trigger_key_event(KeyboardKey::X)
}
// Trigger a "select all" event
#[napi]
pub fn select_all(&mut self) -> Result<(), napi::Error> {
self.trigger_key_event(KeyboardKey::A)
}
// Trigger an "undo" event
#[napi]
pub fn undo(&mut self) -> Result<(), napi::Error> {
self.trigger_key_event(KeyboardKey::Z)
}
}
// Get Active Window
// #[napi]
// pub fn get_active_window() -> Result<String, napi::Error> {
// let enigo = Enigo::new(&Settings::default()).map_err(|e| napi::Error::from_reason(e.to_string()))?;
// let window = enigo.().map_err(|e| napi::Error::from_reason(e.to_string()))?;
// Ok(window)
// }
#[napi]
pub enum ScrollDirection {
Down = 0,
Up = 1,
}
#[napi]
pub enum MouseButton {
Left = 0,
Middle = 1,
Right = 2,
}
#[napi]
pub enum KeyboardKey {
Num0 = 0,
Num1 = 1,
Num2 = 2,
Num3 = 3,
Num4 = 4,
Num5 = 5,
Num6 = 6,
Num7 = 7,
Num8 = 8,
Num9 = 9,
A = 10,
B = 11,
C = 12,
D = 13,
E = 14,
F = 15,
G = 16,
H = 17,
I = 18,
J = 19,
K = 20,
L = 21,
M = 22,
N = 23,
O = 24,
P = 25,
Q = 26,
R = 27,
S = 28,
T = 29,
U = 30,
V = 31,
W = 32,
X = 33,
Y = 34,
Z = 35,
Add = 36,
Subtract = 37,
Multiply = 38,
Divide = 39,
OEM2 = 40,
Tab = 41,
CapsLock = 42,
Shift = 43,
Control = 44,
Alt = 45,
Space = 46,
Backspace = 47,
Return = 48,
Escape = 49,
UpArrow = 50,
DownArrow = 51,
LeftArrow = 52,
RightArrow = 53,
Meta = 54,
}
// Fix: Use snake_case for function name
fn transform_key(key: KeyboardKey) -> Key {
match key {
KeyboardKey::Num0 => Key::Unicode('0'),
KeyboardKey::Num1 => Key::Unicode('1'),
KeyboardKey::Num2 => Key::Unicode('2'),
KeyboardKey::Num3 => Key::Unicode('3'),
KeyboardKey::Num4 => Key::Unicode('4'),
KeyboardKey::Num5 => Key::Unicode('5'),
KeyboardKey::Num6 => Key::Unicode('6'),
KeyboardKey::Num7 => Key::Unicode('7'),
KeyboardKey::Num8 => Key::Unicode('8'),
KeyboardKey::Num9 => Key::Unicode('9'),
KeyboardKey::A => Key::Unicode('a'),
KeyboardKey::B => Key::Unicode('b'),
KeyboardKey::C => Key::Unicode('c'),
KeyboardKey::D => Key::Unicode('d'),
KeyboardKey::E => Key::Unicode('e'),
KeyboardKey::F => Key::Unicode('f'),
KeyboardKey::G => Key::Unicode('g'),
KeyboardKey::H => Key::Unicode('h'),
KeyboardKey::I => Key::Unicode('i'),
KeyboardKey::J => Key::Unicode('j'),
KeyboardKey::K => Key::Unicode('k'),
KeyboardKey::L => Key::Unicode('l'),
KeyboardKey::M => Key::Unicode('m'),
KeyboardKey::N => Key::Unicode('n'),
KeyboardKey::O => Key::Unicode('o'),
KeyboardKey::P => Key::Unicode('p'),
KeyboardKey::Q => Key::Unicode('q'),
KeyboardKey::R => Key::Unicode('r'),
KeyboardKey::S => Key::Unicode('s'),
KeyboardKey::T => Key::Unicode('t'),
KeyboardKey::U => Key::Unicode('u'),
KeyboardKey::V => Key::Unicode('v'),
KeyboardKey::W => Key::Unicode('w'),
KeyboardKey::X => Key::Unicode('x'),
KeyboardKey::Y => Key::Unicode('y'),
KeyboardKey::Z => Key::Unicode('z'),
KeyboardKey::Add => Key::Unicode('+'),
KeyboardKey::Subtract => Key::Unicode('-'),
KeyboardKey::Multiply => Key::Unicode('*'),
KeyboardKey::Divide => Key::Unicode('/'),
KeyboardKey::Tab => Key::Tab,
KeyboardKey::CapsLock => Key::CapsLock,
KeyboardKey::Meta => Key::Meta,
KeyboardKey::Shift => Key::Shift,
KeyboardKey::Control => Key::Control,
KeyboardKey::Alt => Key::Alt,
KeyboardKey::Space => Key::Space,
KeyboardKey::Backspace => Key::Backspace,
KeyboardKey::Return => Key::Return,
KeyboardKey::Escape => Key::Escape,
KeyboardKey::UpArrow => Key::UpArrow,
KeyboardKey::DownArrow => Key::DownArrow,
KeyboardKey::LeftArrow => Key::LeftArrow,
KeyboardKey::RightArrow => Key::RightArrow,
KeyboardKey::OEM2 => Key::End, // Use a placeholder or default key
}
}
#[napi(object)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new(x: i32, y: i32) -> Point {
Point { x, y }
}
}
#[napi(object)]
pub struct ToggleKey {
pub value: KeyboardKey,
pub down: bool,
}