window: add RawKeyEvent concept

on macos only (for now), we generate a RawKeyEvent prior to
dead key or IME composition and route it to the window to give it
a chance to handle the event.

RawKeyEvent handling is scoped only to key assignments, not generating
input for the terminal.

A raw key event can be marked as handled to prevent moving on to
performing composition and generating cooked key input.

refs: https://github.com/wez/wezterm/issues/877
This commit is contained in:
Wez Furlong committed 2022-01-02 15:04:27 -07:00
1 parent 43d9392c52
commit d5726ba91a
5 files changed
+227 -38

No files matched your search

+117 -14
View File
@@ -1,4 +1,4 @@
use ::window::{KeyCode, KeyEvent, Modifiers, WindowOps};
use ::window::{KeyCode, KeyEvent, Modifiers, RawKeyEvent, WindowOps};
use mux::pane::Pane;
use std::rc::Rc;
@@ -35,6 +35,12 @@ pub enum Key {
None,
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum OnlyKeyBindings {
Yes,
No,
}
impl super::TermWindow {
fn process_key(
&mut self,
@@ -44,6 +50,7 @@ impl super::TermWindow {
raw_modifiers: Modifiers,
leader_active: bool,
leader_mod: Modifiers,
only_key_bindings: OnlyKeyBindings,
) -> bool {
if !leader_active {
// Check to see if this key-press is the leader activating
@@ -72,7 +79,13 @@ impl super::TermWindow {
// While the leader modifier is active, only registered
// keybindings are recognized.
if !leader_active {
let only_key_bindings = match (only_key_bindings, leader_active) {
(OnlyKeyBindings::Yes, _) => OnlyKeyBindings::Yes,
(_, true) => OnlyKeyBindings::Yes,
_ => OnlyKeyBindings::No,
};
if only_key_bindings == OnlyKeyBindings::No {
let config = &self.config;
// This is a bit ugly.
@@ -117,9 +130,99 @@ impl super::TermWindow {
false
}
pub fn key_event_impl(&mut self, window_key: KeyEvent, context: &dyn WindowOps) -> bool {
pub fn raw_key_event_impl(&mut self, key: RawKeyEvent, context: &dyn WindowOps) {
if !key.key_is_down {
return;
}
if self.config.debug_key_events {
log::info!("key_event {:?}", key);
} else {
log::trace!("key_event {:?}", key);
}
// The leader key is a kind of modal modifier key.
// It is allowed to be active for up to the leader timeout duration,
// after which it auto-deactivates.
let (leader_active, leader_mod) = match self.leader_is_down.as_ref() {
Some(expiry) if *expiry > std::time::Instant::now() => {
// Currently active
(true, Modifiers::LEADER)
}
Some(_) => {
// Expired; clear out the old expiration time
self.leader_is_down.take();
(false, Modifiers::NONE)
}
_ => (false, Modifiers::NONE),
};
let pane = match self.get_active_pane_or_overlay() {
Some(pane) => pane,
None => return,
};
// First, try to match raw physical key
let phys_key = match &key.key {
phys @ KeyCode::Physical(_) => Some(phys.clone()),
_ => key.phys_code.map(KeyCode::Physical),
};
if let Some(phys_key) = &phys_key {
if self.process_key(
&pane,
context,
&phys_key,
key.modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
key.set_handled();
return;
}
}
// Then try the raw code
let raw_key = match &key.key {
raw @ KeyCode::RawCode(_) => raw.clone(),
_ => KeyCode::RawCode(key.raw_code),
};
if self.process_key(
&pane,
context,
&raw_key,
key.modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
key.set_handled();
return;
}
if phys_key.as_ref() == Some(&key.key) || raw_key == key.key {
// We already matched against whatever key.key is, so no need
// to do it again below
return;
}
if self.process_key(
&pane,
context,
&key.key,
key.modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
key.set_handled();
}
}
pub fn key_event_impl(&mut self, window_key: KeyEvent, context: &dyn WindowOps) {
if !window_key.key_is_down {
return false;
return;
}
if self.config.debug_key_events {
@@ -130,7 +233,7 @@ impl super::TermWindow {
let pane = match self.get_active_pane_or_overlay() {
Some(pane) => pane,
None => return false,
None => return,
};
// The leader key is a kind of modal modifier key.
@@ -163,8 +266,9 @@ impl super::TermWindow {
window_key.raw_modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
return true;
return;
}
}
@@ -179,8 +283,9 @@ impl super::TermWindow {
window_key.raw_modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
return true;
return;
}
}
@@ -194,8 +299,9 @@ impl super::TermWindow {
window_key.raw_modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
) {
return true;
return;
}
}
@@ -206,8 +312,9 @@ impl super::TermWindow {
window_key.modifiers,
leader_active,
leader_mod,
OnlyKeyBindings::No,
) {
return true;
return;
}
let key = self.win_key_code_to_termwiz_key_code(&window_key.key);
@@ -221,9 +328,6 @@ impl super::TermWindow {
if !key.is_modifier() {
context.invalidate();
}
true
} else {
false
}
}
Key::Composed(s) => {
@@ -237,9 +341,8 @@ impl super::TermWindow {
self.maybe_scroll_to_bottom_for_input(&pane);
context.invalidate();
}
true
}
Key::None => false,
Key::None => {}
}
}
+4
View File
@@ -801,6 +801,10 @@ impl TermWindow {
self.resize(dimensions, window_state, window);
Ok(true)
}
WindowEvent::RawKeyEvent(event) => {
self.raw_key_event_impl(event, window);
Ok(true)
}
WindowEvent::KeyEvent(event) => {
self.key_event_impl(event, window);
Ok(true)
+46
View File
@@ -2,6 +2,8 @@ use bitflags::*;
use serde::*;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
pub struct PixelUnit;
pub struct ScreenPixelUnit;
@@ -513,6 +515,50 @@ pub struct MouseEvent {
pub modifiers: Modifiers,
}
#[derive(Debug, Clone)]
pub struct Handled(Arc<AtomicBool>);
impl Handled {
pub fn default() -> Self {
Self(Arc::new(AtomicBool::new(false)))
}
pub fn set_handled(&self) {
self.0.store(true, std::sync::atomic::Ordering::Relaxed);
}
pub fn is_handled(&self) -> bool {
self.0.load(std::sync::atomic::Ordering::Relaxed)
}
}
/// A key event prior to any dead key or IME composition
#[derive(Debug, Clone)]
pub struct RawKeyEvent {
pub key: KeyCode,
pub modifiers: Modifiers,
/// The physical location of the key on an ANSI-Standard US layout
pub phys_code: Option<PhysKeyCode>,
/// The OS and hardware dependent key code for the key
pub raw_code: u32,
/// How many times this key repeats
pub repeat_count: u16,
/// If true, this is a key down rather than a key up event
pub key_is_down: bool,
pub handled: Handled,
}
impl RawKeyEvent {
/// Mark the event as handled, in order to prevent additional
/// processing.
pub fn set_handled(&self) {
self.handled.set_handled();
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
/// Which key was pressed.
+6
View File
@@ -140,6 +140,12 @@ pub enum WindowEvent {
/// Called when the window gains/loses focus
FocusChanged(bool),
/// Called to handle a raw key event, prior to any dead key,
/// keymap composition or other higher level treatment.
/// If you handle this key event, you must call
/// event.set_handled() to prevent additional processing.
RawKeyEvent(RawKeyEvent),
/// Called to handle a key event.
/// If you didn't handle this event, then you must call
/// window.default_key_processing(key) to allow the system to perform
+54 -24
View File
@@ -4,9 +4,9 @@
use super::{nsstring, nsstring_to_str};
use crate::connection::ConnectionOps;
use crate::{
Clipboard, Connection, Dimensions, KeyCode, KeyEvent, Modifiers, MouseButtons, MouseCursor,
MouseEvent, MouseEventKind, MousePress, Point, Rect, ScreenPoint, Size, WindowDecorations,
WindowEvent, WindowEventSender, WindowOps, WindowState,
Clipboard, Connection, Dimensions, Handled, KeyCode, KeyEvent, Modifiers, MouseButtons,
MouseCursor, MouseEvent, MouseEventKind, MousePress, Point, RawKeyEvent, Rect, ScreenPoint,
Size, WindowDecorations, WindowEvent, WindowEventSender, WindowOps, WindowState,
};
use anyhow::{anyhow, bail, ensure};
use async_trait::async_trait;
@@ -1766,6 +1766,56 @@ impl WindowView {
key_is_down
);
// `Delete` on macos is really Backspace and emits BS.
// `Fn-Delete` emits DEL.
// Alt-Delete is mapped by the IME to be equivalent to Fn-Delete.
// We want to emit Alt-BS in that situation.
let unmod =
if virtual_key == super::keycodes::kVK_Delete && modifiers.contains(Modifiers::ALT) {
"\x08"
} else if virtual_key == super::keycodes::kVK_Tab {
"\t"
} else if virtual_key == super::keycodes::kVK_Delete {
"\x08"
} else if virtual_key == super::keycodes::kVK_ANSI_KeypadEnter {
// https://github.com/wez/wezterm/issues/739
// Keypad enter sends ctrl-c for some reason; explicitly
// treat that as enter here.
"\r"
} else {
unmod
};
let phys_code = super::keycodes::vkey_to_phys(virtual_key);
let raw_key_handled = Handled::default();
let raw_key_event = RawKeyEvent {
key: if unmod.is_empty() {
match phys_code {
Some(phys) => KeyCode::Physical(phys),
None => KeyCode::RawCode(virtual_key as _),
}
} else {
KeyCode::composed(unmod)
},
phys_code,
raw_code: virtual_key as _,
modifiers,
repeat_count: 1,
key_is_down,
handled: raw_key_handled.clone(),
};
if let Some(myself) = Self::get_this(this) {
let mut inner = myself.inner.borrow_mut();
inner
.events
.dispatch(WindowEvent::RawKeyEvent(raw_key_event));
}
if raw_key_handled.is_handled() {
log::trace!("raw key was handled; not processing further");
return;
}
let chars = if let Some(myself) = Self::get_this(this) {
let mut inner = myself.inner.borrow_mut();
@@ -1803,26 +1853,6 @@ impl WindowView {
let send_composed_key_when_right_alt_is_pressed =
config_handle.send_composed_key_when_right_alt_is_pressed;
// `Delete` on macos is really Backspace and emits BS.
// `Fn-Delete` emits DEL.
// Alt-Delete is mapped by the IME to be equivalent to Fn-Delete.
// We want to emit Alt-BS in that situation.
let unmod =
if virtual_key == super::keycodes::kVK_Delete && modifiers.contains(Modifiers::ALT) {
"\x08"
} else if virtual_key == super::keycodes::kVK_Tab {
"\t"
} else if virtual_key == super::keycodes::kVK_Delete {
"\x08"
} else if virtual_key == super::keycodes::kVK_ANSI_KeypadEnter {
// https://github.com/wez/wezterm/issues/739
// Keypad enter sends ctrl-c for some reason; explicitly
// treat that as enter here.
"\r"
} else {
unmod
};
// If unmod is empty it most likely means that the user has selected
// an alternate keymap that has a chorded representation of eg: an ASCII
// character. One example of this is selecting a Norwegian keymap on
@@ -1939,7 +1969,7 @@ impl WindowView {
modifiers,
raw_modifiers,
raw_code: Some(virtual_key as u32),
phys_code: super::keycodes::vkey_to_phys(virtual_key),
phys_code,
repeat_count: 1,
key_is_down,
}