Skip to content
Draft
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions winit-win32/src/keyboard_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions winit-win32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading