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

Align NamedKey and KeyCode more closely with the W3C specs #4018

Merged
merged 5 commits into from
Mar 23, 2025
Merged
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
15 changes: 12 additions & 3 deletions examples/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,16 @@ fn modifiers_to_string(mods: ModifiersState) -> String {
let mut mods_line = String::new();
// Always add + since it's printed as a part of the bindings.
for (modifier, desc) in [
(ModifiersState::SUPER, "Super+"),
(
ModifiersState::META,
if cfg!(target_os = "windows") {
"Win+"
} else if cfg!(target_vendor = "apple") {
"Cmd+"
} else {
"Super+"
},
),
(ModifiersState::ALT, "Alt+"),
(ModifiersState::CONTROL, "Ctrl+"),
(ModifiersState::SHIFT, "Shift+"),
Expand Down Expand Up @@ -1264,10 +1273,10 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
Binding::new("Z", ModifiersState::CONTROL, Action::ToggleCursorVisibility),
// K.
Binding::new("K", ModifiersState::empty(), Action::SetTheme(None)),
Binding::new("K", ModifiersState::SUPER, Action::SetTheme(Some(Theme::Light))),
Binding::new("K", ModifiersState::META, Action::SetTheme(Some(Theme::Light))),
Binding::new("K", ModifiersState::CONTROL, Action::SetTheme(Some(Theme::Dark))),
#[cfg(macos_platform)]
Binding::new("T", ModifiersState::SUPER, Action::CreateNewTab),
Binding::new("T", ModifiersState::META, Action::CreateNewTab),
#[cfg(macos_platform)]
Binding::new("O", ModifiersState::CONTROL, Action::CycleOptionAsAlt),
Binding::new("S", ModifiersState::CONTROL, Action::Message),
Expand Down
3 changes: 3 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ changelog entry.
- Removed `KeyEventExtModifierSupplement`, and made the fields `text_with_all_modifiers` and
`key_without_modifiers` public on `KeyEvent` instead.
- Move `window::Fullscreen` to `monitor::Fullscreen`.
- Renamed "super" key to "meta", to match the naming in the W3C specification.
`NamedKey::Super` still exists, but it's non-functional and deprecated, `NamedKey::Meta` should be used instead.

### Removed

Expand Down Expand Up @@ -230,6 +232,7 @@ changelog entry.
- Remove `CustomCursor::from_rgba`, use `CustomCursorSource` instead.
- Removed `ApplicationHandler::exited`, the event loop being shut down can now be listened to in
the `Drop` impl on the application handler.
- Removed `NamedKey::Space`, match on `Key::Character(" ")` instead.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,12 @@ impl Modifiers {

/// The state of the left super key.
pub fn lsuper_state(&self) -> ModifiersKeyState {
self.mod_state(ModifiersKeys::LSUPER)
self.mod_state(ModifiersKeys::LMETA)
}

/// The state of the right super key.
pub fn rsuper_state(&self) -> ModifiersKeyState {
self.mod_state(ModifiersKeys::RSUPER)
self.mod_state(ModifiersKeys::RMETA)
}

fn mod_state(&self, modifier: ModifiersKeys) -> ModifiersKeyState {
Expand Down
61 changes: 29 additions & 32 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Types related to the keyboard.
#![cfg_attr(feature = "serde", allow(deprecated))] // https://github.com/serde-rs/serde/issues/2195

// This file contains a substantial portion of the UI Events Specification by the W3C. In
// particular, the variant names within `Key` and `KeyCode` and their documentation are modified
Expand Down Expand Up @@ -283,11 +284,7 @@ impl PartialEq<PhysicalKey> for NativeKeyCode {

/// Code representing the location of a physical key
///
/// This mostly conforms to the UI Events Specification's [`KeyboardEvent.code`] with a few
/// exceptions:
/// - The keys that the specification calls "MetaLeft" and "MetaRight" are named "SuperLeft" and
/// "SuperRight" here.
/// - The key that the specification calls "Super" is reported as `Unidentified` here.
/// This conforms to the UI Events Specification's [`KeyboardEvent.code`].
///
/// [`KeyboardEvent.code`]: https://w3c.github.io/uievents-code/#code-value-tables
#[non_exhaustive]
Expand Down Expand Up @@ -420,7 +417,7 @@ pub enum KeyCode {
/// <kbd>CapsLock</kbd> or <kbd>⇪</kbd>
CapsLock,
/// The application context menu key, which is typically found between the right
/// <kbd>Super</kbd> key and the right <kbd>Control</kbd> key.
/// <kbd>Meta</kbd> key and the right <kbd>Control</kbd> key.
ContextMenu,
/// <kbd>Control</kbd> or <kbd>⌃</kbd>
ControlLeft,
Expand All @@ -429,9 +426,9 @@ pub enum KeyCode {
/// <kbd>Enter</kbd> or <kbd>↵</kbd>. Labeled <kbd>Return</kbd> on Apple keyboards.
Enter,
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
SuperLeft,
MetaLeft,
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
SuperRight,
MetaRight,
/// <kbd>Shift</kbd> or <kbd>⇧</kbd>
ShiftLeft,
/// <kbd>Shift</kbd> or <kbd>⇧</kbd>
Expand Down Expand Up @@ -613,9 +610,11 @@ pub enum KeyCode {
AudioVolumeMute,
AudioVolumeUp,
WakeUp,
// Legacy modifier key. Also called "Super" in certain places.
Meta,
// Legacy modifier key.
#[deprecated = "marked as legacy in the spec, use Meta instead"]
Super,
// Legacy modifier key.
#[deprecated = "marked as legacy in the spec, use Meta instead"]
Hyper,
Turbo,
Abort,
Expand Down Expand Up @@ -741,12 +740,7 @@ pub enum KeyCode {

/// A [`Key::Named`] value
///
/// This mostly conforms to the UI Events Specification's [`KeyboardEvent.key`] with a few
/// exceptions:
/// - The `Super` variant here, is named `Meta` in the aforementioned specification. (There's
/// another key which the specification calls `Super`. That does not exist here.)
/// - The `Space` variant here, can be identified by the character it generates in the
/// specification.
/// This conforms to the UI Events Specification's [`KeyboardEvent.key`].
///
/// [`KeyboardEvent.key`]: https://w3c.github.io/uievents-key/
#[non_exhaustive]
Expand Down Expand Up @@ -791,24 +785,22 @@ pub enum NamedKey {
/// The Symbol modifier key (used on some virtual keyboards).
Symbol,
SymbolLock,
// Legacy modifier key. Also called "Super" in certain places.
Meta,
// Legacy modifier key.
#[deprecated = "marked as legacy in the spec, use Meta instead"]
Super,
// Legacy modifier key.
#[deprecated = "marked as legacy in the spec, use Meta instead"]
Hyper,
/// Used to enable "super" modifier function for interpreting concurrent or subsequent keyboard
/// Used to enable "meta" modifier function for interpreting concurrent or subsequent keyboard
/// input. This key value is used for the "Windows Logo" key and the Apple `Command` or `⌘`
/// key.
///
/// Note: In some contexts (e.g. the Web) this is referred to as the "Meta" key.
Super,
Meta,
/// The `Enter` or `↵` key. Used to activate current selection or accept current input. This
/// key value is also used for the `Return` (Macintosh numpad) key. This key value is also
/// used for the Android `KEYCODE_DPAD_CENTER`.
Enter,
/// The Horizontal Tabulation `Tab` key.
Tab,
/// Used in text to insert a space between words. Usually located below the character keys.
Space,
/// Navigate or traverse downward. (`KEYCODE_DPAD_DOWN`)
ArrowDown,
/// Navigate or traverse leftward. (`KEYCODE_DPAD_LEFT`)
Expand Down Expand Up @@ -864,7 +856,7 @@ pub enum NamedKey {
Attn,
Cancel,
/// Show the application’s context menu.
/// This key is commonly found between the right `Super` key and the right `Control` key.
/// This key is commonly found between the right `Meta` key and the right `Control` key.
ContextMenu,
/// The `Esc` key. This key was originally used to initiate an escape sequence, but is
/// now more generally used to exit or "escape" the current context, such as closing a dialog
Expand Down Expand Up @@ -1583,7 +1575,6 @@ impl NamedKey {
NamedKey::Enter => Some("\r"),
NamedKey::Backspace => Some("\x08"),
NamedKey::Tab => Some("\t"),
NamedKey::Space => Some(" "),
NamedKey::Escape => Some("\x1b"),
_ => None,
}
Expand Down Expand Up @@ -1709,7 +1700,9 @@ bitflags! {
/// The "alt" key.
const ALT = 0b100 << 6;
/// This is the "windows" key on PC and "command" key on Mac.
const SUPER = 0b100 << 9;
const META = 0b100 << 9;
#[deprecated = "use META instead"]
const SUPER = Self::META.bits();
}
}

Expand All @@ -1729,9 +1722,9 @@ impl ModifiersState {
self.intersects(Self::ALT)
}

/// Returns `true` if the super key is pressed.
pub fn super_key(&self) -> bool {
self.intersects(Self::SUPER)
/// Returns `true` if the meta key is pressed.
pub fn meta_key(&self) -> bool {
self.intersects(Self::META)
}
}

Expand Down Expand Up @@ -1764,7 +1757,11 @@ bitflags! {
const RCONTROL = 0b0000_1000;
const LALT = 0b0001_0000;
const RALT = 0b0010_0000;
const LSUPER = 0b0100_0000;
const RSUPER = 0b1000_0000;
const LMETA = 0b0100_0000;
const RMETA = 0b1000_0000;
#[deprecated = "use LMETA instead"]
const LSUPER = Self::LMETA.bits();
#[deprecated = "use RMETA instead"]
const RSUPER = Self::RMETA.bits();
}
}
10 changes: 5 additions & 5 deletions src/platform_impl/android/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
Keycode::AltLeft => KeyCode::AltLeft,
Keycode::AltRight => KeyCode::AltRight,

Keycode::MetaLeft => KeyCode::SuperLeft,
Keycode::MetaRight => KeyCode::SuperRight,
Keycode::MetaLeft => KeyCode::MetaLeft,
Keycode::MetaRight => KeyCode::MetaRight,

Keycode::LeftBracket => KeyCode::BracketLeft,
Keycode::RightBracket => KeyCode::BracketRight,
Expand Down Expand Up @@ -309,7 +309,7 @@ pub fn to_logical(key_char: Option<KeyMapChar>, keycode: Keycode) -> Key {
ShiftLeft => Key::Named(NamedKey::Shift),
ShiftRight => Key::Named(NamedKey::Shift),
Tab => Key::Named(NamedKey::Tab),
Space => Key::Named(NamedKey::Space),
Space => Key::Character(" ".into()),
Sym => Key::Named(NamedKey::Symbol),
Explorer => Key::Named(NamedKey::LaunchWebBrowser),
Envelope => Key::Named(NamedKey::LaunchMail),
Expand Down Expand Up @@ -340,8 +340,8 @@ pub fn to_logical(key_char: Option<KeyMapChar>, keycode: Keycode) -> Key {
CtrlRight => Key::Named(NamedKey::Control),
CapsLock => Key::Named(NamedKey::CapsLock),
ScrollLock => Key::Named(NamedKey::ScrollLock),
MetaLeft => Key::Named(NamedKey::Super),
MetaRight => Key::Named(NamedKey::Super),
MetaLeft => Key::Named(NamedKey::Meta),
MetaRight => Key::Named(NamedKey::Meta),
Function => Key::Named(NamedKey::Fn),
Sysrq => Key::Named(NamedKey::PrintScreen),
Break => Key::Named(NamedKey::Pause),
Expand Down
24 changes: 12 additions & 12 deletions src/platform_impl/apple/appkit/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ pub fn code_to_key(key: PhysicalKey, scancode: u16) -> Key {
Key::Named(match code {
KeyCode::Enter => NamedKey::Enter,
KeyCode::Tab => NamedKey::Tab,
KeyCode::Space => NamedKey::Space,
KeyCode::Space => return Key::Character(" ".into()),
KeyCode::Backspace => NamedKey::Backspace,
KeyCode::Escape => NamedKey::Escape,
KeyCode::SuperRight => NamedKey::Super,
KeyCode::SuperLeft => NamedKey::Super,
KeyCode::MetaRight => NamedKey::Meta,
KeyCode::MetaLeft => NamedKey::Meta,
KeyCode::ShiftLeft => NamedKey::Shift,
KeyCode::AltLeft => NamedKey::Alt,
KeyCode::ControlLeft => NamedKey::Control,
Expand Down Expand Up @@ -242,8 +242,8 @@ pub fn code_to_location(key: PhysicalKey) -> KeyLocation {
};

match code {
KeyCode::SuperRight => KeyLocation::Right,
KeyCode::SuperLeft => KeyLocation::Left,
KeyCode::MetaRight => KeyLocation::Right,
KeyCode::MetaLeft => KeyLocation::Left,
KeyCode::ShiftLeft => KeyLocation::Left,
KeyCode::AltLeft => KeyLocation::Left,
KeyCode::ControlLeft => KeyLocation::Left,
Expand Down Expand Up @@ -326,9 +326,9 @@ pub(super) fn event_mods(event: &NSEvent) -> Modifiers {
pressed_mods.set(ModifiersKeys::LALT, flags.contains(NX_DEVICELALTKEYMASK));
pressed_mods.set(ModifiersKeys::RALT, flags.contains(NX_DEVICERALTKEYMASK));

state.set(ModifiersState::SUPER, flags.contains(NSEventModifierFlags::Command));
pressed_mods.set(ModifiersKeys::LSUPER, flags.contains(NX_DEVICELCMDKEYMASK));
pressed_mods.set(ModifiersKeys::RSUPER, flags.contains(NX_DEVICERCMDKEYMASK));
state.set(ModifiersState::META, flags.contains(NSEventModifierFlags::Command));
pressed_mods.set(ModifiersKeys::LMETA, flags.contains(NX_DEVICELCMDKEYMASK));
pressed_mods.set(ModifiersKeys::RMETA, flags.contains(NX_DEVICERCMDKEYMASK));

Modifiers { state, pressed_mods }
}
Expand Down Expand Up @@ -409,8 +409,8 @@ pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32>
KeyCode::Backquote => Some(0x32),
KeyCode::Backspace => Some(0x33),
KeyCode::Escape => Some(0x35),
KeyCode::SuperRight => Some(0x36),
KeyCode::SuperLeft => Some(0x37),
KeyCode::MetaRight => Some(0x36),
KeyCode::MetaLeft => Some(0x37),
KeyCode::ShiftLeft => Some(0x38),
KeyCode::CapsLock => Some(0x39),
KeyCode::AltLeft => Some(0x3a),
Expand Down Expand Up @@ -555,8 +555,8 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
0x33 => KeyCode::Backspace,
// 0x34 => unknown, // kVK_Powerbook_KeypadEnter
0x35 => KeyCode::Escape,
0x36 => KeyCode::SuperRight,
0x37 => KeyCode::SuperLeft,
0x36 => KeyCode::MetaRight,
0x37 => KeyCode::MetaLeft,
0x38 => KeyCode::ShiftLeft,
0x39 => KeyCode::CapsLock,
0x3a => KeyCode::AltLeft,
Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/apple/appkit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn key_to_modifier(key: &Key) -> Option<ModifiersState> {
match key {
Key::Named(NamedKey::Alt) => Some(ModifiersState::ALT),
Key::Named(NamedKey::Control) => Some(ModifiersState::CONTROL),
Key::Named(NamedKey::Super) => Some(ModifiersState::SUPER),
Key::Named(NamedKey::Meta) => Some(ModifiersState::META),
Key::Named(NamedKey::Shift) => Some(ModifiersState::SHIFT),
_ => None,
}
Expand All @@ -92,7 +92,7 @@ fn get_right_modifier_code(key: &Key) -> KeyCode {
Key::Named(NamedKey::Alt) => KeyCode::AltRight,
Key::Named(NamedKey::Control) => KeyCode::ControlRight,
Key::Named(NamedKey::Shift) => KeyCode::ShiftRight,
Key::Named(NamedKey::Super) => KeyCode::SuperRight,
Key::Named(NamedKey::Meta) => KeyCode::MetaRight,
_ => unreachable!(),
}
}
Expand All @@ -102,7 +102,7 @@ fn get_left_modifier_code(key: &Key) -> KeyCode {
Key::Named(NamedKey::Alt) => KeyCode::AltLeft,
Key::Named(NamedKey::Control) => KeyCode::ControlLeft,
Key::Named(NamedKey::Shift) => KeyCode::ShiftLeft,
Key::Named(NamedKey::Super) => KeyCode::SuperLeft,
Key::Named(NamedKey::Meta) => KeyCode::MetaLeft,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -1103,7 +1103,7 @@ fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEven
OptionAsAlt::Both if ev_mods.alt_key() => true,
_ => false,
} && !ev_mods.control_key()
&& !ev_mods.super_key();
&& !ev_mods.meta_key();

if ignore_alt_characters {
let ns_chars = unsafe {
Expand Down
Loading