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
use crate::error::WindowsError; use crate::input::{send_inputs, Action, Button, Input}; use crate::vk::Vk; /// A trait for objects that can be used as keys. For example a [`Vk`] or a `char` can be /// used as a key. /// /// ## Example /// /// ```rust, ignore /// use winput::Vk; /// /// // Print `A` /// winput::press(Vk::Shift); /// winput::send(Vk::A); /// winput::release(Vk::Shift); /// /// // Do the same with one line /// winput::send('A'); /// ``` /// /// [`Vk`]: enum.Vk.html pub trait Keylike: Copy { /// Produces an `Input` that causes the given action to be taken on `self`. /// /// ## Panics /// /// This function panics if `self` was not a valid key. For example, any `char` that /// is above `0x0000ffff` cannot be turned into an `Input`. /// /// ## Example /// /// ```rust, ignore /// use winput::{Keylike, Action}; /// /// let input = 'A'.produce_input(Action::Press); /// winput::send_inputs(&[input]); /// ``` fn produce_input(self, action: Action) -> Input; } impl Keylike for char { #[inline(always)] fn produce_input(self, action: Action) -> Input { Input::from_char(self, action).expect("character above 0x0000ffff") } } impl Keylike for Vk { #[inline(always)] fn produce_input(self, action: Action) -> Input { Input::from_vk(self, action) } } impl Keylike for Button { fn produce_input(self, action: Action) -> Input { Input::from_button(self, action) } } /// Synthesize an event that presses the key. /// /// ## Panics /// /// This function panics if `key` was not a valid key. For example, any `char` that /// is above `0x0000ffff` cannot be turned into an `Input`. /// /// ## Example /// /// ```rust, ignore /// winput::press('A').unwrap(); /// ``` #[inline] pub fn press<K: Keylike>(key: K) -> Result<(), WindowsError> { let input = key.produce_input(Action::Press); let count = crate::input::send_inputs(&[input]); if count == 1 { Ok(()) } else { Err(WindowsError::from_last_error()) } } /// Synthesizes an event that releases the key. /// /// ## Panics /// /// This function panics if `key` was not a valid key. For example, any `char` that /// is above `0x0000ffff` cannot be turned into an `Input`. /// /// ## Example /// /// ```rust, ignore /// winput::release('B').unwrap(); /// ``` #[inline(always)] pub fn release<K: Keylike>(key: K) -> Result<(), WindowsError> { let input = key.produce_input(Action::Release); let count = crate::input::send_inputs(&[input]); if count == 1 { Ok(()) } else { Err(WindowsError::from_last_error()) } } /// Synthesizes two events. One that presses the key, one that releases the key. /// /// ## Panics /// /// This function panics if `key` was not a valid value. For example, any `char` that /// is above `0x0000ffff` cannot be turned into an `Input`. /// /// ## Example /// /// ```rust, ignore /// winput::send('C').unwrap(); /// ``` #[inline(always)] pub fn send<K: Keylike>(key: K) -> Result<(), WindowsError> { let inputs = [ key.produce_input(Action::Press), key.produce_input(Action::Release), ]; let count = crate::input::send_inputs(&inputs); if count == 0 { Err(WindowsError::from_last_error()) } else { Ok(()) } } /// Synthesizes keystrokes according to the given iterator of keys. /// /// Note that this function needs to allocate a buffer to store the inputs produced by the /// given keys. /// /// The function returns the number of inputs that were successfully inserted into the /// keyboard input stream. /// /// If no events were successfully sent, the input stream was already blocked by another /// thread. You can use `winput::WindowsError::from_last_error` to retrieve additional /// information about this function failing to send events. /// /// ## Panics /// /// This function panics if the buffer used to store the produced inputs fails to /// allocate or if any of the given keys is unable to produce an `Input`. /// /// ## Example /// /// ```rust, ignore /// use winput::Vk; /// /// let keys = vec![ Vk::A, Vk::B, Vk::C ]; /// /// winput::send_keys(keys); /// ``` pub fn send_keys<I>(keys: I) -> u32 where I: IntoIterator, I::Item: Keylike, { let iter = keys.into_iter(); let mut buffer = Vec::with_capacity(iter.size_hint().0 * 2); for key in iter { buffer.push(key.produce_input(Action::Press)); buffer.push(key.produce_input(Action::Release)); } send_inputs(&buffer) } /// Synthesizes keystrokes following the given string reference. /// /// Note that this function needs to allocate a buffer to store the inputs produced by /// the characters. /// /// The function returns the number of inputs that were successfully inserted into the /// keyboard input stream. /// /// If no events were successfully sent, the input stream was already blocked by another /// thread. You can use `winput::WindowsError::from_last_error` to retreive additional /// information about this function failing to send events. /// /// ## Panics /// /// This function panics if the buffer fails to allocate or if any of the given character /// fails to produce an `Input`. /// /// ## Example /// /// ```rust, ignore /// winput::send_str("Hello, world"); /// ``` #[inline(always)] pub fn send_str(s: &str) -> u32 { send_keys(s.chars()) }