Skip to content

Commit

Permalink
input: route key up events to pane on win32
Browse files Browse the repository at this point in the history
refs: #1509
  • Loading branch information
wez committed Jan 6, 2022
1 parent 763c4d7 commit 27d452a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
75 changes: 45 additions & 30 deletions wezterm-gui/src/termwindow/keyevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl super::TermWindow {
leader_active: bool,
leader_mod: Modifiers,
only_key_bindings: OnlyKeyBindings,
is_down: bool,
) -> bool {
if !leader_active {
if is_down && !leader_active {
// Check to see if this key-press is the leader activating
if let Some(duration) = self.input_map.is_leader(&keycode, raw_modifiers) {
// Yes; record its expiration
Expand All @@ -73,27 +74,29 @@ impl super::TermWindow {
}
}

if let Some(assignment) = self
.input_map
.lookup_key(&keycode, raw_modifiers | leader_mod)
{
if self.config.debug_key_events {
log::info!(
"{:?} {:?} -> perform {:?}",
keycode,
raw_modifiers | leader_mod,
assignment
);
}
self.perform_key_assignment(&pane, &assignment).ok();
context.invalidate();
if is_down {
if let Some(assignment) = self
.input_map
.lookup_key(&keycode, raw_modifiers | leader_mod)
{
if self.config.debug_key_events {
log::info!(
"{:?} {:?} -> perform {:?}",
keycode,
raw_modifiers | leader_mod,
assignment
);
}
self.perform_key_assignment(&pane, &assignment).ok();
context.invalidate();

if leader_active {
// A successful leader key-lookup cancels the leader
// virtual modifier state
self.leader_done();
if leader_active {
// A successful leader key-lookup cancels the leader
// virtual modifier state
self.leader_done();
}
return true;
}
return true;
}

// While the leader modifier is active, only registered
Expand Down Expand Up @@ -138,7 +141,14 @@ impl super::TermWindow {
tw_raw_modifiers
);
}
if pane.key_down(term_key, tw_raw_modifiers).is_ok() {

let res = if is_down {
pane.key_down(term_key, tw_raw_modifiers)
} else {
pane.key_up(term_key, tw_raw_modifiers)
};

if res.is_ok() {
if !keycode.is_modifier()
&& self.pane_state(pane.pane_id()).overlay.is_none()
{
Expand All @@ -159,10 +169,6 @@ impl super::TermWindow {
}

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 {
Expand Down Expand Up @@ -199,6 +205,7 @@ impl super::TermWindow {
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
key.key_is_down,
) {
key.set_handled();
return;
Expand All @@ -218,6 +225,7 @@ impl super::TermWindow {
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
key.key_is_down,
) {
key.set_handled();
return;
Expand All @@ -237,6 +245,7 @@ impl super::TermWindow {
leader_active,
leader_mod,
OnlyKeyBindings::Yes,
key.key_is_down,
) {
key.set_handled();
}
Expand Down Expand Up @@ -280,10 +289,6 @@ impl super::TermWindow {
}

pub fn key_event_impl(&mut self, window_key: KeyEvent, context: &dyn WindowOps) {
if !window_key.key_is_down {
return;
}

if self.config.debug_key_events {
log::info!("key_event {:?}", window_key);
} else {
Expand Down Expand Up @@ -315,6 +320,7 @@ impl super::TermWindow {
leader_active,
leader_mod,
OnlyKeyBindings::No,
window_key.key_is_down,
) {
return;
}
Expand All @@ -335,7 +341,13 @@ impl super::TermWindow {
log::info!("send to pane key={:?} mods={:?}", key, modifiers);
}

if pane.key_down(key, modifiers).is_ok() {
let res = if window_key.key_is_down {
pane.key_down(key, modifiers)
} else {
pane.key_up(key, modifiers)
};

if res.is_ok() {
if !key.is_modifier() && self.pane_state(pane.pane_id()).overlay.is_none() {
self.maybe_scroll_to_bottom_for_input(&pane);
}
Expand All @@ -346,6 +358,9 @@ impl super::TermWindow {
}
}
Key::Composed(s) => {
if !window_key.key_is_down {
return;
}
if leader_active {
// Leader was pressed and this non-modifier keypress isn't
// a registered key binding; swallow this event and cancel
Expand Down
6 changes: 2 additions & 4 deletions wezterm-input-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ impl KeyCode {
Self::Function(19) => PhysKeyCode::F19,
Self::Function(20) => PhysKeyCode::F20,
Self::Physical(p) => *p,
Self::LeftShift => PhysKeyCode::LeftShift,
Self::Shift | Self::LeftShift => PhysKeyCode::LeftShift,
Self::RightShift => PhysKeyCode::RightShift,
Self::Alt | Self::LeftAlt => PhysKeyCode::LeftAlt,
Self::RightAlt => PhysKeyCode::RightAlt,
Self::LeftWindows => PhysKeyCode::LeftWindows,
Self::RightWindows => PhysKeyCode::RightWindows,
Self::LeftControl => PhysKeyCode::LeftControl,
Self::Control | Self::LeftControl => PhysKeyCode::LeftControl,
Self::RightControl => PhysKeyCode::RightControl,
Self::CapsLock => PhysKeyCode::CapsLock,
Self::PageUp => PhysKeyCode::PageUp,
Expand Down Expand Up @@ -281,9 +281,7 @@ impl KeyCode {
| Self::Select
| Self::VoidSymbol
| Self::Pause
| Self::Shift
| Self::Cancel
| Self::Control
| Self::Hyper
| Self::Super
| Self::Meta
Expand Down
1 change: 1 addition & 0 deletions window/src/os/windows/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ fn build_map() -> HashMap<WPARAM, PhysKeyCode> {
(VK_BACK, PhysKeyCode::Backspace),
(VK_ESCAPE, PhysKeyCode::Escape),
(VK_LWIN, PhysKeyCode::LeftWindows),
(VK_SHIFT, PhysKeyCode::LeftShift),
(VK_LSHIFT, PhysKeyCode::LeftShift),
(VK_CAPITAL, PhysKeyCode::CapsLock),
(VK_MENU, PhysKeyCode::LeftAlt),
Expand Down
15 changes: 10 additions & 5 deletions window/src/os/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,11 +1770,6 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<L
// ToUnicode has frustrating statefulness so we take care to
// call it only when we think it will give consistent results.

if releasing {
// Don't care about key-up events
return Some(0);
}

let handled_raw = Handled::new();
let raw_key_event = RawKeyEvent {
key: match phys_code {
Expand Down Expand Up @@ -1816,6 +1811,11 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<L
// these for ourselves in a couple of quick hash lookups.
let vk = wparam as u32;

if releasing && inner.dead_pending.is_some() {
// Don't care about key-up events while processing dead keys
return Some(0);
}

// If we previously had the start of a dead key...
let dead = if let Some(leader) = inner.dead_pending.take() {
inner
Expand Down Expand Up @@ -1874,6 +1874,11 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<L
ResolvedDeadKey::InvalidDeadKey => None,
}
} else if let Some(c) = inner.keyboard_info.is_dead_key_leader(modifiers, vk) {
if releasing {
// Don't care about key-up events while processing dead keys
return Some(0);
}

// They pressed a dead key.
// If they want dead key processing, then record that and
// wait for a subsequent keypress.
Expand Down

0 comments on commit 27d452a

Please sign in to comment.