keyboard: plumb more modifier+state information through

This commit teaches the termwiz layer about positional modifiers,
and expands our modifier concept to also pass through led states
such as caps lock and num lock.

Those aren't actually keyboard modifiers, but the state is useful
to recognize.

Adjust the shift key normalization so that we don't uppercase
alpha characters when both SHIFT and CAPS_LOCK are held.
This processing will remove both SHIFT and CAPS_LOCK in that
situation.

Add a method to KeyEvent that will undo the OS keyboard layer
normalization of positional to generic modifier key presses.
eg: the OS may map LeftControl -> Control, but we actually
prefer to have LeftControl so if we can unambiguously reverse
that mapping, we do so.

refs: #3476
refs: #3475
This commit is contained in:
Wez Furlong committed 2023-04-09 10:34:53 -07:00
1 parent 397593be5c
commit 2dcf7ad98d
7 files changed
+214 -13

No files matched your search

+59 -3
View File
@@ -45,15 +45,25 @@ use winapi::um::wincon::{
bitflags! {
#[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Modifiers: u8 {
pub struct Modifiers: u16 {
const NONE = 0;
const SHIFT = 1<<1;
const ALT = 1<<2;
const CTRL = 1<<3;
const SUPER = 1<<4;
const LEFT_ALT = 1<<5;
const RIGHT_ALT = 1<<6;
/// This is a virtual modifier used by wezterm
#[doc(hidden)]
const LEADER = 1<<5;
const LEADER = 1<<7;
const LEFT_CTRL = 1<<8;
const RIGHT_CTRL = 1<<9;
const LEFT_SHIFT = 1<<10;
const RIGHT_SHIFT = 1<<11;
const ENHANCED_KEY = 1<<12;
/// Not really a modifier, but a keyboard driver state
const CAPS_LOCK = 1<<13;
/// Not really a modifier, but a keyboard driver state
const NUM_LOCK = 1<<14;
}
}
bitflags! {
@@ -367,6 +377,12 @@ impl KeyCode {
if mods.contains(Modifiers::SUPER) {
modifiers |= 8;
}
if mods.contains(Modifiers::CAPS_LOCK) {
modifiers |= 64;
}
if mods.contains(Modifiers::NUM_LOCK) {
modifiers |= 128;
}
modifiers += 1;
let event_type = if flags.contains(KittyKeyboardFlags::REPORT_EVENT_TYPES) && !is_down {
@@ -2190,4 +2206,44 @@ mod test {
"\u{1b}[97:65:97;1:3u".to_string()
);
}
#[test]
fn encode_issue_3476() {
let mode = KeyCodeEncodeModes {
encoding: KeyboardEncoding::Kitty(
KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES
| KittyKeyboardFlags::REPORT_EVENT_TYPES
| KittyKeyboardFlags::REPORT_ALTERNATE_KEYS
| KittyKeyboardFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES,
),
newline_mode: false,
application_cursor_keys: false,
modify_other_keys: None,
};
assert_eq!(
KeyCode::LeftShift
.encode(Modifiers::NONE, mode, true)
.unwrap(),
"\u{1b}[57441;1u".to_string()
);
assert_eq!(
KeyCode::LeftShift
.encode(Modifiers::NONE, mode, false)
.unwrap(),
"\u{1b}[57441;1:3u".to_string()
);
assert_eq!(
KeyCode::LeftControl
.encode(Modifiers::NONE, mode, true)
.unwrap(),
"\u{1b}[57442;1u".to_string()
);
assert_eq!(
KeyCode::LeftControl
.encode(Modifiers::NONE, mode, false)
.unwrap(),
"\u{1b}[57442;1:3u".to_string()
);
}
}
+35 -6
View File
@@ -174,33 +174,62 @@ impl KeyTableState {
pub fn window_mods_to_termwiz_mods(modifiers: ::window::Modifiers) -> termwiz::input::Modifiers {
let mut result = termwiz::input::Modifiers::NONE;
if modifiers.contains(::window::Modifiers::SHIFT) {
result.insert(termwiz::input::Modifiers::SHIFT);
}
if modifiers.contains(::window::Modifiers::LEFT_SHIFT) {
result.insert(termwiz::input::Modifiers::LEFT_SHIFT);
}
if modifiers.contains(::window::Modifiers::RIGHT_SHIFT) {
result.insert(termwiz::input::Modifiers::RIGHT_SHIFT);
}
if modifiers.contains(::window::Modifiers::LEFT_ALT) {
result.insert(termwiz::input::Modifiers::ALT);
result.insert(termwiz::input::Modifiers::LEFT_ALT);
}
/* We DONT want to do this: we carry through RIGHT_ALT
* only for win32-input mode to track when AltGr was used,
* but we don't want that to be treated as regular ALT
* when encoding regular input for the terminal.
* <https://github.com/wez/wezterm/issues/2127>
if modifiers.contains(::window::Modifiers::RIGHT_ALT) {
/* We DONT want to do this: we carry through RIGHT_ALT
* only for win32-input mode to track when AltGr was used,
* but we don't want that to be treated as regular ALT
* when encoding regular input for the terminal.
* <https://github.com/wez/wezterm/issues/2127>
result.insert(termwiz::input::Modifiers::ALT);
*/
// But we do want the positional mod
result.insert(termwiz::input::Modifiers::RIGHT_ALT);
}
*/
if modifiers.contains(::window::Modifiers::ALT) {
result.insert(termwiz::input::Modifiers::ALT);
}
if modifiers.contains(::window::Modifiers::CTRL) {
result.insert(termwiz::input::Modifiers::CTRL);
}
if modifiers.contains(::window::Modifiers::LEFT_CTRL) {
result.insert(termwiz::input::Modifiers::LEFT_CTRL);
}
if modifiers.contains(::window::Modifiers::RIGHT_CTRL) {
result.insert(termwiz::input::Modifiers::RIGHT_CTRL);
}
if modifiers.contains(::window::Modifiers::SUPER) {
result.insert(termwiz::input::Modifiers::SUPER);
}
if modifiers.contains(::window::Modifiers::LEADER) {
result.insert(termwiz::input::Modifiers::LEADER);
}
if modifiers.contains(::window::Modifiers::ENHANCED_KEY) {
result.insert(termwiz::input::Modifiers::ENHANCED_KEY);
}
if modifiers.contains(::window::Modifiers::CAPS_LOCK) {
result.insert(termwiz::input::Modifiers::CAPS_LOCK);
}
if modifiers.contains(::window::Modifiers::NUM_LOCK) {
result.insert(termwiz::input::Modifiers::NUM_LOCK);
}
result
}
+104 -1
View File
@@ -461,12 +461,17 @@ bitflags! {
const SUPER = 1<<4;
const LEFT_ALT = 1<<5;
const RIGHT_ALT = 1<<6;
/// This is a virtual modifier used by wezterm
const LEADER = 1<<7;
const LEFT_CTRL = 1<<8;
const RIGHT_CTRL = 1<<9;
const LEFT_SHIFT = 1<<10;
const RIGHT_SHIFT = 1<<11;
const ENHANCED_KEY = 1<<12;
/// Not really a modifier, but a keyboard driver state
const CAPS_LOCK = 1<<13;
/// Not really a modifier, but a keyboard driver state
const NUM_LOCK = 1<<14;
}
}
@@ -619,6 +624,24 @@ impl Modifiers {
"ENHANCED_KEY",
"ENHANCED_KEY",
),
(
Self::CAPS_LOCK,
"CAPS_LOCK",
"CAPS_LOCK",
"CAPS_LOCK",
"CAPS_LOCK",
"CAPS_LOCK",
"CAPS_LOCK",
),
(
Self::NUM_LOCK,
"NUM_LOCK",
"NUM_LOCK",
"NUM_LOCK",
"NUM_LOCK",
"NUM_LOCK",
"NUM_LOCK",
),
] {
if !self.contains(value) {
continue;
@@ -1223,7 +1246,9 @@ pub struct KeyEvent {
}
fn normalize_shift(key: KeyCode, modifiers: Modifiers) -> (KeyCode, Modifiers) {
if modifiers.contains(Modifiers::SHIFT) {
if modifiers.contains(Modifiers::SHIFT | Modifiers::CAPS_LOCK) {
(key, modifiers - (Modifiers::SHIFT | Modifiers::CAPS_LOCK))
} else if modifiers.contains(Modifiers::SHIFT) {
match key {
KeyCode::Char(c) if c.is_ascii_uppercase() => (key, modifiers - Modifiers::SHIFT),
KeyCode::Char(c) if c.is_ascii_lowercase() => (
@@ -1270,6 +1295,84 @@ impl KeyEvent {
self
}
/// If the key code is a modifier key (Control, Alt, Shift), check
/// the underlying raw event to see if we had a positional version
/// of that key.
/// If so, switch to the positional version.
pub fn resurface_positional_modifier_key(mut self) -> Self {
match self.key {
KeyCode::Control
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::LeftControl | KeyCode::Physical(PhysKeyCode::LeftControl),
..
})
) =>
{
self.key = KeyCode::LeftControl;
}
KeyCode::Control
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::RightControl | KeyCode::Physical(PhysKeyCode::RightControl),
..
})
) =>
{
self.key = KeyCode::RightControl;
}
KeyCode::Alt
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::LeftAlt | KeyCode::Physical(PhysKeyCode::LeftAlt),
..
})
) =>
{
self.key = KeyCode::LeftAlt;
}
KeyCode::Alt
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::RightAlt | KeyCode::Physical(PhysKeyCode::RightAlt),
..
})
) =>
{
self.key = KeyCode::RightAlt;
}
KeyCode::Shift
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::LeftShift | KeyCode::Physical(PhysKeyCode::LeftShift),
..
})
) =>
{
self.key = KeyCode::LeftShift;
}
KeyCode::Shift
if matches!(
self.raw,
Some(RawKeyEvent {
key: KeyCode::RightShift | KeyCode::Physical(PhysKeyCode::RightShift),
..
})
) =>
{
self.key = KeyCode::RightShift;
}
_ => {}
}
self
}
/// If CTRL is held down and we have KeyCode::Char(_) with the
/// ASCII control value encoded, decode it back to the ASCII
/// alpha keycode instead.
+2 -1
View File
@@ -2598,7 +2598,8 @@ impl WindowView {
key_is_down,
raw: Some(raw_key_event),
}
.normalize_shift();
.normalize_shift()
.resurface_positional_modifier_key();
log::debug!(
"key_common {:?} (chars={:?} unmod={:?} modifiers={:?})",
+1
View File
@@ -2495,6 +2495,7 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<L
raw: None,
}
.normalize_shift()
.resurface_positional_modifier_key()
.normalize_ctrl();
inner.events.dispatch(WindowEvent::KeyEvent(key.clone()));
+11 -1
View File
@@ -369,7 +369,8 @@ impl Keyboard {
key_is_down: pressed,
raw: Some(raw_key_event),
}
.normalize_shift();
.normalize_shift()
.resurface_positional_modifier_key();
if pressed && want_repeat {
events.dispatch(WindowEvent::KeyEvent(event.clone()));
@@ -387,6 +388,9 @@ impl Keyboard {
.borrow()
.mod_name_is_active(modifier, xkb::STATE_MODS_EFFECTIVE)
}
fn led_is_active(&self, led: &str) -> bool {
self.state.borrow().led_name_is_active(led)
}
pub fn get_key_modifiers(&self) -> Modifiers {
let mut res = Modifiers::default();
@@ -405,6 +409,12 @@ impl Keyboard {
// Mod4
res |= Modifiers::SUPER;
}
if self.led_is_active(xkb::LED_NAME_NUM) {
res |= Modifiers::NUM_LOCK;
}
if self.led_is_active(xkb::LED_NAME_CAPS) {
res |= Modifiers::CAPS_LOCK;
}
res
}
+2 -1
View File
@@ -638,7 +638,8 @@ impl XWindowInner {
key_is_down: true,
raw: None,
}
.normalize_shift();
.normalize_shift()
.resurface_positional_modifier_key();
self.events.dispatch(WindowEvent::KeyEvent(key_event));
}