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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
//! # Input //! //! The `input` module provides a functionality to read the input events. //! //! This documentation does not contain a lot of examples. The reason is that it's fairly //! obvious how to use this crate. Although, we do provide //! [examples](https://github.com/crossterm-rs/examples) repository //! to demonstrate the capabilities. //! //! ## Synchronous vs Asynchronous //! //! ### Synchronous Reading //! //! Read the input synchronously from the user, the reads performed will be blocking calls. //! Using synchronous over asynchronous reading has the benefit that it is using fewer resources than //! the asynchronous because background thread and queues are left away. //! //! See the [`SyncReader`](struct.SyncReader.html) documentation for more details. //! //! ### Asynchronous Reading //! //! Read the input asynchronously, input events are gathered in the background and queued for you to read. //! Using asynchronous reading has the benefit that input events are queued until you read them. You can poll //! for occurred events, and the reads won't block your program. //! //! See the [`AsyncReader`](struct.AsyncReader.html) documentation for more details. //! //! ### Technical details //! //! On UNIX systems crossterm reads from the TTY, on Windows, it uses `ReadConsoleInputW`. //! For asynchronous reading, a background thread will be fired up to read input events, //! occurred events will be queued on an MPSC-channel, and the user can iterate over those events. //! //! The terminal has to be in the raw mode, raw mode prevents the input of the user to be displayed //! on the terminal screen. See the //! [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation to learn more. #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use crate::utils::Result; #[cfg(unix)] use self::input::unix::UnixInput; #[cfg(windows)] use self::input::windows::WindowsInput; use self::input::Input; pub use self::input::{AsyncReader, SyncReader}; mod input; mod sys; /// Represents an input event. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialOrd, PartialEq, Hash, Clone)] pub enum InputEvent { /// A single key or a combination of keys. Keyboard(KeyEvent), /// A mouse event. Mouse(MouseEvent), /// An unsupported event. /// /// You can ignore this type of event, because it isn't used. Unsupported(Vec<u8>), // TODO Not used, should be removed. /// An unknown event. Unknown, /// Internal cursor position event. Don't use it, it will be removed in the /// `crossterm` 1.0. #[doc(hidden)] #[cfg(unix)] CursorPosition(u16, u16), // TODO 1.0: Remove } /// Represents a mouse event. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)] pub enum MouseEvent { /// Pressed mouse button at the location (column, row). Press(MouseButton, u16, u16), /// Released mouse button at the location (column, row). Release(u16, u16), /// Mouse moved with a pressed left button to the new location (column, row). Hold(u16, u16), /// An unknown mouse event. Unknown, } /// Represents a mouse button/wheel. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)] pub enum MouseButton { /// Left mouse button. Left, /// Right mouse button. Right, /// Middle mouse button. Middle, /// Wheel scrolled up. WheelUp, /// Wheel scrolled down. WheelDown, } /// Represents a key or a combination of keys. #[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum KeyEvent { /// Backspace key. Backspace, /// Enter key. Enter, /// Left arrow key. Left, /// Right arrow key. Right, /// Up arrow key. Up, /// Down arrow key. Down, /// Home key. Home, /// End key. End, /// Page up key. PageUp, /// Page dow key. PageDown, /// Tab key. Tab, /// Shift + Tab key. BackTab, /// Delete key. Delete, /// Insert key. Insert, /// F key. /// /// `KeyEvent::F(1)` represents F1 key, etc. F(u8), /// A character. /// /// `KeyEvent::Char('c')` represents `c` character, etc. Char(char), /// Alt key + character. /// /// `KeyEvent::Alt('c')` represents `Alt + c`, etc. Alt(char), /// Ctrl key + character. /// /// `KeyEvent::Ctrl('c') ` represents `Ctrl + c`, etc. Ctrl(char), /// Null. Null, /// Escape key. Esc, /// Ctrl + up arrow key. CtrlUp, /// Ctrl + down arrow key. CtrlDown, /// Ctrl + right arrow key. CtrlRight, /// Ctrl + left arrow key. CtrlLeft, /// Shift + up arrow key. ShiftUp, /// Shift + down arrow key. ShiftDown, /// Shift + right arrow key. ShiftRight, /// Shift + left arrow key. ShiftLeft, } /// An internal event. /// /// Encapsulates publicly available `InputEvent` with additional internal /// events that shouldn't be publicly available to the crate users. #[cfg(unix)] #[derive(Debug, PartialOrd, PartialEq, Hash, Clone)] pub(crate) enum InternalEvent { /// An input event. Input(InputEvent), /// A cursor position (`x`, `y`). CursorPosition(u16, u16), } /// Converts an `InternalEvent` into a possible `InputEvent`. #[cfg(unix)] impl From<InternalEvent> for Option<InputEvent> { fn from(ie: InternalEvent) -> Self { match ie { InternalEvent::Input(input_event) => Some(input_event), // TODO 1.0: Swallow `CursorPosition` and return `None`. // `cursor::pos_raw()` will be able to use this module `internal_event_receiver()` InternalEvent::CursorPosition(x, y) => Some(InputEvent::CursorPosition(x, y)), } } } /// A terminal input. /// /// # Examples /// /// ```no_run /// // You can replace the following line with `use crossterm::...;` /// // if you're using the `crossterm` crate with the `input` feature enabled. /// use crossterm::{Result, TerminalInput, RawScreen}; /// /// fn main() -> Result<()> { /// let input = TerminalInput::new(); /// // Read a single character /// let char = input.read_char()?; /// // Read a single line /// let line = input.read_line()?; /// /// // Make sure to enable raw screen when reading input events /// let screen = RawScreen::into_raw_mode(); /// /// // Create async reader /// let mut async_stdin = input.read_async(); /// /// // Create sync reader /// let mut sync_stdin = input.read_sync(); /// /// // Enable mouse input events /// input.enable_mouse_mode()?; /// // Disable mouse input events /// input.disable_mouse_mode() /// } /// ``` pub struct TerminalInput { #[cfg(windows)] input: WindowsInput, #[cfg(unix)] input: UnixInput, } impl TerminalInput { /// Creates a new `TerminalInput`. pub fn new() -> TerminalInput { #[cfg(windows)] let input = WindowsInput::new(); #[cfg(unix)] let input = UnixInput::new(); TerminalInput { input } } /// Reads one line from the user input and strips the new line character(s). /// /// This function **does not work** when the raw mode is enabled (see the /// [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation /// to learn more). You should use the /// [`read_async`](struct.TerminalInput.html#method.read_async), /// [`read_until_async`](struct.TerminalInput.html#method.read_until_async) /// or [`read_sync`](struct.TerminalInput.html#method.read_sync) method if the /// raw mode is enabled. /// /// # Examples /// /// ```no_run /// let input = crossterm::input(); /// match input.read_line() { /// Ok(s) => println!("string typed: {}", s), /// Err(e) => println!("error: {}", e), /// } /// ``` pub fn read_line(&self) -> Result<String> { self.input.read_line() } /// Reads one character from the user input. /// /// # Examples /// /// ```no_run /// let input = crossterm::input(); /// match input.read_char() { /// Ok(c) => println!("character pressed: {}", c), /// Err(e) => println!("error: {}", e), /// } /// ``` pub fn read_char(&self) -> Result<char> { self.input.read_char() } /// Creates a new `AsyncReader` allowing to read the input asynchronously (not blocking). /// /// If you want a blocking, or less resource consuming read, see the /// [`read_sync`](struct.TerminalInput.html#method.read_sync) method. /// /// # Notes /// /// * It requires enabled raw mode (see the /// [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation to learn more). /// * A thread is spawned to read the input. /// * The reading thread is cleaned up when you drop the [`AsyncReader`](struct.AsyncReader.html). /// /// # Examples /// /// ```no_run /// use std::{thread, time::Duration}; /// use crossterm::input; /// /// let mut async_stdin = input().read_async(); /// /// loop { /// if let Some(key_event) = async_stdin.next() { /// /* Check which event occurred here */ /// } /// /// thread::sleep(Duration::from_millis(50)); /// } /// ``` pub fn read_async(&self) -> AsyncReader { self.input.read_async() } /// Creates a new `AsyncReader` allowing to read the input asynchronously (not blocking) until the /// given `delimiter`. /// /// It behaves in the same way as the [`read_async`](struct.TerminalInput.html#method.read_async) /// method, but it stops reading when the `delimiter` is hit. /// /// # Notes /// /// * It requires enabled raw mode (see the /// [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation to learn more). /// * A thread is spawned to read the input. /// * The reading thread is cleaned up when you drop the [`AsyncReader`](struct.AsyncReader.html). /// /// # Examples /// /// ```no_run /// use std::{thread, time::Duration}; /// /// let mut async_stdin = crossterm::input().read_until_async(b'x'); /// /// loop { /// if let Some(key_event) = async_stdin.next() { /// /* Check which event occurred here */ /// } /// /// thread::sleep(Duration::from_millis(50)); /// } /// ``` pub fn read_until_async(&self, delimiter: u8) -> AsyncReader { self.input.read_until_async(delimiter) } /// Creates a new `SyncReader` allowing to read the input synchronously (blocking). /// /// It's less resource hungry when compared to the /// [`read_async`](struct.TerminalInput.html#method.read_async) method, because it doesn't /// spawn any reading threads. /// /// # Examples /// /// ```no_run /// use std::{thread, time::Duration}; /// /// let mut sync_stdin = crossterm::input().read_sync(); /// /// loop { /// if let Some(key_event) = sync_stdin.next() { /// /* Check which event occurred here */ /// } /// } /// ``` pub fn read_sync(&self) -> SyncReader { self.input.read_sync() } /// Enables mouse events. /// /// Mouse events will be produced by the /// [`AsyncReader`](struct.AsyncReader.html)/[`SyncReader`](struct.SyncReader.html). pub fn enable_mouse_mode(&self) -> Result<()> { self.input.enable_mouse_mode() } /// Disables mouse events. /// /// Mouse events wont be produced by the /// [`AsyncReader`](struct.AsyncReader.html)/[`SyncReader`](struct.SyncReader.html). pub fn disable_mouse_mode(&self) -> Result<()> { self.input.disable_mouse_mode() } } /// Creates a new `TerminalInput`. /// /// # Examples /// /// ```no_run /// // You can replace the following line with `use crossterm::...;` /// // if you're using the `crossterm` crate with the `input` feature enabled. /// use crossterm::{input, RawScreen, Result}; /// /// fn main() -> Result<()> { /// let input = input(); /// // Read a single character /// let char = input.read_char()?; /// // Read a single line /// let line = input.read_line()?; /// /// // Make sure to enable raw screen when reading input events /// let screen = RawScreen::into_raw_mode(); /// /// // Create async reader /// let mut async_stdin = input.read_async(); /// /// // Create sync reader /// let mut sync_stdin = input.read_sync(); /// /// // Enable mouse input events /// input.enable_mouse_mode()?; /// // Disable mouse input events /// input.disable_mouse_mode() /// } /// ``` pub fn input() -> TerminalInput { TerminalInput::new() }