Add input support for more keys

Shift and Ctrl Function keys
Shift-Del
This commit is contained in:
Alexandre Bury 2015-05-29 11:31:20 -07:00
parent e07a0319f2
commit 00b75a98f6

View File

@ -38,6 +38,8 @@ pub enum Key {
End,
/// Delete key
Del,
ShiftDel,
CtrlDel,
/// Insert key
Ins,
/// Escape key. Often buffered by the terminal,
@ -48,14 +50,16 @@ pub enum Key {
CtrlPageDown,
ShiftLeft,
ShiftRight,
ShiftCtrlLeft,
ShiftCtrlRight,
CtrlShiftLeft,
CtrlShiftRight,
CtrlLeft,
CtrlRight,
CtrlUp,
CtrlDown,
CtrlDel,
F(u8),
CtrlF(u8),
ShiftF(u8),
CtrlShiftF(u8),
CtrlChar(char),
Unknown(i32),
}
@ -63,22 +67,27 @@ pub enum Key {
impl Key {
pub fn from_ncurses(ch: i32) -> Self {
match ch {
// Tab is '\t'
9 => Key::Tab,
// Treat Return and the numpad Enter the same
// Treat '\n' and the numpad Enter the same
10 | ncurses::KEY_ENTER => Key::Enter,
// This is the escape key when pressed by itself.
// When used for control sequences, it should have been caught earlier.
27 => Key::Esc,
// `Backspace` sends 127, but Ctrl-H sends `Backspace`
127 | ncurses::KEY_BACKSPACE => Key::Backspace,
// Del and Ins currently use a bad value in ncurses-rs
330 => Key::Del,
331 => Key::Ins,
// Those keys don't seem to be documented...
515 => Key::CtrlDel,
521 => Key::CtrlDown,
541 => Key::CtrlLeft,
542 => Key::ShiftCtrlLeft,
542 => Key::CtrlShiftLeft,
546 => Key::CtrlPageDown,
551 => Key::CtrlPageUp,
556 => Key::CtrlRight,
557 => Key::ShiftCtrlRight,
557 => Key::CtrlShiftRight,
562 => Key::CtrlUp,
ncurses::KEY_BTAB => Key::ShiftTab,
ncurses::KEY_SLEFT => Key::ShiftLeft,
@ -91,7 +100,15 @@ impl Key {
ncurses::KEY_NPAGE => Key::PageDown,
ncurses::KEY_HOME => Key::Home,
ncurses::KEY_END => Key::End,
ncurses::KEY_SDC => Key::ShiftDel,
// All Fn keys use the same enum with associated number
f @ ncurses::KEY_F1 ... ncurses::KEY_F15 => Key::F((f - ncurses::KEY_F0) as u8),
f @ 281 ... 291 => Key::ShiftF((f - 281 + 5) as u8),
f @ 293 ... 303 => Key::CtrlF((f - 293 + 5) as u8),
f @ 305 ... 315 => Key::CtrlShiftF((f - 305 + 5) as u8),
// Shift and Ctrl F{1-4} need escape sequences...
// TODO: shift and ctrl Fn keys
// Avoids 8-10 (H,I,J), they are used by other commands.
c @ 1 ... 7 | c @ 11 ... 25 => Key::CtrlChar(('a' as u8 + (c-1) as u8) as char),
_ => Key::Unknown(ch),
@ -105,15 +122,19 @@ impl fmt::Display for Key {
Key::Unknown(ch) => write!(f, "Unknown: {}", ch),
Key::CtrlChar(ch) => write!(f, "Ctrl-{}", ch),
Key::F(n) => write!(f, "F{}", n),
Key::ShiftF(n) => write!(f, "Shift-F{}", n),
Key::CtrlF(n) => write!(f, "Ctrl-F{}", n),
Key::CtrlShiftF(n) => write!(f, "Ctrl-Shift-F{}", n),
key => write!(f, "{}", match key {
Key::Left => "Left",
Key::Right => "Right",
Key::Down => "Down",
Key::Up => "Up",
Key::ShiftCtrlLeft => "Shift-Ctrl-Left",
Key::ShiftCtrlRight => "Shift-Ctrl-Right",
Key::CtrlShiftLeft => "Ctrl-Shift-Left",
Key::CtrlShiftRight => "Ctrl-Shift-Right",
Key::ShiftLeft => "Shift-Left",
Key::ShiftRight => "Shift-Right",
Key::ShiftDel => "Shift-Del",
Key::CtrlDel => "Ctrl-Del",
Key::CtrlLeft => "Ctrl-Left",
Key::CtrlRight => "Ctrl-Right",
@ -132,7 +153,7 @@ impl fmt::Display for Key {
Key::Tab => "Tab",
Key::Ins => "Ins",
Key::Esc => "Esc",
_ => "",
_ => "Missing key label",
}),
}
}