diff --git a/Cargo.lock b/Cargo.lock index be5c056a6..61e1d82f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4779,6 +4779,7 @@ version = "0.1.0" dependencies = [ "bitflags", "euclid", + "lazy_static", "serde", ] diff --git a/config/src/keys.rs b/config/src/keys.rs index 2209c1ddd..f284c01f2 100644 --- a/config/src/keys.rs +++ b/config/src/keys.rs @@ -2,7 +2,8 @@ use crate::{KeyAssignment, MouseEventTrigger}; use luahelper::impl_lua_conversion; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::collections::HashMap; -use wezterm_input_types::{KeyCode, Modifiers}; +use std::convert::TryFrom; +use wezterm_input_types::{KeyCode, Modifiers, PhysKeyCode}; #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct KeyNoAction { @@ -190,6 +191,16 @@ where return Ok(c.clone()); } + if s.len() > 5 && s.starts_with("phys:") { + let phys = PhysKeyCode::try_from(&s[5..]).map_err(|_| { + serde::de::Error::custom(format!( + "expected phys:CODE physical keycode string, got: {}", + s + )) + })?; + return Ok(KeyCode::Physical(phys)); + } + if s.len() > 4 && s.starts_with("raw:") { let num: u32 = s[4..].parse().map_err(|_| { serde::de::Error::custom(format!( diff --git a/wezterm-gui/src/termwindow/keyevent.rs b/wezterm-gui/src/termwindow/keyevent.rs index 0b57c1c66..5eed6a382 100644 --- a/wezterm-gui/src/termwindow/keyevent.rs +++ b/wezterm-gui/src/termwindow/keyevent.rs @@ -267,6 +267,7 @@ impl super::TermWindow { } WK::Char('\u{1b}') => KC::Escape, WK::RawCode(_) => return Key::None, + WK::Physical(_) => return Key::None, WK::Char(c) => KC::Char(*c), WK::Composed(ref s) => { diff --git a/wezterm-input-types/Cargo.toml b/wezterm-input-types/Cargo.toml index bc01177d1..97898be5f 100644 --- a/wezterm-input-types/Cargo.toml +++ b/wezterm-input-types/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] bitflags = "1.3" euclid = "0.22" +lazy_static = "1.4" serde = {version="1.0", features = ["rc", "derive"]} diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 92827aecb..c9b6d97d1 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1,5 +1,6 @@ use bitflags::*; use serde::*; +use std::collections::HashMap; use std::convert::TryFrom; pub struct PixelUnit; @@ -17,6 +18,7 @@ pub enum KeyCode { Char(char), Composed(String), RawCode(u32), + Physical(PhysKeyCode), Hyper, Super, @@ -176,6 +178,282 @@ impl ToString for Modifiers { } } +/// These keycodes identify keys based on their physical +/// position on an ANSI-standard US keyboard. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Hash, Copy)] +pub enum PhysKeyCode { + A, + B, + Backslash, + C, + CapsLock, + Comma, + D, + Delete, + DownArrow, + E, + End, + Equal, + Escape, + F, + F1, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F2, + F20, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + ForwardDelete, + Function, + G, + Grave, + H, + Help, + Home, + I, + J, + K, + K0, + K1, + K2, + K3, + K4, + K5, + K6, + K7, + K8, + K9, + Keypad0, + Keypad1, + Keypad2, + Keypad3, + Keypad4, + Keypad5, + Keypad6, + Keypad7, + Keypad8, + Keypad9, + KeypadClear, + KeypadDecimal, + KeypadDivide, + KeypadEnter, + KeypadEquals, + KeypadMinus, + KeypadMultiply, + KeypadPlus, + L, + LeftAlt, + LeftArrow, + LeftBracket, + LeftControl, + LeftShift, + LeftWindows, + M, + Minus, + Mute, + N, + O, + P, + PageDown, + PageUp, + Period, + Q, + Quote, + R, + Return, + RightAlt, + RightArrow, + RightBracket, + RightControl, + RightShift, + RightWindows, + S, + Semicolon, + Slash, + Space, + T, + Tab, + U, + UpArrow, + V, + VolumeDown, + VolumeUp, + W, + X, + Y, + Z, +} + +impl PhysKeyCode { + fn make_map() -> HashMap { + let mut map = HashMap::new(); + + macro_rules! m { + ($($val:ident),* $(,)?) => { + $( + map.insert(stringify!($val).to_string(), PhysKeyCode::$val); + )* + } + } + + m!( + A, + B, + Backslash, + C, + CapsLock, + Comma, + D, + Delete, + DownArrow, + E, + End, + Equal, + Escape, + F, + F1, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F2, + F20, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + ForwardDelete, + Function, + G, + Grave, + H, + Help, + Home, + I, + J, + K, + Keypad0, + Keypad1, + Keypad2, + Keypad3, + Keypad4, + Keypad5, + Keypad6, + Keypad7, + Keypad8, + Keypad9, + KeypadClear, + KeypadDecimal, + KeypadDivide, + KeypadEnter, + KeypadEquals, + KeypadMinus, + KeypadMultiply, + KeypadPlus, + L, + LeftAlt, + LeftArrow, + LeftBracket, + LeftControl, + LeftShift, + LeftWindows, + M, + Minus, + Mute, + N, + O, + P, + PageDown, + PageUp, + Period, + Q, + Quote, + R, + Return, + RightAlt, + RightArrow, + RightBracket, + RightControl, + RightShift, + RightWindows, + S, + Semicolon, + Slash, + Space, + T, + Tab, + U, + UpArrow, + V, + VolumeDown, + VolumeUp, + W, + X, + Y, + Z, + ); + + map.insert("0".to_string(), PhysKeyCode::K0); + map.insert("1".to_string(), PhysKeyCode::K1); + map.insert("2".to_string(), PhysKeyCode::K2); + map.insert("3".to_string(), PhysKeyCode::K3); + map.insert("4".to_string(), PhysKeyCode::K4); + map.insert("5".to_string(), PhysKeyCode::K5); + map.insert("6".to_string(), PhysKeyCode::K6); + map.insert("7".to_string(), PhysKeyCode::K7); + map.insert("8".to_string(), PhysKeyCode::K8); + map.insert("9".to_string(), PhysKeyCode::K9); + + map + } + + fn make_inv_map() -> HashMap { + let mut map = HashMap::new(); + for (k, v) in PHYSKEYCODE_MAP.iter() { + map.insert(v.clone(), k.clone()); + } + map + } +} + +lazy_static::lazy_static! { + static ref PHYSKEYCODE_MAP: HashMap = PhysKeyCode::make_map(); + static ref INV_PHYSKEYCODE_MAP: HashMap = PhysKeyCode::make_inv_map(); +} + +impl TryFrom<&str> for PhysKeyCode { + type Error = String; + fn try_from(s: &str) -> std::result::Result { + if let Some(code) = PHYSKEYCODE_MAP.get(s) { + Ok(*code) + } else { + Err(format!("invalid PhysKeyCode '{}'", s)) + } + } +} + bitflags! { #[derive(Default)] pub struct MouseButtons: u8 {