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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
//! Enigo lets you simulate mouse and keyboard input-events as if they were //! made by the actual hardware. The goal is to make it available on different //! operating systems like Linux, macOS and Windows – possibly many more but //! [Redox](https://redox-os.org/) and *BSD are planned. Please see the //! [Repo](https://github.com/enigo-rs/enigo) for the current status. //! //! I consider this library in an early alpha status, the API will change in //! in the future. The keyboard handling is far from being very usable. I plan //! to build a simple //! [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) //! that will resemble something like: //! //! `"hello {+SHIFT}world{-SHIFT} and break line{ENTER}"` //! //! The current status is that you can just print //! [unicode](http://unicode.org/) //! characters like [emoji](http://getemoji.com/) without the `{+SHIFT}` //! [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) //! or any other "special" key on the Linux, macOS and Windows operating system. //! //! Possible use cases could be for testing user interfaces on different //! plattforms, //! building remote control applications or just automating tasks for user //! interfaces unaccessible by a public API or scripting laguage. //! //! For the keyboard there are currently two modes you can use. The first mode //! is represented by the [key_sequence]() function //! its purpose is to simply write unicode characters. This is independent of //! the keyboardlayout. Please note that //! you're not be able to use modifier keys like Control //! to influence the outcome. If you want to use modifier keys to e.g. //! copy/paste //! use the Layout variant. Please note that this is indeed layout dependent. //! # Examples //! ```no_run //! use enigo::*; //! let mut enigo = Enigo::new(); //! //paste //! enigo.key_down(Key::Control); //! enigo.key_click(Key::Layout('v')); //! enigo.key_up(Key::Control); //! ``` //! //! ```no_run //! use enigo::*; //! let mut enigo = Enigo::new(); //! enigo.mouse_move_to(500, 200); //! enigo.mouse_down(MouseButton::Left); //! enigo.mouse_move_relative(100, 100); //! enigo.mouse_up(MouseButton::Left); //! enigo.key_sequence("hello world"); //! ``` #![deny(missing_docs)] #[cfg(target_os = "macos")] #[macro_use] extern crate objc; // TODO(dustin) use interior mutability not &mut self #[cfg(target_os = "windows")] mod win; #[cfg(target_os = "windows")] pub use win::Enigo; #[cfg(target_os = "macos")] mod macos; #[cfg(target_os = "macos")] pub use macos::Enigo; #[cfg(target_os = "linux")] mod linux; #[cfg(target_os = "linux")] pub use linux::Enigo; /// DSL parser module pub mod dsl; #[cfg(feature = "with_serde")] #[macro_use] extern crate serde_derive; #[cfg(feature = "with_serde")] extern crate serde; #[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq)] /// MouseButton represents a mouse button, /// and is used in for example /// [mouse_click](trait.MouseControllable.html#tymethod.mouse_click). /// WARNING: Types with the prefix Scroll /// IS NOT intended to be used, and may not work on /// all operating systems. pub enum MouseButton { /// Left mouse button Left, /// Middle mouse button Middle, /// Right mouse button Right, /// Scroll up button ScrollUp, /// Left right button ScrollDown, /// Left right button ScrollLeft, /// Left right button ScrollRight, } /// Representing an interface and a set of mouse functions every /// operating system implementation _should_ implement. pub trait MouseControllable { /// Lets the mouse cursor move to the specified x and y coordinates. /// /// The topleft 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. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_move_to(500, 200); /// ``` fn mouse_move_to(&mut self, x: i32, y: i32); /// Lets the mouse cursor move the specified amount in the x and y /// direction. /// /// The amount specified in the x and y parameters are added to the /// current location of the mouse cursor. A positive x values lets /// the mouse cursor move an amount of `x` pixels to the right. A negative /// value for `x` lets the mouse cursor go to the left. A positive value /// of y /// lets the mouse cursor go down, a negative one lets the mouse cursor go /// up. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_move_relative(100, 100); /// ``` fn mouse_move_relative(&mut self, x: i32, y: i32); /// Push down one of the mouse buttons /// /// Push down the mouse button specified by the parameter `button` of /// type [MouseButton](enum.MouseButton.html) /// and holds it until it is released by /// [mouse_up](trait.MouseControllable.html#tymethod.mouse_up). /// Calls to [mouse_move_to](trait.MouseControllable.html#tymethod. /// mouse_move_to) or /// [mouse_move_relative](trait.MouseControllable.html#tymethod. /// mouse_move_relative) /// will work like expected and will e.g. drag widgets or highlight text. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_down(MouseButton::Left); /// ``` fn mouse_down(&mut self, button: MouseButton); /// Lift up a pushed down mouse button /// /// Lift up a previously pushed down button (by invoking /// [mouse_down](trait.MouseControllable.html#tymethod.mouse_down)). /// If the button was not pushed down or consecutive calls without /// invoking [mouse_down](trait.MouseControllable.html#tymethod.mouse_down) /// will emit lift up events. It depends on the /// operating system whats actually happening – my guess is it will just /// get ignored. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_up(MouseButton::Right); /// ``` fn mouse_up(&mut self, button: MouseButton); /// Click a mouse button /// /// it's esentially just a consecutive invokation of /// [mouse_down](trait.MouseControllable.html#tymethod.mouse_down) followed /// by a [mouse_up](trait.MouseControllable.html#tymethod.mouse_up). Just /// for /// convenience. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_click(MouseButton::Right); /// ``` fn mouse_click(&mut self, button: MouseButton); /// Scroll the mouse (wheel) left or right /// /// Positive numbers for length lets the mouse wheel scroll to the right /// and negative ones to the left. The value that is specified translates /// to `lines` defined by the operating system and is essentially one 15° /// (click)rotation on the mouse wheel. How many lines it moves depends /// on the current setting in the operating system. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_scroll_x(2); /// ``` fn mouse_scroll_x(&mut self, length: i32); /// Scroll the mouse (wheel) up or down /// /// Positive numbers for length lets the mouse wheel scroll down /// and negative ones up. The value that is specified translates /// to `lines` defined by the operating system and is essentially one 15° /// (click)rotation on the mouse wheel. How many lines it moves depends /// on the current setting in the operating system. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.mouse_scroll_y(2); /// ``` fn mouse_scroll_y(&mut self, length: i32); } /// A key on the keyboard. /// For alphabetical keys, use Key::Layout for a system independent key. /// If a key is missing, you can use the raw keycode with Key::Raw. #[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Key { /// alt key on Linux and Windows (option key on macOS) Alt, /// backspace key Backspace, /// caps lock key CapsLock, #[deprecated(since="0.0.12", note="now renamed to Meta")] /// command key on macOS (super key on Linux, windows key on Windows) Command, /// control key Control, /// delete key Delete, /// down arrow key DownArrow, /// end key End, /// escape key (esc) Escape, /// F1 key F1, /// F10 key F10, /// F11 key F11, /// F12 key F12, /// F2 key F2, /// F3 key F3, /// F4 key F4, /// F5 key F5, /// F6 key F6, /// F7 key F7, /// F8 key F8, /// F9 key F9, /// home key Home, /// left arrow key LeftArrow, /// meta key (also known as "windows", "super", and "command") Meta, /// option key on macOS (alt key on Linux and Windows) Option, /// page down key PageDown, /// page up key PageUp, /// return key Return, /// right arrow key RightArrow, /// shift key Shift, /// space key Space, #[deprecated(since="0.0.12", note="now renamed to Meta")] /// super key on linux (command key on macOS, windows key on Windows) Super, /// tab key (tabulator) Tab, /// up arrow key UpArrow, #[deprecated(since="0.0.12", note="now renamed to Meta")] /// windows key on Windows (super key on Linux, command key on macOS) Windows, /// keyboard layout dependent key Layout(char), /// raw keycode eg 0x38 Raw(u16) } /// Representing an interface and a set of keyboard functions every /// operating system implementation _should_ implement. pub trait KeyboardControllable { /// Types the string parsed with DSL. /// /// Typing {+SHIFT}hello{-SHIFT} becomes HELLO. /// TODO: Full documentation fn key_sequence_parse(&mut self, sequence: &str) where Self: Sized, { self.key_sequence_parse_try(sequence).expect("Could not parse sequence"); } /// Same as key_sequence_parse except returns any errors fn key_sequence_parse_try(&mut self, sequence: &str) -> Result<(), dsl::ParseError> where Self: Sized, { dsl::eval(self, sequence) } /// Types the string /// /// Emits keystrokes such that the given string is inputted. /// /// You can use many unicode here like: ❤️. This works /// regadless of the current keyboardlayout. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// enigo.key_sequence("hello world ❤️"); /// ``` fn key_sequence(&mut self, sequence: &str); /// presses a given key down fn key_down(&mut self, key: Key); /// release a given key formally pressed down by /// [key_down](trait.KeyboardControllable.html#tymethod.key_down) fn key_up(&mut self, key: Key); /// Much like the /// [key_down](trait.KeyboardControllable.html#tymethod.key_down) and /// [key_up](trait.KeyboardControllable.html#tymethod.key_up) /// function they're just invoked consecutively fn key_click(&mut self, key: Key); } impl Enigo { /// Constructs a new `Enigo` instance. /// /// # Example /// /// ```no_run /// use enigo::*; /// let mut enigo = Enigo::new(); /// ``` pub fn new() -> Self { Self::default() } } use std::fmt; impl fmt::Debug for Enigo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Enigo") } }