diff --git a/src/lib.rs b/src/lib.rs index 24704b9..1202f1b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use winapi::um::winuser::GetKeyNameTextW; use std::ffi::OsString; -use wio::wide::FromWide; +use wio::wide::{FromWide, ToWide}; pub mod logger; @@ -9,6 +9,8 @@ pub fn scan_code_to_key_name(scan_code: u32) -> String { unsafe { let mut out = [0u16; 128]; GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 128); - OsString::from_wide(&out).to_str().unwrap().replace('\0', "") + let null_pos = out.iter().position(|x| *x == b'\0' as u16).unwrap_or_else(|| out.len()); + //use to_string_lossy to avoid unicode checks for better performance. if the windows api screws up thats not my fault :P + OsString::from_wide(&out[..null_pos]).to_string_lossy().into() } } \ No newline at end of file