lifecycle: detach IME via ImmAssociateContextEx on title windows for CJK Windows

This commit is contained in:
susu
2026-12-06 03:44:17 +08:00
parent deb2a0d897
commit 24699cf8b6

View File

@ -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<bool> = 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;
}