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
//! Read Implementation Cross Platform
//!
//! This module provides cross-platform functionality for reading keyboard input,
//! allowing your console application to handle various key events uniformly.
//! # Examples
//!
//! ```no_run
//! use console_utils::read::{Key, read_key};
//!
//! // Cross-platform key reading example
//! let key = read_key().unwrap();
//!
//! println!("Pressed key: {:?}", key);
//! ```
/// # Key Enum
///
/// The `Key` enum represents different keyboard keys that can be captured by the
/// `read_key` function.
///
/// - `ArrowUp`: Represents the arrow up key.
/// - `ArrowDown`: Represents the arrow down key.
/// - `ArrowRight`: Represents the arrow right key.
/// - `ArrowLeft`: Represents the arrow left key.
/// - `Enter`: Represents the Enter/Return key.
/// - `Tab`: Represents the Tab key.
/// - `Backspace`: Represents the Backspace key.
/// - `Escape`: Represents the Escape key.
/// - `Char(char)`: Represents any printable character on the keyboard.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Key {
ArrowUp,
ArrowDown,
ArrowRight,
ArrowLeft,
Enter,
Tab,
Backspace,
Escape,
Char(char),
}
/// # Read Key Function
///
/// The `read_key` function reads a single key event from the console input
/// and returns a `Key` enum.
pub fn read_key() -> Option<Key> {
#[cfg(windows)]
{
windows::read_key()
}
#[cfg(unix)]
{
unix::read_key()
}
}
/// # Windows Module
///
/// The `windows` module contains Windows-specific implementation details for reading
/// keyboard input. It utilizes the `windows` crate to interact with Windows Console API.
#[cfg(windows)]
pub mod windows {
use std::io::{self, Read};
use windows_sys::Win32::System::Console::{
GetConsoleMode, GetStdHandle, SetConsoleMode, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT,
STD_INPUT_HANDLE,
};
use super::Key;
// Internal function for disabling line buffering.
fn disable_line_buffering() -> io::Result<()> {
let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) };
let mut mode: u32 = 0;
unsafe {
if GetConsoleMode(handle, &mut mode) == 0 {
return Err(io::Error::last_os_error());
}
if SetConsoleMode(handle, mode & !(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)) == 0 {
return Err(io::Error::last_os_error());
}
}
Ok(())
}
// Internal function for enabling line buffering.
fn enable_line_buffering() -> io::Result<()> {
let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) };
let mut mode: u32 = 0;
unsafe {
if GetConsoleMode(handle, &mut mode) == 0 {
return Err(io::Error::last_os_error());
}
if SetConsoleMode(handle, mode | (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)) == 0 {
return Err(io::Error::last_os_error());
}
}
Ok(())
}
// Internal function for reading a key from the console.
pub(crate) fn read_key() -> Option<Key> {
let mut buffer = [0; 3];
disable_line_buffering().unwrap();
if std::io::stdin().read(&mut buffer).is_ok() {
enable_line_buffering().unwrap();
match buffer[0] {
13 => Some(Key::Enter),
9 => Some(Key::Tab),
8 => Some(Key::Backspace),
27 => Some(Key::Escape),
38 => Some(Key::ArrowUp),
40 => Some(Key::ArrowDown),
39 => Some(Key::ArrowRight),
37 => Some(Key::ArrowLeft),
c => Some(Key::Char(c as char)),
}
} else {
None
}
}
}
/// # Unix Module
///
/// The `unix` module contains Unix-specific implementation details for reading
/// keyboard input. It uses the `libc` crate to manipulate terminal attributes.
#[cfg(unix)]
pub mod unix {
use libc::{tcgetattr, tcsetattr, ECHO, ICANON, STDIN_FILENO, TCSANOW};
use std::io::{self, Read};
use std::mem;
use super::Key;
// Internal function for disabling line buffering.
fn disable_line_buffering() -> io::Result<()> {
let mut termios = unsafe { mem::zeroed() };
if unsafe { tcgetattr(STDIN_FILENO, &mut termios) } != 0 {
return Err(io::Error::last_os_error());
}
termios.c_lflag &= !(ICANON | ECHO);
if unsafe { tcsetattr(STDIN_FILENO, TCSANOW, &termios) } != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
// Internal function for enabling line buffering.
fn enable_line_buffering() -> io::Result<()> {
let mut termios = unsafe { mem::zeroed() };
if unsafe { tcgetattr(STDIN_FILENO, &mut termios) } != 0 {
return Err(io::Error::last_os_error());
}
termios.c_lflag |= ICANON | ECHO;
if unsafe { tcsetattr(STDIN_FILENO, TCSANOW, &termios) } != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
// Internal function for reading a key from the console.
pub(crate) fn read_key() -> Option<Key> {
let mut buffer = [0; 3];
disable_line_buffering().unwrap();
if std::io::stdin().read(&mut buffer).is_ok() {
enable_line_buffering().unwrap();
match buffer[0] {
27 => {
// Arrow key sequence
if buffer[1] == 91 {
match buffer[2] {
65 => Some(Key::ArrowUp),
66 => Some(Key::ArrowDown),
67 => Some(Key::ArrowRight),
68 => Some(Key::ArrowLeft),
_ => None,
}
} else {
None
}
}
b'\n' => Some(Key::Enter),
b'\t' => Some(Key::Tab),
127 => Some(Key::Backspace),
c => Some(Key::Char(c as char)),
}
} else {
None
}
}
}