Merge pull request #560 from dylanede/master

Modified Windows key event handling to support modifier keys.
This commit is contained in:
tomaka 2015-08-26 16:43:50 +02:00
commit 66317f5924
2 changed files with 23 additions and 10 deletions

View file

@ -129,8 +129,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
winapi::WM_KEYDOWN => { winapi::WM_KEYDOWN => {
use events::Event::KeyboardInput; use events::Event::KeyboardInput;
use events::ElementState::Pressed; use events::ElementState::Pressed;
let scancode = ((lparam >> 16) & 0xff) as u8; let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
let vkey = event::vkeycode_to_element(wparam);
send_event(window, KeyboardInput(Pressed, scancode, vkey)); send_event(window, KeyboardInput(Pressed, scancode, vkey));
0 0
}, },
@ -138,8 +137,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
winapi::WM_KEYUP => { winapi::WM_KEYUP => {
use events::Event::KeyboardInput; use events::Event::KeyboardInput;
use events::ElementState::Released; use events::ElementState::Released;
let scancode = ((lparam >> 16) & 0xff) as u8; let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
let vkey = event::vkeycode_to_element(wparam);
send_event(window, KeyboardInput(Released, scancode, vkey)); send_event(window, KeyboardInput(Released, scancode, vkey));
0 0
}, },

View file

@ -1,8 +1,20 @@
use events::VirtualKeyCode; use events::VirtualKeyCode;
use winapi; use winapi;
use user32;
use ScanCode;
pub fn vkeycode_to_element(code: winapi::WPARAM) -> Option<VirtualKeyCode> { const MAPVK_VSC_TO_VK_EX: u32 = 3;
match code as i32 {
pub fn vkeycode_to_element(wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> (ScanCode, Option<VirtualKeyCode>) {
let scancode = ((lparam >> 16) & 0xff) as u8;
let extended = (lparam & 0x01000000) != 0;
let vk = match wparam as i32 {
winapi::VK_SHIFT => unsafe { user32::MapVirtualKeyA(scancode as u32, MAPVK_VSC_TO_VK_EX) as i32 },
winapi::VK_CONTROL => if extended { winapi::VK_RCONTROL } else { winapi::VK_LCONTROL },
winapi::VK_MENU => if extended { winapi::VK_RMENU } else { winapi::VK_LMENU },
other => other
};
(scancode, match vk {
//winapi::VK_LBUTTON => Some(VirtualKeyCode::Lbutton), //winapi::VK_LBUTTON => Some(VirtualKeyCode::Lbutton),
//winapi::VK_RBUTTON => Some(VirtualKeyCode::Rbutton), //winapi::VK_RBUTTON => Some(VirtualKeyCode::Rbutton),
//winapi::VK_CANCEL => Some(VirtualKeyCode::Cancel), //winapi::VK_CANCEL => Some(VirtualKeyCode::Cancel),
@ -13,9 +25,12 @@ pub fn vkeycode_to_element(code: winapi::WPARAM) -> Option<VirtualKeyCode> {
winapi::VK_TAB => Some(VirtualKeyCode::Tab), winapi::VK_TAB => Some(VirtualKeyCode::Tab),
//winapi::VK_CLEAR => Some(VirtualKeyCode::Clear), //winapi::VK_CLEAR => Some(VirtualKeyCode::Clear),
winapi::VK_RETURN => Some(VirtualKeyCode::Return), winapi::VK_RETURN => Some(VirtualKeyCode::Return),
//winapi::VK_SHIFT => Some(VirtualKeyCode::Shift), winapi::VK_LSHIFT => Some(VirtualKeyCode::LShift),
//winapi::VK_CONTROL => Some(VirtualKeyCode::Control), winapi::VK_RSHIFT => Some(VirtualKeyCode::RShift),
//winapi::VK_MENU => Some(VirtualKeyCode::Menu), winapi::VK_LCONTROL => Some(VirtualKeyCode::LControl),
winapi::VK_RCONTROL => Some(VirtualKeyCode::RControl),
winapi::VK_LMENU => Some(VirtualKeyCode::LMenu),
winapi::VK_RMENU => Some(VirtualKeyCode::RMenu),
winapi::VK_PAUSE => Some(VirtualKeyCode::Pause), winapi::VK_PAUSE => Some(VirtualKeyCode::Pause),
winapi::VK_CAPITAL => Some(VirtualKeyCode::Capital), winapi::VK_CAPITAL => Some(VirtualKeyCode::Capital),
winapi::VK_KANA => Some(VirtualKeyCode::Kana), winapi::VK_KANA => Some(VirtualKeyCode::Kana),
@ -177,5 +192,5 @@ pub fn vkeycode_to_element(code: winapi::WPARAM) -> Option<VirtualKeyCode> {
winapi::VK_PA1 => Some(VirtualKeyCode::Pa1), winapi::VK_PA1 => Some(VirtualKeyCode::Pa1),
winapi::VK_OEM_CLEAR => Some(VirtualKeyCode::Oem_clear),*/ winapi::VK_OEM_CLEAR => Some(VirtualKeyCode::Oem_clear),*/
_ => None _ => None
} })
} }