Skip to content

Commit 8b7fc3b

Browse files
committed
🔧 Generalized
1 parent fbbc070 commit 8b7fc3b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ version = "0.52.0"
2020
features = [
2121
"Win32_Foundation",
2222
"Win32_System_Console",
23-
"Win32_System_SystemServices"
23+
"Win32_System_SystemServices",
24+
"Win32_UI",
25+
"Win32_UI_Input",
26+
"Win32_UI_Input_KeyboardAndMouse",
2427
]

src/read.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub mod windows {
6767
GetConsoleMode, GetStdHandle, SetConsoleMode, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT,
6868
STD_INPUT_HANDLE,
6969
};
70+
use windows_sys::Win32::UI::Input::KeyboardAndMouse;
7071

7172
use super::Key;
7273

@@ -108,20 +109,20 @@ pub mod windows {
108109

109110
// Internal function for reading a key from the console.
110111
pub(crate) fn read_key() -> Option<Key> {
111-
let mut buffer = [0; 3];
112+
let mut buffer = [0; 2];
112113
disable_line_buffering().unwrap();
113114
if std::io::stdin().read(&mut buffer).is_ok() {
114115
enable_line_buffering().unwrap();
115-
match buffer {
116-
[13, 0, 0] => Some(Key::Enter),
117-
[9, 0, 0] => Some(Key::Tab),
118-
[8, 0, 0] => Some(Key::Backspace),
119-
[27, 0, 0] => Some(Key::Escape),
120-
[0, 0, 72] => Some(Key::ArrowUp),
121-
[0, 0, 80] => Some(Key::ArrowDown),
122-
[0, 0, 77] => Some(Key::ArrowRight),
123-
[0, 0, 75] => Some(Key::ArrowLeft),
124-
[c, _, _] => Some(Key::Char(c as char)),
116+
match u16::from_le_bytes(buffer) {
117+
KeyboardAndMouse::VK_RETURN => Some(Key::Enter),
118+
KeyboardAndMouse::VK_TAB => Some(Key::Tab),
119+
KeyboardAndMouse::VK_BACK => Some(Key::Backspace),
120+
KeyboardAndMouse::VK_ESCAPE => Some(Key::Escape),
121+
KeyboardAndMouse::VK_UP => Some(Key::ArrowUp),
122+
KeyboardAndMouse::VK_DOWN => Some(Key::ArrowDown),
123+
KeyboardAndMouse::VK_RIGHT => Some(Key::ArrowRight),
124+
KeyboardAndMouse::VK_LEFT => Some(Key::ArrowLeft),
125+
c => Some(Key::Char(char::from_u32(c.into()).unwrap())),
125126
}
126127
} else {
127128
None

0 commit comments

Comments
 (0)