From 71f1f3728d22b6fa59549316e79c3d78390b205f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antal=20Szab=C3=B3?= Date: Mon, 3 Nov 2025 18:29:28 +0100 Subject: [PATCH] windows: Remove null terminator from keyboard ID (#41785) Closes #41486, closes #35862 It is unnecessary, and it broke the `uses_altgr` function. Also add Slovenian layout as using AltGr. This should fix: - https://github.com/zed-industries/zed/pull/40536#issuecomment-3477121224 - https://github.com/zed-industries/zed/issues/41486 - https://github.com/zed-industries/zed/issues/35862 As the current strategy relies on manually adding layouts that have AltGr, it's brittle and not very elegant. It also has other issues (it requests the current layout on every kesytroke and mouse movement). **A potentially better and more comprehensive solution is at https://github.com/zed-industries/zed/pull/41259** This is just to fix the immediate issues while that gets reviewed. Release Notes: - windows: Fix AltGr handling on non-US layouts again. --- crates/gpui/src/platform/windows/keyboard.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/gpui/src/platform/windows/keyboard.rs b/crates/gpui/src/platform/windows/keyboard.rs index 7a8478d5910d35fb98a913ed799f2fa1447e9a65..cd0c1da10561d7bfafafbc70989344826e8e5b16 100644 --- a/crates/gpui/src/platform/windows/keyboard.rs +++ b/crates/gpui/src/platform/windows/keyboard.rs @@ -9,7 +9,6 @@ use windows::Win32::UI::{ }, WindowsAndMessaging::KL_NAMELENGTH, }; -use windows_core::HSTRING; use crate::{ KeybindingKeystroke, Keystroke, Modifiers, PlatformKeyboardLayout, PlatformKeyboardMapper, @@ -93,14 +92,13 @@ impl PlatformKeyboardMapper for WindowsKeyboardMapper { impl WindowsKeyboardLayout { pub(crate) fn new() -> Result { - let mut buffer = [0u16; KL_NAMELENGTH as usize]; + let mut buffer = [0u16; KL_NAMELENGTH as usize]; // KL_NAMELENGTH includes the null terminator unsafe { GetKeyboardLayoutNameW(&mut buffer)? }; - let id = HSTRING::from_wide(&buffer).to_string(); + let id = String::from_utf16_lossy(&buffer[..buffer.len() - 1]); // Remove the null terminator let entry = windows_registry::LOCAL_MACHINE.open(format!( - "System\\CurrentControlSet\\Control\\Keyboard Layouts\\{}", - id + "System\\CurrentControlSet\\Control\\Keyboard Layouts\\{id}" ))?; - let name = entry.get_hstring("Layout Text")?.to_string(); + let name = entry.get_string("Layout Text")?; Ok(Self { id, name }) } @@ -135,6 +133,7 @@ impl WindowsKeyboardLayout { b"0405" | // Czech b"040E" | // Hungarian b"0424" | // Slovenian + b"041A" | // Croatian b"041B" | // Slovak b"0418" // Romanian )