Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing windows virtual key codes #241

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/platform/windows/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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<VirtualKeyCode> {
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
}
}