From 32eea41b1b5b19d97373d4e53ae0f5773c168fcc Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Mon, 24 Jul 2017 20:21:04 +1000 Subject: [PATCH] add missing windows virtual key codes --- src/platform/windows/event.rs | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/platform/windows/event.rs b/src/platform/windows/event.rs index 671e6347..b37fc252 100644 --- a/src/platform/windows/event.rs +++ b/src/platform/windows/event.rs @@ -3,7 +3,9 @@ use events::ModifiersState; use winapi; use user32; use ScanCode; +use std::char; +const MAPVK_VK_TO_CHAR: u32 = 2; const MAPVK_VSC_TO_VK_EX: u32 = 3; pub fn get_key_mods() -> ModifiersState { @@ -34,6 +36,8 @@ pub fn vkeycode_to_element(wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> (S winapi::VK_MENU => if extended { winapi::VK_RMENU } else { winapi::VK_LMENU }, other => other }; + + // VK_* codes are documented here https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx (scancode, match vk { //winapi::VK_LBUTTON => Some(VirtualKeyCode::Lbutton), //winapi::VK_RBUTTON => Some(VirtualKeyCode::Rbutton), @@ -181,18 +185,18 @@ pub fn vkeycode_to_element(wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> (S winapi::VK_LAUNCH_MEDIA_SELECT => Some(VirtualKeyCode::MediaSelect), /*winapi::VK_LAUNCH_APP1 => Some(VirtualKeyCode::Launch_app1), winapi::VK_LAUNCH_APP2 => Some(VirtualKeyCode::Launch_app2),*/ - winapi::VK_OEM_PLUS => Some(VirtualKeyCode::Equals), + winapi::VK_OEM_PLUS => Some(VirtualKeyCode::Equals), winapi::VK_OEM_COMMA => Some(VirtualKeyCode::Comma), winapi::VK_OEM_MINUS => Some(VirtualKeyCode::Minus), winapi::VK_OEM_PERIOD => Some(VirtualKeyCode::Period), - /*winapi::VK_OEM_1 => Some(VirtualKeyCode::Oem_1), - winapi::VK_OEM_2 => Some(VirtualKeyCode::Oem_2), - winapi::VK_OEM_3 => Some(VirtualKeyCode::Oem_3), - winapi::VK_OEM_4 => Some(VirtualKeyCode::Oem_4), - winapi::VK_OEM_5 => Some(VirtualKeyCode::Oem_5), - winapi::VK_OEM_6 => Some(VirtualKeyCode::Oem_6), - winapi::VK_OEM_7 => Some(VirtualKeyCode::Oem_7), - winapi::VK_OEM_8 => Some(VirtualKeyCode::Oem_8), */ + winapi::VK_OEM_1 => map_text_keys(vk), + winapi::VK_OEM_2 => map_text_keys(vk), + winapi::VK_OEM_3 => map_text_keys(vk), + winapi::VK_OEM_4 => map_text_keys(vk), + winapi::VK_OEM_5 => map_text_keys(vk), + winapi::VK_OEM_6 => map_text_keys(vk), + winapi::VK_OEM_7 => map_text_keys(vk), + /*winapi::VK_OEM_8 => Some(VirtualKeyCode::Oem_8), */ winapi::VK_OEM_102 => Some(VirtualKeyCode::OEM102), /*winapi::VK_PROCESSKEY => Some(VirtualKeyCode::Processkey), winapi::VK_PACKET => Some(VirtualKeyCode::Packet), @@ -208,3 +212,19 @@ pub fn vkeycode_to_element(wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> (S _ => None }) } + +// This is needed as windows doesn't properly distinguish +// some virtual key codes for different keyboard layouts +fn map_text_keys(win_virtual_key: i32) -> Option { + let char_key = unsafe { user32::MapVirtualKeyA(win_virtual_key as u32, MAPVK_VK_TO_CHAR) } & 0x7FFF; + match char::from_u32(char_key) { + Some(';') => Some(VirtualKeyCode::Semicolon), + Some('/') => Some(VirtualKeyCode::Slash), + Some('`') => Some(VirtualKeyCode::Grave), + Some('[') => Some(VirtualKeyCode::LBracket), + Some(']') => Some(VirtualKeyCode::RBracket), + Some('\'') => Some(VirtualKeyCode::Apostrophe), + Some('\\') => Some(VirtualKeyCode::Backslash), + _ => None + } +}