Add support for cursor keys

This commit is contained in:
Wez Furlong committed 2018-01-31 20:31:06 -08:00
1 parent a49e4b653b
commit e7e0aeebc2
2 files changed
+99 -27

No files matched your search

+8
View File
@@ -210,6 +210,14 @@ pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode {
XK_Super_L | XK_Super_R => KeyCode::Super,
XK_Hyper_L | XK_Hyper_R => KeyCode::Hyper,
XK_Shift_L | XK_Shift_R => KeyCode::Shift,
XK_Home => KeyCode::Home,
XK_Left => KeyCode::Left,
XK_Up => KeyCode::Up,
XK_Right => KeyCode::Right,
XK_Down => KeyCode::Down,
XK_Page_Up => KeyCode::PageUp,
XK_Page_Down => KeyCode::PageDown,
XK_End => KeyCode::End,
_ => KeyCode::Unknown,
}
}
+91 -27
View File
@@ -59,6 +59,14 @@ pub enum KeyCode {
Super,
Hyper,
Shift,
Left,
Up,
Right,
Down,
PageUp,
PageDown,
Home,
End,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -506,6 +514,19 @@ pub struct TerminalState {
/// The scroll region
scroll_top: usize,
scroll_bottom: usize,
/// When set, modifies the sequence of bytes sent for keys
/// designated as cursor keys. This includes various navigation
/// keys. The code in key_down() is responsible for interpreting this.
application_cursor_keys: bool,
/// When set, modifies the sequence of bytes sent for keys
/// in the numeric keypad portion of the keyboard.
application_keypad: bool,
/// When set, pasting the clipboard should bracket the data with
/// designated marker characters.
bracketed_paste: bool,
}
impl TerminalState {
@@ -528,6 +549,9 @@ impl TerminalState {
scroll_top: 0,
scroll_bottom: physical_rows - 1,
wrap_next: false,
application_cursor_keys: false,
application_keypad: false,
bracketed_paste: false,
}
}
@@ -547,37 +571,69 @@ impl TerminalState {
}
}
/// Processes a key_down event generated by the gui/render layer
/// that is embedding the Terminal. This method translates the
/// keycode into a sequence of bytes to send to the slave end
/// of the pty via the `Write`-able object provided by the caller.
pub fn key_down<W: std::io::Write>(
&mut self,
key: KeyCode,
mods: KeyModifiers,
write: &mut W,
) -> Result<(), Error> {
match key {
KeyCode::Char(c) => {
let adjusted = if mods.contains(KeyModifiers::CTRL) && c <= 0xff as char {
if mods.contains(KeyModifiers::SHIFT) {
// If shift is held we have C == 0x43 and want to translate
// that into 0x03
(c as u8 - 0x40) as char
} else {
// If shift is not held we have c == 0x63 and want to translate
// that into 0x03
(c as u8 - 0x60) as char
}
} else if mods.contains(KeyModifiers::ALT) && c <= 0xff as char {
(c as u8 | 0x80) as char
} else {
c
};
const CTRL: KeyModifiers = KeyModifiers::CTRL;
const SHIFT: KeyModifiers = KeyModifiers::SHIFT;
const ALT: KeyModifiers = KeyModifiers::ALT;
const APPCURSOR: bool = true;
use KeyCode::*;
let mut buf = [0; 8];
let encoded = adjusted.encode_utf8(&mut buf);
write.write(encoded.as_bytes())?;
Ok(())
let ctrl = mods & CTRL;
let shift = mods & SHIFT;
let alt = mods & ALT;
// https://doc.rust-lang.org/std/primitive.char.html#method.encode_utf8
// says "A buffer of length four is large enough to encode any char."
let mut buf = [0u8; 4];
// TODO: also respect self.application_keypad
let to_send = match (key, ctrl, alt, shift, self.application_cursor_keys) {
(Char(c), CTRL, _, SHIFT, _) if c <= 0xff as char => {
// If shift is held we have C == 0x43 and want to translate
// that into 0x03
((c as u8 - 0x40) as char).encode_utf8(&mut buf) as &str
}
_ => Ok(()),
}
(Char(c), CTRL, ..) if c <= 0xff as char => {
// If shift is not held we have C == 0x63 and want to translate
// that into 0x03
((c as u8 - 0x60) as char).encode_utf8(&mut buf) as &str
}
(Char(c), _, ALT, ..) if c <= 0xff as char => {
((c as u8 | 0x80) as char).encode_utf8(&mut buf) as &str
}
(Char(c), ..) => c.encode_utf8(&mut buf),
(Up, _, _, _, APPCURSOR) => "\x1bOA",
(Down, _, _, _, APPCURSOR) => "\x1bOB",
(Right, _, _, _, APPCURSOR) => "\x1bOC",
(Left, _, _, _, APPCURSOR) => "\x1bOD",
(Home, _, _, _, APPCURSOR) => "\x1bOH",
(End, _, _, _, APPCURSOR) => "\x1bOF",
(Up, ..) => "\x1b[A",
(Down, ..) => "\x1b[B",
(Right, ..) => "\x1b[C",
(Left, ..) => "\x1b[D",
(PageUp, ..) => "\x1b[5~",
(PageDown, ..) => "\x1b[6~",
(Home, ..) => "\x1b[H",
(End, ..) => "\x1b[F",
// Modifier keys pressed on their own and unmappable keys don't expand to anything
(Control, ..) | (Alt, ..) | (Meta, ..) | (Super, ..) | (Hyper, ..) | (Shift, ..) |
(Unknown, ..) => "",
};
write.write(&to_send.as_bytes())?;
Ok(())
}
pub fn key_up<W: std::io::Write>(
@@ -912,8 +968,12 @@ impl vte::Perform for TerminalState {
}
}
}
CSIAction::SetDecPrivateMode(DecPrivateMode::ApplicationCursorKeys, _on) => {}
CSIAction::SetDecPrivateMode(DecPrivateMode::BrackedPaste, _on) => {}
CSIAction::SetDecPrivateMode(DecPrivateMode::ApplicationCursorKeys, on) => {
self.application_cursor_keys = on;
}
CSIAction::SetDecPrivateMode(DecPrivateMode::BrackedPaste, on) => {
self.bracketed_paste = on;
}
CSIAction::DeviceStatusReport => {
// "OK"
self.push_answerback(b"\x1b[0n");
@@ -980,9 +1040,13 @@ impl vte::Perform for TerminalState {
// handled by vte::Parser
(b'\\', &[], &[]) => {}
// Application Keypad (DECKPAM)
(b'=', &[], &[]) => {}
(b'=', &[], &[]) => {
self.application_keypad = true;
}
// Normal Keypad (DECKPAM)
(b'>', &[], &[]) => {}
(b'>', &[], &[]) => {
self.application_keypad = false;
}
// Reverse Index (RI)
(b'M', &[], &[]) => self.reverse_index(),