1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 06:12:16 +03:00

macos: add un-shifted keycode for raw_key

The NSEvent::charactersIgnoringModifiers method ignores modifiers
except for shift, which is unfortunate because it produces eg: `!`
instead of `1`.

This commit adds a mapping from the underlying `keyCode` to the
corresponding letter position.
This commit is contained in:
Wez Furlong 2019-11-06 00:14:52 -08:00
parent a9a0f463e6
commit fea35978c4
3 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,67 @@
#![allow(non_upper_case_globals)]
#![allow(dead_code)]
pub const kVK_ANSI_A: u16 = 0x00;
pub const kVK_ANSI_S: u16 = 0x01;
pub const kVK_ANSI_D: u16 = 0x02;
pub const kVK_ANSI_F: u16 = 0x03;
pub const kVK_ANSI_H: u16 = 0x04;
pub const kVK_ANSI_G: u16 = 0x05;
pub const kVK_ANSI_Z: u16 = 0x06;
pub const kVK_ANSI_X: u16 = 0x07;
pub const kVK_ANSI_C: u16 = 0x08;
pub const kVK_ANSI_V: u16 = 0x09;
pub const kVK_ANSI_B: u16 = 0x0B;
pub const kVK_ANSI_Q: u16 = 0x0C;
pub const kVK_ANSI_W: u16 = 0x0D;
pub const kVK_ANSI_E: u16 = 0x0E;
pub const kVK_ANSI_R: u16 = 0x0F;
pub const kVK_ANSI_Y: u16 = 0x10;
pub const kVK_ANSI_T: u16 = 0x11;
pub const kVK_ANSI_1: u16 = 0x12;
pub const kVK_ANSI_2: u16 = 0x13;
pub const kVK_ANSI_3: u16 = 0x14;
pub const kVK_ANSI_4: u16 = 0x15;
pub const kVK_ANSI_6: u16 = 0x16;
pub const kVK_ANSI_5: u16 = 0x17;
pub const kVK_ANSI_Equal: u16 = 0x18;
pub const kVK_ANSI_9: u16 = 0x19;
pub const kVK_ANSI_7: u16 = 0x1A;
pub const kVK_ANSI_Minus: u16 = 0x1B;
pub const kVK_ANSI_8: u16 = 0x1C;
pub const kVK_ANSI_0: u16 = 0x1D;
pub const kVK_ANSI_RightBracket: u16 = 0x1E;
pub const kVK_ANSI_O: u16 = 0x1F;
pub const kVK_ANSI_U: u16 = 0x20;
pub const kVK_ANSI_LeftBracket: u16 = 0x21;
pub const kVK_ANSI_I: u16 = 0x22;
pub const kVK_ANSI_P: u16 = 0x23;
pub const kVK_ANSI_L: u16 = 0x25;
pub const kVK_ANSI_J: u16 = 0x26;
pub const kVK_ANSI_Quote: u16 = 0x27;
pub const kVK_ANSI_K: u16 = 0x28;
pub const kVK_ANSI_Semicolon: u16 = 0x29;
pub const kVK_ANSI_Backslash: u16 = 0x2A;
pub const kVK_ANSI_Comma: u16 = 0x2B;
pub const kVK_ANSI_Slash: u16 = 0x2C;
pub const kVK_ANSI_N: u16 = 0x2D;
pub const kVK_ANSI_M: u16 = 0x2E;
pub const kVK_ANSI_Period: u16 = 0x2F;
pub const kVK_ANSI_Grave: u16 = 0x32;
pub const kVK_ANSI_KeypadDecimal: u16 = 0x41;
pub const kVK_ANSI_KeypadMultiply: u16 = 0x43;
pub const kVK_ANSI_KeypadPlus: u16 = 0x45;
pub const kVK_ANSI_KeypadClear: u16 = 0x47;
pub const kVK_ANSI_KeypadDivide: u16 = 0x4B;
pub const kVK_ANSI_KeypadEnter: u16 = 0x4C;
pub const kVK_ANSI_KeypadMinus: u16 = 0x4E;
pub const kVK_ANSI_KeypadEquals: u16 = 0x51;
pub const kVK_ANSI_Keypad0: u16 = 0x52;
pub const kVK_ANSI_Keypad1: u16 = 0x53;
pub const kVK_ANSI_Keypad2: u16 = 0x54;
pub const kVK_ANSI_Keypad3: u16 = 0x55;
pub const kVK_ANSI_Keypad4: u16 = 0x56;
pub const kVK_ANSI_Keypad5: u16 = 0x57;
pub const kVK_ANSI_Keypad6: u16 = 0x58;
pub const kVK_ANSI_Keypad7: u16 = 0x59;
pub const kVK_ANSI_Keypad8: u16 = 0x5B;
pub const kVK_ANSI_Keypad9: u16 = 0x5C;

View File

@ -7,6 +7,8 @@ pub mod bitmap;
pub mod connection;
pub mod window;
mod keycodes;
pub use self::window::*;
pub use bitmap::*;
pub use connection::*;

View File

@ -873,6 +873,7 @@ impl WindowView {
let chars = unsafe { nsstring_to_str(nsevent.characters()) };
let unmod = unsafe { nsstring_to_str(nsevent.charactersIgnoringModifiers()) };
let modifiers = unsafe { key_modifiers(nsevent.modifierFlags()) };
let virtual_key = unsafe { nsevent.keyCode() };
if modifiers.is_empty() && !is_a_repeat {
unsafe {
@ -898,11 +899,66 @@ impl WindowView {
}
}
fn normalize_shifted_unmodified_key(kc: KeyCode, virtual_key: u16) -> KeyCode {
use super::keycodes;
match virtual_key {
keycodes::kVK_ANSI_A => KeyCode::Char('a'),
keycodes::kVK_ANSI_B => KeyCode::Char('b'),
keycodes::kVK_ANSI_C => KeyCode::Char('c'),
keycodes::kVK_ANSI_D => KeyCode::Char('d'),
keycodes::kVK_ANSI_E => KeyCode::Char('e'),
keycodes::kVK_ANSI_F => KeyCode::Char('f'),
keycodes::kVK_ANSI_G => KeyCode::Char('g'),
keycodes::kVK_ANSI_H => KeyCode::Char('h'),
keycodes::kVK_ANSI_I => KeyCode::Char('i'),
keycodes::kVK_ANSI_J => KeyCode::Char('j'),
keycodes::kVK_ANSI_K => KeyCode::Char('k'),
keycodes::kVK_ANSI_L => KeyCode::Char('l'),
keycodes::kVK_ANSI_M => KeyCode::Char('m'),
keycodes::kVK_ANSI_N => KeyCode::Char('n'),
keycodes::kVK_ANSI_O => KeyCode::Char('o'),
keycodes::kVK_ANSI_P => KeyCode::Char('p'),
keycodes::kVK_ANSI_Q => KeyCode::Char('q'),
keycodes::kVK_ANSI_R => KeyCode::Char('r'),
keycodes::kVK_ANSI_S => KeyCode::Char('s'),
keycodes::kVK_ANSI_T => KeyCode::Char('t'),
keycodes::kVK_ANSI_U => KeyCode::Char('u'),
keycodes::kVK_ANSI_V => KeyCode::Char('v'),
keycodes::kVK_ANSI_W => KeyCode::Char('w'),
keycodes::kVK_ANSI_X => KeyCode::Char('x'),
keycodes::kVK_ANSI_Y => KeyCode::Char('y'),
keycodes::kVK_ANSI_Z => KeyCode::Char('z'),
keycodes::kVK_ANSI_0 => KeyCode::Char('0'),
keycodes::kVK_ANSI_1 => KeyCode::Char('1'),
keycodes::kVK_ANSI_2 => KeyCode::Char('2'),
keycodes::kVK_ANSI_3 => KeyCode::Char('3'),
keycodes::kVK_ANSI_4 => KeyCode::Char('4'),
keycodes::kVK_ANSI_5 => KeyCode::Char('5'),
keycodes::kVK_ANSI_6 => KeyCode::Char('6'),
keycodes::kVK_ANSI_7 => KeyCode::Char('7'),
keycodes::kVK_ANSI_8 => KeyCode::Char('8'),
keycodes::kVK_ANSI_9 => KeyCode::Char('9'),
keycodes::kVK_ANSI_Equal => KeyCode::Char('='),
keycodes::kVK_ANSI_Minus => KeyCode::Char('-'),
keycodes::kVK_ANSI_LeftBracket => KeyCode::Char('['),
keycodes::kVK_ANSI_RightBracket => KeyCode::Char(']'),
keycodes::kVK_ANSI_Quote => KeyCode::Char('\''),
keycodes::kVK_ANSI_Semicolon => KeyCode::Char(';'),
keycodes::kVK_ANSI_Backslash => KeyCode::Char('\\'),
keycodes::kVK_ANSI_Comma => KeyCode::Char(','),
keycodes::kVK_ANSI_Slash => KeyCode::Char('/'),
keycodes::kVK_ANSI_Period => KeyCode::Char('.'),
keycodes::kVK_ANSI_Grave => KeyCode::Char('`'),
_ => kc,
}
}
if let Some(key) = key_string_to_key_code(chars) {
let raw_key = if chars == unmod {
None
} else {
key_string_to_key_code(unmod)
.map(|kc| normalize_shifted_unmodified_key(kc, virtual_key))
};
let event = KeyEvent {