Implement KeyboardEvent ModifiersState for win32

This commit is contained in:
Erik Rigtorp 2017-01-19 19:45:02 -06:00
parent a2c6fb720c
commit 5d41067a05
2 changed files with 23 additions and 3 deletions

View file

@ -8,7 +8,7 @@ use std::os::windows::ffi::OsStringExt;
use CursorState;
use WindowEvent as Event;
use events::ModifersState;
use events::ModifiersState;
use super::event;
use super::WindowState;
@ -201,7 +201,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
user32::DefWindowProcW(window, msg, wparam, lparam)
} else {
let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
send_event(window, KeyboardInput(Pressed, scancode, vkey, ModifersState::default()));
send_event(window, KeyboardInput(Pressed, scancode, vkey, event::get_key_mods()));
0
}
},
@ -210,7 +210,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
use events::WindowEvent::KeyboardInput;
use events::ElementState::Released;
let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
send_event(window, KeyboardInput(Released, scancode, vkey, ModifersState::default()));
send_event(window, KeyboardInput(Released, scancode, vkey, event::get_key_mods()));
0
},

View file

@ -1,10 +1,30 @@
use events::VirtualKeyCode;
use events::ModifiersState;
use winapi;
use user32;
use ScanCode;
const MAPVK_VSC_TO_VK_EX: u32 = 3;
pub fn get_key_mods() -> ModifiersState {
let mut mods = ModifiersState::default();
unsafe {
if user32::GetKeyState(winapi::VK_SHIFT) & (1 << 15) == (1 << 15) {
mods.shift = true;
}
if user32::GetKeyState(winapi::VK_CONTROL) & (1 << 15) == (1 << 15) {
mods.ctrl = true;
}
if user32::GetKeyState(winapi::VK_MENU) & (1 << 15) == (1 << 15) {
mods.alt = true;
}
if (user32::GetKeyState(winapi::VK_LWIN) | user32::GetKeyState(winapi::VK_RWIN)) & (1 << 15) == (1 << 15) {
mods.logo = true;
}
}
mods
}
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;