Skip to content

Commit

Permalink
support shift/ctrl/alt + arrow keys combinations added at termion v4
Browse files Browse the repository at this point in the history
fix #68

termion added the support at this commit:

redox-os/termion@3b52dc9
  • Loading branch information
rhysd committed Aug 2, 2024
1 parent e5500df commit c4f1b22
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/input/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl From<KeyEvent> for Input {
fn from(key: KeyEvent) -> Self {
let mut ctrl = false;
let mut alt = false;
let mut shift = false;
let key = match key {
KeyEvent::Char('\n' | '\r') => Key::Enter,
KeyEvent::Char(c) => Key::Char(c),
Expand All @@ -30,9 +31,57 @@ impl From<KeyEvent> for Input {
}
KeyEvent::Backspace => Key::Backspace,
KeyEvent::Left => Key::Left,
KeyEvent::ShiftLeft => {
shift = true;
Key::Left
}
KeyEvent::AltLeft => {
alt = true;
Key::Left
}
KeyEvent::CtrlLeft => {
ctrl = true;
Key::Left
}
KeyEvent::Right => Key::Right,
KeyEvent::ShiftRight => {
shift = true;
Key::Right
}
KeyEvent::AltRight => {
alt = true;
Key::Right
}
KeyEvent::CtrlRight => {
ctrl = true;
Key::Right
}
KeyEvent::Up => Key::Up,
KeyEvent::ShiftUp => {
shift = true;
Key::Up
}
KeyEvent::AltUp => {
alt = true;
Key::Up
}
KeyEvent::CtrlUp => {
ctrl = true;
Key::Up
}
KeyEvent::Down => Key::Down,
KeyEvent::ShiftDown => {
shift = true;
Key::Down
}
KeyEvent::AltDown => {
alt = true;
Key::Down
}
KeyEvent::CtrlDown => {
ctrl = true;
Key::Down
}
KeyEvent::Home => Key::Home,
KeyEvent::End => Key::End,
KeyEvent::PageUp => Key::PageUp,
Expand All @@ -48,7 +97,7 @@ impl From<KeyEvent> for Input {
key,
ctrl,
alt,
shift: false,
shift,
}
}
}
Expand Down Expand Up @@ -106,6 +155,9 @@ mod tests {
(KeyEvent::F(1), input(Key::F(1), false, false, false)),
(KeyEvent::BackTab, input(Key::Tab, false, false, false)),
(KeyEvent::Null, input(Key::Null, false, false, false)),
(KeyEvent::ShiftDown, input(Key::Down, false, false, true)),
(KeyEvent::AltUp, input(Key::Up, false, true, false)),
(KeyEvent::CtrlLeft, input(Key::Left, false, false, false)),
] {
assert_eq!(Input::from(from), to, "{:?} -> {:?}", from, to);
}
Expand Down

0 comments on commit c4f1b22

Please sign in to comment.