-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
179 lines (156 loc) · 6.04 KB
/
lib.rs
File metadata and controls
179 lines (156 loc) · 6.04 KB
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
#![deny(clippy::all)]
mod error;
mod util;
use enigo::Axis;
use enigo::{Button, Coordinate, Direction, Enigo, Keyboard, Mouse, Settings};
use napi_derive::napi;
use crate::error::Result;
pub use crate::util::{JsAxis, JsButton, JsDirection};
#[napi(js_name = "Enigo")]
pub struct JsEnigo {
enigo: Enigo,
}
#[napi]
impl JsEnigo {
#[napi(factory)]
pub fn create() -> Result<Self> {
Ok(Self {
enigo: Enigo::new(&Settings::default())?,
})
}
#[napi]
/// Get the location of the mouse in pixels.
pub fn get_mouse_position(&self) -> Result<(i32, i32)> {
self.enigo.location().map_err(Into::into)
}
#[napi]
/// Move the mouse cursor to the specified x and y coordinates.
///
/// The top left corner of your monitor screen is x=0 y=0.
/// Move the cursor down the screen by increasing the y and to the right by increasing x coordinate.
pub fn mouse_move(&mut self, x: i32, y: i32) -> Result<()> {
self.enigo
.move_mouse(x, y, Coordinate::Abs)
.map_err(Into::into)
}
#[napi]
/// Sends an individual mouse button event.
///
/// You can use this for example to simulate a click of the left mouse key.
/// Some of the buttons are specific to a platform.
pub fn mouse_button(&mut self, button: JsButton, direction: JsDirection) -> Result<()> {
self.enigo
.button(Button::from(&button), Direction::from(&direction))
.map_err(Into::into)
}
#[napi]
/// Sends an individual mouse button event using Direction::Press.
pub fn mouse_down(&mut self, button: JsButton) -> Result<()> {
self.enigo
.button(Button::from(&button), Direction::Press)
.map_err(Into::into)
}
#[napi]
/// Sends an individual mouse button event using Direction::Release.
pub fn mouse_up(&mut self, button: JsButton) -> Result<()> {
self.enigo
.button(Button::from(&button), Direction::Release)
.map_err(Into::into)
}
#[napi]
/// Sends an individual mouse button event using Direction::Click (press then release).
pub fn mouse_click(&mut self, button: JsButton) -> Result<()> {
self.enigo
.button(Button::from(&button), Direction::Click)
.map_err(Into::into)
}
#[napi]
/// Send a mouse scroll event
///
/// ### Arguments
/// - `length` - Number of 15° (click) rotations of the mouse wheel to scroll. How many lines will be scrolled depends on the current setting of the operating system.
/// - `axis` - The axis to scroll on
///
/// With `Axis::Vertical`, a positive length will result in scrolling down and negative ones up.
/// With `Axis::Horizontal`, a positive length will result in scrolling to the right and negative ones to the left
pub fn scroll(&mut self, length: i32, axis: JsAxis) -> Result<()> {
self.enigo
.scroll(length, Axis::from(&axis))
.map_err(Into::into)
}
#[napi]
/// Send a mouse scroll event on vertical axis.
/// A positive length will result in scrolling down and negative ones up.
pub fn scroll_vertical(&mut self, length: i32) -> Result<()> {
self.enigo
.scroll(length, Axis::Vertical)
.map_err(Into::into)
}
#[napi]
/// Send a mouse scroll event on horizontal axis.
/// A positive length will result in scrolling to the right and negative ones to the left.
pub fn scroll_horizontal(&mut self, length: i32) -> Result<()> {
self.enigo
.scroll(length, Axis::Horizontal)
.map_err(Into::into)
}
#[napi]
/// Sends an individual key event.
pub fn key(&mut self, key: String, direction: JsDirection) -> Result<()> {
let key = util::key_from_string(key)?;
self.enigo
.key(key, Direction::from(&direction))
.map_err(Into::into)
}
#[napi]
/// Sends an individual key event with Direction::Press.
pub fn key_down(&mut self, key: String) -> Result<()> {
let key = util::key_from_string(key)?;
self.enigo.key(key, Direction::Press).map_err(Into::into)
}
#[napi]
/// Sends an individual key event with Direction::Release.
pub fn key_up(&mut self, key: String) -> Result<()> {
let key = util::key_from_string(key)?;
self.enigo.key(key, Direction::Release).map_err(Into::into)
}
#[napi]
/// Sends an individual key event with Direction::Click (press then release).
pub fn key_press(&mut self, key: String) -> Result<()> {
let key = util::key_from_string(key)?;
self.enigo.key(key, Direction::Click).map_err(Into::into)
}
#[napi]
/// Sends a raw keycode.
/// The keycode may or may not be mapped on the current layout.
/// You have to make sure of that yourself.
pub fn key_raw(&mut self, key: u16, direction: JsDirection) -> Result<()> {
self.enigo
.raw(key, Direction::from(&direction))
.map_err(Into::into)
}
#[napi]
/// Sends a raw keycode with Direction::Press.
pub fn key_down_raw(&mut self, key: u16) -> Result<()> {
self.enigo.raw(key, Direction::Press).map_err(Into::into)
}
#[napi]
/// Sends a raw keycode with Direction::Release.
pub fn key_up_raw(&mut self, key: u16) -> Result<()> {
self.enigo.raw(key, Direction::Release).map_err(Into::into)
}
#[napi]
/// Sends a raw keycode with Direction::Click (press then release).
pub fn key_press_raw(&mut self, key: u16) -> Result<()> {
self.enigo.raw(key, Direction::Click).map_err(Into::into)
}
#[napi]
/// Enter the text Use a fast method to enter the text, if it is available.
/// You can use unicode here like: ❤️. This works regardless of the current keyboard layout.
///
/// You cannot use this function for entering shortcuts or something similar.
/// For shortcuts, use the key() method instead.
pub fn type_text(&mut self, text: String) -> Result<()> {
self.enigo.text(&text).map_err(Into::into)
}
}