Skip to main content

umacro_lib/
lib.rs

1use std::time::Duration;
2use std::{i32, thread};
3
4use uinput::event::{relative, Controller, Keyboard, Relative};
5
6pub struct VirtualDevice {
7    device: uinput::Device,
8}
9
10pub type Result<T> = uinput::Result<T>;
11pub type Key = uinput::event::keyboard::Key;
12pub type Mouse = uinput::event::controller::Mouse;
13
14pub fn key_code_from_char(c: char) -> Option<Key> {
15    Some(match c {
16        'a' | 'A' => Key::A,
17        'b' | 'B' => Key::B,
18        'c' | 'C' => Key::C,
19        'd' | 'D' => Key::D,
20        'e' | 'E' => Key::E,
21        'f' | 'F' => Key::F,
22        'g' | 'G' => Key::G,
23        'h' | 'H' => Key::H,
24        'i' | 'I' => Key::I,
25        'j' | 'J' => Key::J,
26        'k' | 'K' => Key::K,
27        'l' | 'L' => Key::L,
28        'm' | 'M' => Key::M,
29        'n' | 'N' => Key::N,
30        'o' | 'O' => Key::O,
31        'p' | 'P' => Key::P,
32        'q' | 'Q' => Key::Q,
33        'r' | 'R' => Key::R,
34        's' | 'S' => Key::S,
35        't' | 'T' => Key::T,
36        'u' | 'U' => Key::U,
37        'v' | 'V' => Key::V,
38        'w' | 'W' => Key::W,
39        'x' | 'X' => Key::X,
40        'y' | 'Y' => Key::Y,
41        'z' | 'Z' => Key::Z,
42
43        '0' => Key::_0,
44        '1' => Key::_1,
45        '2' => Key::_2,
46        '3' => Key::_3,
47        '4' => Key::_4,
48        '5' => Key::_5,
49        '6' => Key::_6,
50        '7' => Key::_7,
51        '8' => Key::_8,
52        '9' => Key::_9,
53
54        '.' => Key::Dot,
55        ',' => Key::Comma,
56        '/' => Key::Slash,
57        '\\' => Key::BackSlash,
58        '[' => Key::LeftBrace,
59        ']' => Key::RightBrace,
60        '\'' => Key::Apostrophe,
61        '-' => Key::Minus,
62        '`' => Key::Grave,
63        ';' => Key::SemiColon,
64        '=' => Key::Equal,
65        '<' => Key::LeftMeta,
66
67        ' ' => Key::Space,
68        '\t' => Key::Tab,
69
70        _ => return None,
71    })
72}
73
74impl VirtualDevice {
75    pub fn key_down(&mut self, code: Key) -> Result<()> {
76        self.device.press(&Keyboard::Key(code))?;
77
78        self.device.synchronize()?;
79
80        Ok(())
81    }
82
83    pub fn key_up(&mut self, code: Key) -> Result<()> {
84        self.device.release(&Keyboard::Key(code))?;
85
86        self.device.synchronize()?;
87
88        Ok(())
89    }
90
91    pub fn key(&mut self, code: Key) -> Result<()> {
92        self.device.click(&Keyboard::Key(code))?;
93
94        self.device.synchronize()?;
95
96        Ok(())
97    }
98    pub fn key_write(&mut self, text: &str, delay_ms: u64) -> Result<()> {
99        let delay = Duration::from_millis(delay_ms);
100
101        for c in text.chars() {
102            let key = key_code_from_char(c);
103            match key {
104                Some(k) => {
105                    std::thread::sleep(delay);
106
107                    self.key(k)?;
108                }
109                None => (),
110            }
111        }
112
113        Ok(())
114    }
115
116    pub fn mouse_down(&mut self, button: Mouse) -> Result<()> {
117        self.device.press(&Controller::Mouse(button))?;
118
119        self.device.synchronize()?;
120
121        Ok(())
122    }
123
124    pub fn mouse_up(&mut self, button: Mouse) -> Result<()> {
125        self.device.release(&Controller::Mouse(button))?;
126
127        self.device.synchronize()?;
128
129        Ok(())
130    }
131
132    pub fn mouse_click(&mut self, button: Mouse) -> Result<()> {
133        self.device.click(&Controller::Mouse(button))?;
134
135        self.device.synchronize()?;
136
137        Ok(())
138    }
139
140    pub fn mouse_move(&mut self, x: i32, y: i32) -> Result<()> {
141        for _ in 0..10 {
142            self.device.send(relative::Position::X, -500)?;
143            self.device.send(relative::Position::Y, -500)?;
144        }
145        self.device.synchronize()?;
146
147        for _ in 0..4 {
148            self.device.send(relative::Position::X, x / 4)?;
149            self.device.send(relative::Position::Y, y / 4)?;
150        }
151        self.device.synchronize()?;
152
153        Ok(())
154    }
155
156    pub fn mouse_move_relative(&mut self, x: i32, y: i32) -> Result<()> {
157        for _ in 0..4 {
158            self.device.send(relative::Position::X, x / 4)?;
159            self.device.send(relative::Position::Y, y / 4)?;
160        }
161        self.device.synchronize()?;
162
163        Ok(())
164    }
165
166    pub fn wait(&self, milliseconds: u64) {
167        thread::sleep(Duration::from_millis(milliseconds));
168    }
169}
170
171pub fn create_virtual_device() -> Result<VirtualDevice> {
172    let device = uinput::default()
173        .unwrap()
174        .name("fdr-virtual-device")?
175        .event(Keyboard::All)?
176        .event(Controller::Mouse(Mouse::Left))?
177        .event(Controller::Mouse(Mouse::Right))?
178        .event(Controller::Mouse(Mouse::Middle))?
179        .event(Relative::Position(relative::Position::X))?
180        .event(Relative::Position(relative::Position::Y))?
181        .create()?;
182
183    Ok(VirtualDevice { device })
184}
185
186// Examples:
187#[cfg(test)]
188mod tests {
189    use super::*;
190
191    #[test]
192    fn mouse_relative() -> Result<()> {
193        let mut device = create_virtual_device()?;
194
195        // Wait for device initialization:
196        device.wait(200);
197
198        for i in 0..3 {
199            device.mouse_move_relative(150, 0)?;
200            device.wait(100);
201
202            device.mouse_move_relative(0, 150)?;
203            device.wait(100);
204
205            device.mouse_move_relative(-150, 0)?;
206            device.wait(100);
207
208            device.mouse_move_relative(0, -150)?;
209            device.wait(100);
210        }
211
212        Ok(())
213    }
214
215    #[test]
216    fn mouse_absolute() -> Result<()> {
217        let mut device = create_virtual_device()?;
218
219        // Wait for device initialization:
220        device.wait(200);
221
222        for i in 0..10 {
223            device.mouse_move(500 - i * 20, 500 - i * 20)?;
224            device.wait(50);
225        }
226
227        Ok(())
228    }
229
230    #[test]
231    fn mouse_click() -> Result<()> {
232        let mut device = create_virtual_device()?;
233
234        // Wait for device initialization:
235        device.wait(200);
236
237        device.mouse_move(250, 200)?;
238        device.wait(500);
239        device.mouse_down(Mouse::Left)?;
240
241        device.wait(1000);
242
243        device.mouse_move_relative(150, 150)?;
244        device.wait(500);
245
246        device.mouse_up(Mouse::Left)?;
247
248        device.wait(500);
249
250        device.mouse_click(Mouse::Right)?;
251
252        Ok(())
253    }
254
255    #[test]
256    fn key_click() -> Result<()> {
257        let mut device = create_virtual_device()?;
258
259        device.wait(3000); // Wait 3 seconds to initialize keyboard
260
261        device.key(Key::H)?;
262        device.key(Key::E)?;
263        device.key(Key::L)?;
264        device.key(Key::L)?;
265        device.key(Key::O)?;
266
267        // Press & release example
268        device.key_down(Key::Space)?;
269        device.key_up(Key::Space)?;
270
271        device.key(Key::W)?;
272        device.key(Key::O)?;
273        device.key(Key::R)?;
274        device.key(Key::L)?;
275        device.key(Key::D)?;
276
277        Ok(())
278    }
279
280    #[test]
281    fn key_write() -> Result<()> {
282        let mut device = create_virtual_device()?;
283
284        device.wait(3000); // Wait 3 seconds to initialize keyboard
285
286        // This is case insensitive, it presses real keyboard buttons, not sending chars.
287        // So you can't write emojis and special characters here.
288        // 10 is wait milliseconds between chars
289        device.key_write("hello world", 10)?;
290
291        device.key(Key::Space)?;
292
293        //If you want to write BIG chars, press shift before.
294        device.key_down(Key::LeftShift)?;
295        device.key_write("big hello world", 200)?; // result: "BIG HELLO WORLD"
296        device.key_up(Key::LeftShift)?;
297
298        Ok(())
299    }
300}