use crate::errors::KeyLayoutError;
use std::convert::TryInto;
pub struct KeyLayout {}
impl KeyLayout {
pub fn new_from_window(_window: &winit::window::Window) -> Result<KeyLayout, KeyLayoutError> {
Ok(KeyLayout {})
}
#[allow(unsafe_code)]
pub fn get_key_as_string(&self, scancode: u32) -> String {
let win_vk = unsafe {
winapi::um::winuser::MapVirtualKeyW(scancode, winapi::um::winuser::MAPVK_VSC_TO_VK_EX)
};
let char_key = unsafe {
winapi::um::winuser::MapVirtualKeyW(win_vk, winapi::um::winuser::MAPVK_VK_TO_CHAR)
};
if char_key != 0 {
let mut output = String::from_utf16_lossy(&[char_key as u16]);
output = output.replace(|c: char| c.is_control(), "");
if !output.trim().is_empty() {
return output;
}
}
let mut l_param: i32 = (scancode.clone()).try_into().unwrap();
l_param <<= 16;
if (scancode & 0x0000FF00) == 0xE000 {
l_param |= 0b01 << 24;
}
let mut utf_key: [u16; 32] = [0; 32];
let output_size =
unsafe { winapi::um::winuser::GetKeyNameTextW(l_param, utf_key.as_mut_ptr(), 32) };
let (utf_key, _) = utf_key.split_at(output_size.try_into().unwrap());
String::from_utf16_lossy(utf_key)
}
pub fn new() -> Result<KeyLayout, KeyLayoutError> {
Ok(KeyLayout {})
}
}