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
/// ## keyboard example
/// ```rust
///    use doe::keyboard::key_press;
///    use doe::keyboard::key_release;
///    use doe::keyboard::listen_mouse_position;
///    use doe::keyboard::get_mouse_position;
///
///    use doe::keyboard::KeyCode;
///    key_press(KeyCode::COMMAND);
///    key_press(KeyCode::V);
///    key_release(KeyCode::V);
/// ```
#[allow(warnings)]
#[cfg(feature = "keyboard")]
pub mod keyboard {

    pub mod common {}

    #[cfg(target_os = "linux")]
    pub mod linux {
        ///
        /// ```rust
        ///fn main(){
        ///     use doe::keyboard::keyboard::key_press;
        ///     key_press("ctrl+v",100);
        /// }
        /// ```
        ///
        ///

        #[cfg(target_os = "linux")]
        pub fn key_press(keys: &str) {
            use xdotool::command::options;
            use xdotool::keyboard::send_key_down;
            use xdotool::keyboard::send_key_up;
            use xdotool::option_vec;
            use xdotool::OptionVec;
            send_key_down(keys, option_vec![options::KeyboardOption::Delay(100)]);
        }
        #[cfg(target_os = "linux")]
        pub fn key_release(keys: &str) {
            use xdotool::command::options;
            use xdotool::keyboard::send_key_down;
            use xdotool::keyboard::send_key_up;
            use xdotool::option_vec;
            use xdotool::OptionVec;
            send_key_up(keys, option_vec![options::KeyboardOption::Delay(100)]);
        }
    }

    #[cfg(target_os = "macos")]
    pub mod macos {
        /// ## keyboard example
        /// ```rust
        ///    use doe::keyboard::key_press;
        ///    use doe::keyboard::key_release;
        ///    use doe::keyboard::listen_mouse_position;
        ///    use doe::keyboard::get_mouse_position;
        ///
        ///    use doe::keyboard::KeyCode;
        ///    key_press(KeyCode::COMMAND);
        ///    key_press(KeyCode::V);
        ///    key_release(KeyCode::V);
        /// ```
        #[cfg(target_os = "macos")]
        pub fn key_press(keycode: u16) {
            use core_graphics::event::CGKeyCode;
            use core_graphics::event::{CGEvent, CGEventFlags, CGEventTapLocation};
            use core_graphics::event_source::CGEventSource;
            use core_graphics::event_source::CGEventSourceStateID;
            let state_id: CGEventSourceStateID = CGEventSourceStateID::HIDSystemState;
            let source: CGEventSource = CGEventSource::new(state_id).unwrap();
            let new_keycode: CGKeyCode = keycode;
            let event = CGEvent::new_keyboard_event(source, new_keycode, true).unwrap();
            event.set_flags(CGEventFlags::CGEventFlagCommand);
            event.post(CGEventTapLocation::HID);
        }
        /// ## keyboard example
        /// ```rust
        ///fn main(){
        ///    use doe::keyboard::key_press;
        ///    use doe::keyboard::key_release;
        ///    use doe::keyboard::listen_mouse_position;
        ///    use doe::keyboard::get_mouse_position;
        ///
        ///    use doe::keyboard::KeyCode;
        ///    key_press(KeyCode::COMMAND);
        ///    key_press(KeyCode::V);
        ///    key_release(KeyCode::V);
        ///}
        /// ```

        #[cfg(target_os = "macos")]
        pub fn key_release(keycode: u16) {
            use core_graphics::event::CGKeyCode;
            use core_graphics::event::{CGEvent, CGEventFlags, CGEventTapLocation};
            use core_graphics::event_source::CGEventSource;
            use core_graphics::event_source::CGEventSourceStateID;
            let state_id: CGEventSourceStateID = CGEventSourceStateID::HIDSystemState;
            let source: CGEventSource = CGEventSource::new(state_id).unwrap();
            let new_keycode: CGKeyCode = keycode;
            let event = CGEvent::new_keyboard_event(source, new_keycode, false).unwrap();
            event.set_flags(CGEventFlags::CGEventFlagCommand);
            event.post(CGEventTapLocation::HID);
        }
    }

    #[cfg(target_os = "windows")]
    pub mod windows {
        pub fn key_release_keybd_event(keycode: u16) {
            use winapi::um::winuser::keybd_event;

            unsafe {
                keybd_event(keycode as u8, 0, 0x2, 0);
            }
        }
        pub fn key_press_by_keybd_event(keycode: u16) {
            use winapi::um::winuser::keybd_event;
            use winapi::um::winuser::mouse_event;

            unsafe {
                keybd_event(keycode as u8, 0, 0, 0);
            }
        }
        pub fn key_release(keycode: u16) {
            use winapi::shared::minwindef::DWORD;
            use winapi::um::winuser::keybd_event;
            use winapi::um::winuser::{SendInput, INPUT, INPUT_KEYBOARD};
            pub const KEYEVENTF_KEYUP: DWORD = 0x0002;
            unsafe {
                let mut input = INPUT::default();
                input.type_ = INPUT_KEYBOARD;
                input.u.ki_mut().wVk = keycode as _;
                input.u.ki_mut().dwFlags = KEYEVENTF_KEYUP;
                // Simulate key press
                let usent = SendInput(
                    1,                    // Number of inputs
                    &mut input as *mut _, // Pointer to the input array
                    std::mem::size_of::<INPUT>() as i32,
                );
                if usent != 1 {
                    println!("send error");
                }
            }
        }

        pub fn key_press(keycode: u16) {
            use winapi::shared::minwindef::DWORD;
            use winapi::um::winuser::keybd_event;
            use winapi::um::winuser::{SendInput, INPUT, INPUT_KEYBOARD};
            pub const KEYEVENTF_KEYUP: DWORD = 0x0002;
            unsafe {
                let mut input = INPUT::default();
                input.type_ = INPUT_KEYBOARD;
                input.u.ki_mut().wVk = keycode as _;
                input.u.ki_mut().dwFlags = 0; // Press the key

                // Simulate key press
                let usent = SendInput(
                    1,                    // Number of inputs
                    &mut input as *mut _, // Pointer to the input array
                    std::mem::size_of::<INPUT>() as i32,
                );
                if usent != 1 {
                    println!("send error");
                }
            }
        }
    }

    pub use common::*;

    #[cfg(target_os = "linux")]
    pub use linux::*;

    #[cfg(target_os = "macos")]
    pub use macos::*;

    #[cfg(target_os = "windows")]
    pub use windows::*;
}

#[cfg(feature = "keyboard")]
pub use keyboard::*;

#[allow(warnings)]
pub struct WinKeyCode;
impl WinKeyCode {
    pub const A: u16 = 65;
    pub const B: u16 = 66;
    pub const C: u16 = 67;
    pub const D: u16 = 68;
    pub const E: u16 = 69;
    pub const F: u16 = 70;
    pub const G: u16 = 71;
    pub const H: u16 = 72;
    pub const I: u16 = 73;
    pub const J: u16 = 74;
    pub const K: u16 = 75;
    pub const L: u16 = 76;
    pub const M: u16 = 77;
    pub const N: u16 = 78;
    pub const O: u16 = 79;
    pub const P: u16 = 80;
    pub const Q: u16 = 81;
    pub const R: u16 = 82;
    pub const S: u16 = 83;
    pub const T: u16 = 84;
    pub const U: u16 = 85;
    pub const V: u16 = 86;
    pub const W: u16 = 87;
    pub const X: u16 = 88;
    pub const Y: u16 = 89;
    pub const Z: u16 = 90;
    pub const NUM_0: u16 = 48;
    pub const NUM_1: u16 = 49;
    pub const NUM_2: u16 = 50;
    pub const NUM_3: u16 = 51;
    pub const NUM_4: u16 = 52;
    pub const NUM_5: u16 = 53;
    pub const NUM_6: u16 = 54;
    pub const NUM_7: u16 = 55;
    pub const NUM_8: u16 = 56;
    pub const NUM_9: u16 = 57;
    pub const F1: u16 = 112;
    pub const F2: u16 = 113;
    pub const F3: u16 = 114;
    pub const F4: u16 = 115;
    pub const F5: u16 = 116;
    pub const F6: u16 = 117;
    pub const F7: u16 = 118;
    pub const F8: u16 = 119;
    pub const F9: u16 = 120;
    pub const F10: u16 = 121;
    pub const F11: u16 = 122;
    pub const F12: u16 = 123;
    pub const ENTER: u16 = 10;
    pub const END: u16 = 35;
    pub const HOME: u16 = 36;
    pub const LEFT_ARROW: u16 = 37;
    pub const UP_ARROW: u16 = 38;
    pub const RIGHT_ARROW: u16 = 39;
    pub const DOWN_ARROW: u16 = 40;
    pub const INSERT: u16 = 45;
    pub const DELETE: u16 = 46;
    pub const HELP: u16 = 47;
    pub const NUM_LOCK: u16 = 14;
    pub const BACKSPACE: u16 = 8;
    pub const TAB: u16 = 9;
    pub const CLEAR: u16 = 12;
    pub const SHIFT: u16 = 16;
    pub const CONTROL: u16 = 17;
    pub const ALT: u16 = 18;
    pub const CAPS_LOCK: u16 = 20;
    pub const ESC: u16 = 27;
    pub const SPACEBAR: u16 = 32;
    pub const PAGE_UP: u16 = 33;
    pub const PAGE_DOWN: u16 = 3;
    pub const SIGN_MUL: u16 = 106; //*
    pub const SIGN_ADD: u16 = 107; //+
    pub const SIGN_SUB: u16 = 109; //+
    pub const SIGN_DOT: u16 = 110; //.
    pub const SIGN_DIV: u16 = 111; // \/
}
#[allow(warnings)]
pub struct KeyCode;
#[allow(warnings)]
impl KeyCode {
    pub const RETURN: u16 = 0x24;
    pub const TAB: u16 = 0x30;
    pub const SPACE: u16 = 0x31;
    pub const DELETE: u16 = 0x33;
    pub const ESCAPE: u16 = 0x35;
    pub const COMMAND: u16 = 0x37;
    pub const SHIFT: u16 = 0x38;
    pub const CAPS_LOCK: u16 = 0x39;
    pub const OPTION: u16 = 0x3A;
    pub const CONTROL: u16 = 0x3B;
    pub const RIGHT_COMMAND: u16 = 0x36;
    pub const RIGHT_SHIFT: u16 = 0x3C;
    pub const RIGHT_OPTION: u16 = 0x3D;
    pub const RIGHT_CONTROL: u16 = 0x3E;
    pub const FUNCTION: u16 = 0x3F;
    pub const VOLUME_UP: u16 = 0x48;
    pub const VOLUME_DOWN: u16 = 0x49;
    pub const MUTE: u16 = 0x4A;
    pub const F1: u16 = 0x7A;
    pub const F2: u16 = 0x78;
    pub const F3: u16 = 0x63;
    pub const F4: u16 = 0x76;
    pub const F5: u16 = 0x60;
    pub const F6: u16 = 0x61;
    pub const F7: u16 = 0x62;
    pub const F8: u16 = 0x64;
    pub const F9: u16 = 0x65;
    pub const F10: u16 = 0x6D;
    pub const F11: u16 = 0x67;
    pub const F12: u16 = 0x6F;
    pub const F13: u16 = 0x69;
    pub const F14: u16 = 0x6B;
    pub const F15: u16 = 0x71;
    pub const F16: u16 = 0x6A;
    pub const F17: u16 = 0x40;
    pub const F18: u16 = 0x4F;
    pub const F19: u16 = 0x50;
    pub const F20: u16 = 0x5A;
    pub const HELP: u16 = 0x72;
    pub const HOME: u16 = 0x73;
    pub const PAGE_UP: u16 = 0x74;
    pub const FORWARD_DELETE: u16 = 0x75;
    pub const END: u16 = 0x77;
    pub const PAGE_DOWN: u16 = 0x79;
    pub const LEFT_ARROW: u16 = 0x7B;
    pub const RIGHT_ARROW: u16 = 0x7C;
    pub const DOWN_ARROW: u16 = 0x7D;
    pub const UP_ARROW: u16 = 0x7E;

    // 字母 A-Z
    pub const A: u16 = 0x00;
    pub const B: u16 = 0x0B;
    pub const C: u16 = 0x08;
    pub const D: u16 = 0x02;
    pub const E: u16 = 0x0E;
    pub const F: u16 = 0x03;
    pub const G: u16 = 0x05;
    pub const H: u16 = 0x04;
    pub const I: u16 = 0x22;
    pub const J: u16 = 0x26;
    pub const K: u16 = 0x28;
    pub const L: u16 = 0x25;
    pub const M: u16 = 0x2E;
    pub const N: u16 = 0x2D;
    pub const O: u16 = 0x1F;
    pub const P: u16 = 0x23;
    pub const Q: u16 = 0x0C;
    pub const R: u16 = 0x0F;
    pub const S: u16 = 0x01;
    pub const T: u16 = 0x11;
    pub const U: u16 = 0x20;
    pub const V: u16 = 0x09;
    pub const W: u16 = 0x0D;
    pub const X: u16 = 0x07;
    pub const Y: u16 = 0x10;
    pub const Z: u16 = 0x06;

    // 数字 0-9
    pub const NUM_0: u16 = 0x1D;
    pub const NUM_1: u16 = 0x12;
    pub const NUM_2: u16 = 0x13;
    pub const NUM_3: u16 = 0x14;
    pub const NUM_4: u16 = 0x15;
    pub const NUM_5: u16 = 0x17;
    pub const NUM_6: u16 = 0x16;
    pub const NUM_7: u16 = 0x1A;
    pub const NUM_8: u16 = 0x1C;
    pub const NUM_9: u16 = 0x19;
}