From 924c77b494d4960cf67580a0bebc880dd1fd6e13 Mon Sep 17 00:00:00 2001 From: austin62jake <340374338+austin62jake@users.noreply.github.com> Date: Mon, 2 Nov 2026 23:35:05 -0600 Subject: [PATCH 1/4] W36: Windows: an Alt code types its character MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Holding Alt and typing a number on the number pad (Alt+0181 for µ) makes Windows send the character in WM_CHAR after Alt's key-up. The keyboard builder adds a WM_CHAR only to a pending key press, so after a release it took the character for IME text and dropped it: Alt codes typed nothing in any text field (storytold/pdfcraft#5746). When Alt's release is followed by WM_CHAR or WM_SYSCHAR, the character now goes on a press of its own, reported like a character injected with VK_PACKET: an unidentified key whose text and logical key are the character. A release follows the press at once, so the key isn't left down. A character after any other key's release is still left to the input method. Tested with a message-only window: Alt's release then the queued µ give the Alt release, then a press with text "µ" and its release; without the fix the µ gives no event. A character after Enter's release still gives none. Co-Authored-By: Claude Opus 5.5 --- src/platform_impl/windows/keyboard.rs | 155 +++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 1 deletion(-) diff --git a/src/platform_impl/windows/keyboard.rs b/src/platform_impl/windows/keyboard.rs index ec12056a..3e06a95d 100644 --- a/src/platform_impl/windows/keyboard.rs +++ b/src/platform_impl/windows/keyboard.rs @@ -61,7 +61,9 @@ pub struct MessageAsKeyEvent { /// the key. /// /// Key release messages are a bit different due to the fact that they don't contribute to -/// text input. The "sequence" only consists of one WM_KEYUP / WM_SYSKEYUP event. +/// text input. The "sequence" only consists of one WM_KEYUP / WM_SYSKEYUP event. The exception +/// is Alt's release ending an Alt code, which the code's WM_CHAR follows; that character is +/// reported as a press and a release of its own (`PartialKeyEventInfo::alt_code`). pub struct KeyEventBuilder { event_info: Mutex>, pending: PendingEventQueue, @@ -260,7 +262,34 @@ impl KeyEventBuilder { let key = layout.get_key(mod_no_ctrl, num_lock_on, vkey, physical_key); event_info.text = PartialText::Text(key.to_text().map(SmolStr::new)); } + let alt_code = event_info.alt_code; let ev = event_info.finalize(); + if alt_code { + // No key was pressed for the character, so none is left down: its + // release follows the press. `pending_token` is earlier and still + // incomplete, so completing the release dispatches nothing yet. + let release = KeyEvent { + state: ElementState::Released, + text: None, + platform_specific: KeyEventExtra { + text_with_all_modifiers: None, + key_without_modifiers: ev + .platform_specific + .key_without_modifiers + .clone(), + }, + ..ev.clone() + }; + let release_token = self.pending.add_pending(); + let early = self.pending.complete_pending( + release_token, + MessageAsKeyEvent { event: release, is_synthetic: false }, + ); + debug_assert!( + early.is_empty(), + "the release went out before its press" + ); + } MatchResult::MessagesToDispatch(self.pending.complete_pending( pending_token, MessageAsKeyEvent { event: ev, is_synthetic: false }, @@ -284,6 +313,16 @@ impl KeyEventBuilder { // It's important that we create the pending token before reading the next // message. let next_msg = next_kbd_msg(hwnd); + // An Alt code types its character when Alt is released: the `WM_CHAR` + // follows Alt's key-up, and with no press for the `WM_CHAR` branch to add it + // to, it was dropped as IME text. It goes on a press of its own instead + // (storytold/pdfcraft#5746). + let alt_released = matches!(event_info.vkey, VK_MENU | VK_LMENU | VK_RMENU); + if alt_released + && next_msg.is_some_and(|m| matches!(m.message, WM_CHAR | WM_SYSCHAR)) + { + *self.event_info.lock().unwrap() = Some(PartialKeyEventInfo::alt_code()); + } let mut valid_event_info = Some(event_info); if let Some(next_msg) = next_msg { let mut layouts = LAYOUT_CACHE.lock().unwrap(); @@ -457,6 +496,7 @@ impl KeyEventBuilder { location: get_location(scancode, locale_id), utf16parts: Vec::with_capacity(8), text: PartialText::Text(text.clone()), + alt_code: false, }; let mut event = event_info.finalize(); @@ -498,9 +538,32 @@ struct PartialKeyEventInfo { utf16parts: Vec, text: PartialText, + + /// The press an Alt code's character is reported on ([`Self::alt_code`]), which a release + /// follows at once. + alt_code: bool, } impl PartialKeyEventInfo { + /// The press that carries an Alt code's character. Holding Alt and typing a number on the + /// number pad (Alt+0181 for µ) makes Windows send the character in `WM_CHAR` after Alt is + /// released, with no key press of its own. It is reported like a character injected with + /// `VK_PACKET`: a press of an unidentified key, whose text and logical key are the character. + fn alt_code() -> Self { + PartialKeyEventInfo { + vkey: 0, + key_state: ElementState::Pressed, + is_repeat: false, + physical_key: PhysicalKey::Unidentified(NativeKeyCode::Unidentified), + location: KeyLocation::Standard, + logical_key: PartialLogicalKey::TextOr(Key::Unidentified(NativeKey::Unidentified)), + key_without_modifiers: Key::Unidentified(NativeKey::Unidentified), + utf16parts: Vec::with_capacity(8), + text: PartialText::System(Vec::new()), + alt_code: true, + } + } + fn from_message( wparam: WPARAM, lparam: LPARAM, @@ -590,6 +653,7 @@ impl PartialKeyEventInfo { location, utf16parts: Vec::with_capacity(8), text: PartialText::System(Vec::new()), + alt_code: false, } } @@ -1250,3 +1314,92 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { _ => return PhysicalKey::Unidentified(NativeKeyCode::Windows(scancode as u16)), }) } + +#[cfg(test)] +mod alt_code_tests { + use std::{mem, ptr}; + + use windows_sys::Win32::Foundation::{HWND, LPARAM, WPARAM}; + use windows_sys::Win32::UI::Input::KeyboardAndMouse::{VK_MENU, VK_RETURN}; + use windows_sys::Win32::UI::WindowsAndMessaging::{ + CreateWindowExW, DestroyWindow, PeekMessageW, PostMessageW, HWND_MESSAGE, MSG, PM_REMOVE, + WM_CHAR, WM_KEYUP, + }; + + use super::{KeyEventBuilder, MessageAsKeyEvent}; + use crate::event::ElementState; + use crate::keyboard::{Key, NamedKey}; + use crate::platform_impl::platform::event_loop::ProcResult; + use crate::platform_impl::platform::util; + + /// The lParam of a key's release: repeat count 1, the key's scan code, previous state down, + /// transition up. + fn released(scancode: u32) -> LPARAM { + (1 | scancode << 16 | 1 << 30 | 1 << 31) as LPARAM + } + + /// A key release, then the `WM_CHAR` Windows queues after it, through one builder: the events + /// for the release and for the character. + fn release_then_char( + vkey: u16, + scancode: u32, + ch: char, + ) -> (Vec, Vec) { + let class: Vec = "STATIC\0".encode_utf16().collect(); + let window: HWND = unsafe { + CreateWindowExW( + 0, + class.as_ptr(), + ptr::null(), + 0, + 0, + 0, + 0, + 0, + HWND_MESSAGE, + 0, + util::get_instance_handle(), + ptr::null(), + ) + }; + assert_ne!(window, 0); + let lparam = released(scancode); + assert_ne!(unsafe { PostMessageW(window, WM_CHAR, ch as WPARAM, lparam) }, 0); + let builder = KeyEventBuilder::default(); + let mut result = ProcResult::DefWindowProc(0); + let release = + builder.process_message(window, WM_KEYUP, vkey as WPARAM, lparam, &mut result); + let mut msg: MSG = unsafe { mem::zeroed() }; + assert_ne!(unsafe { PeekMessageW(&mut msg, window, WM_CHAR, WM_CHAR, PM_REMOVE) }, 0); + let typed = + builder.process_message(window, msg.message, msg.wParam, msg.lParam, &mut result); + unsafe { DestroyWindow(window) }; + (release, typed) + } + + #[test] + fn an_alt_code_types_its_character() { + // Alt+0181: Windows sends µ after Alt's release (storytold/pdfcraft#5746). + let (release, typed) = release_then_char(VK_MENU, 0x38, 'µ'); + let [alt] = release.as_slice() else { + panic!("{} events for Alt's release", release.len()) + }; + assert_eq!(alt.event.logical_key, Key::Named(NamedKey::Alt)); + assert_eq!(alt.event.state, ElementState::Released); + let [press, up] = typed.as_slice() else { panic!("{} events for µ", typed.len()) }; + assert_eq!(press.event.state, ElementState::Pressed); + assert_eq!(press.event.text.as_deref(), Some("µ")); + assert_eq!(press.event.logical_key, Key::Character("µ".into())); + assert_eq!(up.event.state, ElementState::Released); + assert_eq!(up.event.text, None); + assert_eq!(up.event.physical_key, press.event.physical_key); + } + + #[test] + fn a_character_after_another_release_is_still_left_to_the_ime() { + // Only Alt's release ends an Alt code; a character queued after any other release is + // input method text, which reaches the app as `Ime` events. + let (_, typed) = release_then_char(VK_RETURN, 0x1c, 'x'); + assert!(typed.is_empty(), "{} events for x", typed.len()); + } +} From f502d3b1d9ebc13c20e4795781d6f47b6957751e Mon Sep 17 00:00:00 2001 From: austin62jake <340374338+austin62jake@users.noreply.github.com> Date: Mon, 2 Nov 2026 23:35:18 -0600 Subject: [PATCH 2/4] W36: PATCHES.md row with its commit and pdfcraft#5746 Co-Authored-By: Claude Opus 5.5 --- PATCHES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/PATCHES.md b/PATCHES.md index c100c83d..f94544c1 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -34,6 +34,7 @@ input lives in `craft-tablet`). | W25 | `eb4007da` | X11/Wayland: `platform::latin_layout::KeyEventExtLatinLayout`, the key on the first Latin layout group for shortcuts on non-Latin layouts | none; to be proposed | upstream release with an equivalent | storytold/winit PR #5; craft-libs#198 (K2) | | W26 | `2b9cd1cf` | macOS: replaced text is deleted by UTF-8 byte count with the commit (`WindowExtMacOS::take_ime_delete_surrounding`, same shape as W27) | master: `Ime::DeleteSurrounding` (a new `Ime` variant breaks 0.30 matchers) | the egui → 0.31 move | storytold/winit PR #6 (`d103edae`) | | W27 | `f40c76a2` | Wayland: text-input-v3 surrounding text (`WindowExtWayland::set_ime_surrounding_text`, `set_ime_surrounding_text_with_change_cause`, `clear_ime_surrounding_text`), delete-surrounding attached to the commit (`take_ime_delete_surrounding` → `ImeDeleteSurrounding { before_bytes, after_bytes }`), per-seat preedit ownership and outgoing state waiting for a matching `done` (E21b) | master has both as core API (`Ime::DeleteSurrounding`) | the egui → 0.31 move | storytold/winit PR #7; craft-libs#198 (I7) | +| W36 | `924c77b4` | Windows: an Alt code (Alt+0181 for µ) types its character; Windows sends it in `WM_CHAR` after Alt's key-up, which the keyboard builder dropped as IME text. It goes on a press of an unidentified key (as `VK_PACKET` is reported) with a release after it | none found; to be proposed | upstream release with an equivalent | pdfcraft#5746 | Conflict resolved by hand: `wayland/seat/mod.rs` `remove_seat` keeps all three cleanups — cancel a file drag (W15), remove the seat from winit's clipboard (W15), destroy the seat's text input (W2). From ecba8dd5c6f71ddbeff0fd57a6775e075bfd29b8 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 27 Nov 2026 14:04:34 +0100 Subject: [PATCH 3/4] W36: test an Alt code whose Alt release arrives as WM_SYSKEYUP The release branch handles WM_KEYUP and WM_SYSKEYUP alike; the test now pins both. Co-Authored-By: Claude Opus 5.5 --- src/platform_impl/windows/keyboard.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/platform_impl/windows/keyboard.rs b/src/platform_impl/windows/keyboard.rs index 3e06a95d..e695ea2d 100644 --- a/src/platform_impl/windows/keyboard.rs +++ b/src/platform_impl/windows/keyboard.rs @@ -1323,7 +1323,7 @@ mod alt_code_tests { use windows_sys::Win32::UI::Input::KeyboardAndMouse::{VK_MENU, VK_RETURN}; use windows_sys::Win32::UI::WindowsAndMessaging::{ CreateWindowExW, DestroyWindow, PeekMessageW, PostMessageW, HWND_MESSAGE, MSG, PM_REMOVE, - WM_CHAR, WM_KEYUP, + WM_CHAR, WM_KEYUP, WM_SYSKEYUP, }; use super::{KeyEventBuilder, MessageAsKeyEvent}; @@ -1338,9 +1338,10 @@ mod alt_code_tests { (1 | scancode << 16 | 1 << 30 | 1 << 31) as LPARAM } - /// A key release, then the `WM_CHAR` Windows queues after it, through one builder: the events - /// for the release and for the character. + /// A key release (`WM_KEYUP` or `WM_SYSKEYUP`), then the `WM_CHAR` Windows queues after it, + /// through one builder: the events for the release and for the character. fn release_then_char( + up_msg: u32, vkey: u16, scancode: u32, ch: char, @@ -1367,8 +1368,7 @@ mod alt_code_tests { assert_ne!(unsafe { PostMessageW(window, WM_CHAR, ch as WPARAM, lparam) }, 0); let builder = KeyEventBuilder::default(); let mut result = ProcResult::DefWindowProc(0); - let release = - builder.process_message(window, WM_KEYUP, vkey as WPARAM, lparam, &mut result); + let release = builder.process_message(window, up_msg, vkey as WPARAM, lparam, &mut result); let mut msg: MSG = unsafe { mem::zeroed() }; assert_ne!(unsafe { PeekMessageW(&mut msg, window, WM_CHAR, WM_CHAR, PM_REMOVE) }, 0); let typed = @@ -1380,7 +1380,17 @@ mod alt_code_tests { #[test] fn an_alt_code_types_its_character() { // Alt+0181: Windows sends µ after Alt's release (storytold/pdfcraft#5746). - let (release, typed) = release_then_char(VK_MENU, 0x38, 'µ'); + assert_alt_code_types(WM_KEYUP); + } + + #[test] + fn an_alt_code_types_its_character_after_a_syskeyup() { + // Alt's release can also arrive as `WM_SYSKEYUP`; the builder treats both the same. + assert_alt_code_types(WM_SYSKEYUP); + } + + fn assert_alt_code_types(up_msg: u32) { + let (release, typed) = release_then_char(up_msg, VK_MENU, 0x38, 'µ'); let [alt] = release.as_slice() else { panic!("{} events for Alt's release", release.len()) }; @@ -1399,7 +1409,7 @@ mod alt_code_tests { fn a_character_after_another_release_is_still_left_to_the_ime() { // Only Alt's release ends an Alt code; a character queued after any other release is // input method text, which reaches the app as `Ime` events. - let (_, typed) = release_then_char(VK_RETURN, 0x1c, 'x'); + let (_, typed) = release_then_char(WM_KEYUP, VK_RETURN, 0x1c, 'x'); assert!(typed.is_empty(), "{} events for x", typed.len()); } } From 4e6436c5f3b6e8b11ff153a9fca7d12edc86335a Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 27 Nov 2026 14:08:19 +0100 Subject: [PATCH 4/4] W36: rustfmt (nightly) on the Alt-code release Co-Authored-By: Claude Opus 5.5 --- src/platform_impl/windows/keyboard.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/platform_impl/windows/keyboard.rs b/src/platform_impl/windows/keyboard.rs index e695ea2d..9b81f594 100644 --- a/src/platform_impl/windows/keyboard.rs +++ b/src/platform_impl/windows/keyboard.rs @@ -281,10 +281,11 @@ impl KeyEventBuilder { ..ev.clone() }; let release_token = self.pending.add_pending(); - let early = self.pending.complete_pending( - release_token, - MessageAsKeyEvent { event: release, is_synthetic: false }, - ); + let early = + self.pending.complete_pending(release_token, MessageAsKeyEvent { + event: release, + is_synthetic: false, + }); debug_assert!( early.is_empty(), "the release went out before its press"