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
//! # Simulating keyboard on Linux, Windows and Mac OS in rust
//!
//! On the next example, the library simulates key A, Z pressed.
//!
//! **The keyboard layout on the computer is important!**
//!
//! If you use a keyboard layout the US, you have corresponding keys, but if you use, for example, the french layout, you have another result.
//! ```
//!extern crate keybd_event;
//!
//!#[cfg(target_os = "linux")]
//!use std::thread::sleep;
//!#[cfg(target_os = "linux")]
//!use std::time::Duration;
//!use keybd_event::KeyboardKey::{KeyA,KeyZ};
//!use keybd_event::KeyBondingInstance;
//!
//!fn main() {
//!    let mut kb = KeyBondingInstance::new().unwrap();
//!    #[cfg(target_os = "linux")]
//!        sleep(Duration::from_secs(2));
//!    kb.has_shift(true);
//!    kb.add_keys(&[KeyA, KeyZ]);
//!    kb.launching();
//!}
//! ```
//! <div style="text-align: center;"><img alt="keyboard image" src="https://github.com/micmonay/keybd_event-rs/raw/master/keyboard-rust.png"/></div>
//!
//! ## Linux
//!
//! On Linux this library use **uinput**, but generally the uinput is only for the root user.
//!
//! The easy solution is executing on root user or change permission by `chmod`, but it is not good.
//!
//! You can follow the next example, for more security.
//!
//!```bash
//!sudo groupadd uinput
//!sudo usermod -a -G uinput my_username
//!sudo udevadm control --reload-rules
//!echo "SUBSYSTEM==\"misc\", KERNEL==\"uinput\", GROUP=\"uinput\", MODE=\"0660\"" | sudo tee /etc/udev/rules.d/uinput.rules
//!echo uinput | sudo tee /etc/modules-load.d/uinput.conf
//!```
//!
//! Another subtlety on Linux, it is important after creating **KeyBondingInstance**, to waiting 2 seconds before running first keyboard actions
//!
//! ## Darwin (MAC OS)
//! This library depends on the frameworks Apple, I did not find a solution for cross-compilation.
#[cfg(target_os = "macos")]
extern crate core_graphics;
#[cfg(target_os = "linux")]
extern crate uinput;

#[cfg(target_os = "linux")]
use linux::LinuxKeyBD;
#[cfg(target_os = "macos")]
use macos::MacOSKeyBD;
#[cfg(target_os = "windows")]
use windows::WindowsKeyBD;

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "macos")]
mod macos;

#[cfg(target_os = "windows")]
mod windows;

/// Contain all Keyboard key compatible
#[derive(Copy, Clone, Debug)]
pub enum KeyboardKey {
    KeySP1 = 41,
    KeySP2 = 12,
    KeySP3 = 13,
    KeySP4 = 26,
    KeySP5 = 27,
    KeySP6 = 39,
    KeySP7 = 40,
    KeySP8 = 43,
    KeySP9 = 51,
    KeySP10 = 52,
    KeySP11 = 53,
    KeySP12 = 86,
    KeyUP = 103,
    KeyDOWN = 108,
    KeyLEFT = 105,
    KeyRIGHT = 106,
    KeyESC = 1,
    Key1 = 2,
    Key2 = 3,
    Key3 = 4,
    Key4 = 5,
    Key5 = 6,
    Key6 = 7,
    Key7 = 8,
    Key8 = 9,
    Key9 = 10,
    Key0 = 11,
    KeyQ = 16,
    KeyW = 17,
    KeyE = 18,
    KeyR = 19,
    KeyT = 20,
    KeyY = 21,
    KeyU = 22,
    KeyI = 23,
    KeyO = 24,
    KeyP = 25,
    KeyA = 30,
    KeyS = 31,
    KeyD = 32,
    KeyF = 33,
    KeyG = 34,
    KeyH = 35,
    KeyJ = 36,
    KeyK = 37,
    KeyL = 38,
    KeyZ = 44,
    KeyX = 45,
    KeyC = 46,
    KeyV = 47,
    KeyB = 48,
    KeyN = 49,
    KeyM = 50,
    KeyF1 = 59,
    KeyF2 = 60,
    KeyF3 = 61,
    KeyF4 = 62,
    KeyF5 = 63,
    KeyF6 = 64,
    KeyF7 = 65,
    KeyF8 = 66,
    KeyF9 = 67,
    KeyF10 = 68,
    KeyF11 = 87,
    KeyF12 = 88,
    KeyNUMLock = 69,
    KeyScrollLock = 70,
    KeyRESERVED = 0,
    KeyBACKSPACE = 14,
    KeyTAB = 15,
    KeyENTER = 28,
    KeySPACE = 57,
    KeyCAPSLock = 58,
    KeyKP0 = 82,
    KeyKP1 = 79,
    KeyKP2 = 80,
    KeyKP3 = 81,
    KeyKP4 = 75,
    KeyKP5 = 76,
    KeyKP6 = 77,
    KeyKP7 = 71,
    KeyKP8 = 72,
    KeyKP9 = 73,
    KeyKPMinus = 74,
    KeyKPPlus = 78,
    KeyKPDot = 83,
    KeyKPJPComma = 95,
    KeyKPEnter = 96,
    KeyKPSlash = 98,
    KeyKPAsterisk = 55,
    KeyKPEqual = 117,
    KeyKPPlusMinus = 118,
    KeyKPComma = 121,
}

/// All platform need implement this trait.
pub trait KBPlatform {
    fn run_action(&mut self, key_bonding: KeyBonding);
}

/// Use for create and run the simulation.
pub struct KeyBondingInstance {
    key_bonding: KeyBonding,
    platform: Box<dyn KBPlatform>,
}

/// Data information for platform.
#[derive(Clone, Debug)]
pub struct KeyBonding {
    pub has_ctrl: bool,
    pub has_alt: bool,
    pub has_shift: bool,
    pub has_rctrl: bool,
    pub has_rshift: bool,
    pub has_altgr: bool,
    pub keys: Vec<KeyboardKey>,
}

impl KeyBondingInstance {
    /// Default function for create a new instance of KeyBondingInstance.
    pub fn new() -> Result<KeyBondingInstance, String> {
        let platform = match KeyBondingInstance::get_platform() {
            Ok(p) => p,
            Err(error) => return Err(error),
        };
        return KeyBondingInstance::new_with_platform(platform);
    }
    /// For create new KeyBondingInstance with specific platform.
    pub fn new_with_platform(platform: Box<dyn KBPlatform>) -> Result<KeyBondingInstance, String> {
        Ok(KeyBondingInstance {
            key_bonding: KeyBonding {
                has_ctrl: false,
                has_alt: false,
                has_shift: false,
                has_rctrl: false,
                has_rshift: false,
                has_altgr: false,
                keys: vec![],
            },
            platform,
        })
    }
    fn get_platform() -> Result<Box<dyn KBPlatform>, String> {
        #[cfg(target_os = "windows")]
        return WindowsKeyBD::new();
        #[cfg(target_os = "linux")]
        return LinuxKeyBD::new();
        #[cfg(target_os = "macos")]
        return MacOSKeyBD::new();

        #[allow(unreachable_code)]
        Err("Not compatible platform for keybd_event".to_string())
    }
    /// Clean data of KeyBonding
    pub fn clear(&mut self) {
        self.key_bonding = KeyBonding {
            has_ctrl: false,
            has_alt: false,
            has_shift: false,
            has_rctrl: false,
            has_rshift: false,
            has_altgr: false,
            keys: vec![],
        }
    }

    pub fn set_keys(&mut self, keys: Vec<KeyboardKey>) {
        self.key_bonding.keys = keys;
    }

    pub fn add_keys(&mut self, keys: &[KeyboardKey]) {
        self.key_bonding.keys.extend_from_slice(keys);
    }
    pub fn add_key(&mut self, key: KeyboardKey) {
        self.key_bonding.keys.push(key);
    }
    pub fn has_shift(&mut self, b: bool) {
        self.key_bonding.has_shift = b;
    }
    pub fn has_alt(&mut self, b: bool) {
        self.key_bonding.has_alt = b;
    }
    pub fn has_ctrl(&mut self, b: bool) {
        self.key_bonding.has_ctrl = b;
    }
    pub fn has_rctrl(&mut self, b: bool) {
        self.key_bonding.has_rctrl = b;
    }
    pub fn has_rshift(&mut self, b: bool) {
        self.key_bonding.has_rshift = b;
    }

    pub fn has_altgr(&mut self, b: bool) {
        self.key_bonding.has_altgr = b;
    }
    /// For launch the simulation
    pub fn launching(&mut self) {
        self.platform.run_action(self.key_bonding.clone());
    }
}

#[cfg(test)]
mod tests {
    #[cfg(target_os = "linux")]
    use std::thread::{sleep, Thread};
    #[cfg(target_os = "linux")]
    use std::time::Duration;

    use KeyBondingInstance;
    use KeyboardKey::*;

    #[test]
    fn it_works() {
        let mut kb = KeyBondingInstance::new().unwrap();
        #[cfg(target_os = "linux")]
        sleep(Duration::from_secs(2));
        kb.has_shift(true);
        kb.add_keys(&[KeyA, KeyZ]);
        kb.launching();
    }
}