1use anyhow::Result;
2use windows::Win32::UI::{
3 Input::KeyboardAndMouse::GetKeyboardLayoutNameW, WindowsAndMessaging::KL_NAMELENGTH,
4};
5use windows_core::HSTRING;
6
7use crate::PlatformKeyboardLayout;
8
9pub(crate) struct WindowsKeyboardLayout {
10 id: String,
11 name: String,
12}
13
14impl PlatformKeyboardLayout for WindowsKeyboardLayout {
15 fn id(&self) -> &str {
16 &self.id
17 }
18
19 fn name(&self) -> &str {
20 &self.name
21 }
22}
23
24impl WindowsKeyboardLayout {
25 pub(crate) fn new() -> Result<Self> {
26 let mut buffer = [0u16; KL_NAMELENGTH as usize];
27 unsafe { GetKeyboardLayoutNameW(&mut buffer)? };
28 let id = HSTRING::from_wide(&buffer).to_string();
29 let entry = windows_registry::LOCAL_MACHINE.open(format!(
30 "System\\CurrentControlSet\\Control\\Keyboard Layouts\\{}",
31 id
32 ))?;
33 let name = entry.get_hstring("Layout Text")?.to_string();
34 Ok(Self { id, name })
35 }
36
37 pub(crate) fn unknown() -> Self {
38 Self {
39 id: "unknown".to_string(),
40 name: "unknown".to_string(),
41 }
42 }
43}