From 24699cf8b63c478aaebec793582ad24211362148 Mon Sep 17 00:00:00 2001 From: susu <204942126+860776819@users.noreply.github.com> Date: Sun, 6 Dec 2026 03:44:17 +0800 Subject: [PATCH] lifecycle: detach IME via ImmAssociateContextEx on title windows for CJK Windows --- crates/larp-hle/src/modules/lifecycle.rs | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/larp-hle/src/modules/lifecycle.rs b/crates/larp-hle/src/modules/lifecycle.rs index 731e773..400e7ee 100644 --- a/crates/larp-hle/src/modules/lifecycle.rs +++ b/crates/larp-hle/src/modules/lifecycle.rs @@ -161,6 +161,29 @@ fn hook(hwnd: HWND) { wm::SetWindowLongPtrW(hwnd, wm::GWLP_WNDPROC, window_proc as *const () as isize); } super::theme::apply(hwnd); + detach_ime(hwnd); +} + +/// Detaches the Windows IME from `hwnd` so CJK input methods (MS Pinyin, +/// Sogou, etc.) never pop up candidate boxes or intercept WASD/Shift keys +/// during gameplay. Opt out with `LARP_IME_DETACH=0`. +fn detach_ime(hwnd: HWND) { + static ENABLED: std::sync::OnceLock = std::sync::OnceLock::new(); + let enabled = *ENABLED.get_or_init(|| { + std::env::var("LARP_IME_DETACH").ok().as_deref().map(str::trim) != Some("0") + }); + if !enabled || hwnd.is_null() { + return; + } + #[link(name = "imm32")] + unsafe extern "system" { + fn ImmAssociateContextEx(hwnd: HWND, himc: *mut c_void, flags: u32) -> i32; + } + // SAFETY: `hwnd` is a valid window handle on this thread; passing a null + // HIMC with flags=0 detaches the IME only from this window. + unsafe { + let _ = ImmAssociateContextEx(hwnd, std::ptr::null_mut(), 0); + } } extern "system" fn window_proc( @@ -190,6 +213,9 @@ extern "system" fn window_proc( if message == wm::WM_SETTINGCHANGE { super::theme::apply_for_setting(hwnd, lparam); } + if message == wm::WM_SETFOCUS { + detach_ime(hwnd); + } if larp_gpu::alt_enter(hwnd, message, wparam, lparam) { return 0; }