diff --git a/winit-win32/src/keyboard_layout.rs b/winit-win32/src/keyboard_layout.rs index 7b6a370fdb..368737d8c3 100644 --- a/winit-win32/src/keyboard_layout.rs +++ b/winit-win32/src/keyboard_layout.rs @@ -986,3 +986,47 @@ fn vkey_to_non_char_key( _ => Key::Unidentified(native_code), } } + +/// Query the logical key that would be produced by a physical key under the +/// current keyboard layout with the given modifier state. +/// +/// # Example +/// +/// ```ignore +/// use winit::keyboard::{KeyCode, ModifiersState}; +/// use winit_win32::keycode_to_key; +/// +/// // Get what the 'Q' key produces without modifiers +/// let key = keycode_to_key(KeyCode::KeyQ, ModifiersState::empty(), false, false); +/// +/// // Get what Shift+Q produces +/// let shifted = keycode_to_key(KeyCode::KeyQ, ModifiersState::SHIFT, false, false); +/// ``` +pub fn keycode_to_key( + keycode: KeyCode, + modifiers: ModifiersState, + caps_lock: bool, + num_lock: bool, +) -> Key { + let mut cache = LAYOUT_CACHE.lock().unwrap(); + let (locale_id, layout) = cache.get_current_layout(); + + let mut win_mods = WindowsModifiers::empty(); + if modifiers.shift_key() { + win_mods.insert(WindowsModifiers::SHIFT); + } + if modifiers.control_key() { + win_mods.insert(WindowsModifiers::CONTROL); + } + if modifiers.alt_key() { + win_mods.insert(WindowsModifiers::ALT); + } + if caps_lock { + win_mods.insert(WindowsModifiers::CAPS_LOCK); + } + + let vkey = keycode_to_vkey(keycode, locale_id); + let physical_key = PhysicalKey::Code(keycode); + + layout.get_key(win_mods, num_lock, vkey, &physical_key) +} diff --git a/winit-win32/src/lib.rs b/winit-win32/src/lib.rs index 569d1f32c1..a5619206e2 100644 --- a/winit-win32/src/lib.rs +++ b/winit-win32/src/lib.rs @@ -37,6 +37,7 @@ use winit_core::window::{PlatformWindowAttributes, Window as CoreWindow}; pub use self::event_loop::{EventLoop, PlatformSpecificEventLoopAttributes}; use self::icon::{RaiiIcon, SelectedCursor}; pub use self::keyboard::{physicalkey_to_scancode, scancode_to_physicalkey}; +pub use self::keyboard_layout::keycode_to_key; pub use self::monitor::{MonitorHandle, VideoModeHandle}; pub use self::window::Window;