-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using Ctrl/Alt/Meta with non-character keys
- Loading branch information
Showing
4 changed files
with
224 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,73 @@ | ||
|
||
#[derive(Debug, Clone, PartialEq, Copy)] | ||
pub enum KeyPress { | ||
UnknownEscSeq, | ||
pub enum Key { | ||
Backspace, | ||
Char(char), | ||
Ctrl(char), | ||
Delete, | ||
Down, | ||
End, | ||
Enter, // Ctrl('M') | ||
Enter, | ||
Esc, | ||
Home, | ||
Left, | ||
Meta(char), | ||
Null, | ||
PageDown, | ||
PageUp, | ||
Right, | ||
Tab, // Ctrl('I') | ||
Tab, | ||
Up, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Copy)] | ||
pub enum KeyPress { | ||
Alt(Key), | ||
Ctrl(Key), | ||
Key(Key), | ||
Meta(Key), | ||
Null, | ||
UnknownEscSeq, | ||
} | ||
|
||
#[allow(match_same_arms)] | ||
pub fn char_to_key_press(c: char) -> KeyPress { | ||
if !c.is_control() { | ||
return KeyPress::Char(c); | ||
return KeyPress::Key(Key::Char(c)); | ||
} | ||
match c { | ||
'\x00' => KeyPress::Null, | ||
'\x01' => KeyPress::Ctrl('A'), | ||
'\x02' => KeyPress::Ctrl('B'), | ||
'\x03' => KeyPress::Ctrl('C'), | ||
'\x04' => KeyPress::Ctrl('D'), | ||
'\x05' => KeyPress::Ctrl('E'), | ||
'\x06' => KeyPress::Ctrl('F'), | ||
'\x07' => KeyPress::Ctrl('G'), | ||
'\x08' => KeyPress::Backspace, // '\b' | ||
'\x09' => KeyPress::Tab, | ||
'\x0a' => KeyPress::Ctrl('J'), // '\n' (10) | ||
'\x0b' => KeyPress::Ctrl('K'), | ||
'\x0c' => KeyPress::Ctrl('L'), | ||
'\x0d' => KeyPress::Enter, // '\r' (13) | ||
'\x0e' => KeyPress::Ctrl('N'), | ||
'\x10' => KeyPress::Ctrl('P'), | ||
'\x12' => KeyPress::Ctrl('R'), | ||
'\x13' => KeyPress::Ctrl('S'), | ||
'\x14' => KeyPress::Ctrl('T'), | ||
'\x15' => KeyPress::Ctrl('U'), | ||
'\x16' => KeyPress::Ctrl('V'), | ||
'\x17' => KeyPress::Ctrl('W'), | ||
'\x19' => KeyPress::Ctrl('Y'), | ||
'\x1a' => KeyPress::Ctrl('Z'), | ||
'\x1b' => KeyPress::Esc, | ||
'\x7f' => KeyPress::Backspace, // TODO Validate | ||
'\x01' => KeyPress::Ctrl(Key::Char('A')), | ||
'\x02' => KeyPress::Ctrl(Key::Char('B')), | ||
'\x03' => KeyPress::Ctrl(Key::Char('C')), | ||
'\x04' => KeyPress::Ctrl(Key::Char('D')), | ||
'\x05' => KeyPress::Ctrl(Key::Char('E')), | ||
'\x06' => KeyPress::Ctrl(Key::Char('F')), | ||
'\x07' => KeyPress::Ctrl(Key::Char('G')), | ||
'\x08' => KeyPress::Key(Key::Backspace), | ||
'\x09' => KeyPress::Key(Key::Tab), | ||
'\x0a' => KeyPress::Ctrl(Key::Char('J')), | ||
'\x0b' => KeyPress::Ctrl(Key::Char('K')), | ||
'\x0c' => KeyPress::Ctrl(Key::Char('L')), | ||
'\x0d' => KeyPress::Key(Key::Enter), | ||
'\x0e' => KeyPress::Ctrl(Key::Char('N')), | ||
'\x10' => KeyPress::Ctrl(Key::Char('P')), | ||
'\x12' => KeyPress::Ctrl(Key::Char('R')), | ||
'\x13' => KeyPress::Ctrl(Key::Char('S')), | ||
'\x14' => KeyPress::Ctrl(Key::Char('T')), | ||
'\x15' => KeyPress::Ctrl(Key::Char('U')), | ||
'\x16' => KeyPress::Ctrl(Key::Char('V')), | ||
'\x17' => KeyPress::Ctrl(Key::Char('W')), | ||
'\x19' => KeyPress::Ctrl(Key::Char('Y')), | ||
'\x1a' => KeyPress::Ctrl(Key::Char('Z')), | ||
'\x1b' => KeyPress::Key(Key::Esc), | ||
'\x7f' => KeyPress::Key(Key::Backspace), | ||
_ => KeyPress::Null, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{char_to_key_press, KeyPress}; | ||
use super::{char_to_key_press, Key, KeyPress}; | ||
|
||
#[test] | ||
fn char_to_key() { | ||
assert_eq!(KeyPress::Esc, char_to_key_press('\x1b')); | ||
assert_eq!(KeyPress::Key(Key::Esc), char_to_key_press('\x1b')); | ||
} | ||
} |
Oops, something went wrong.