From 86fa12229e7117306b8297a09aa22c99802600c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 10 Nov 2020 21:18:21 +0100 Subject: [PATCH] Introduce `is_command_pressed` to `ModifiersState` --- core/src/keyboard/modifiers_state.rs | 20 ++++++++++++++++++++ examples/pane_grid/src/main.rs | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs index 4d24266f..254013c3 100644 --- a/core/src/keyboard/modifiers_state.rs +++ b/core/src/keyboard/modifiers_state.rs @@ -15,6 +15,26 @@ pub struct ModifiersState { } impl ModifiersState { + /// Returns true if the current [`ModifiersState`] has a "command key" + /// pressed. + /// + /// The "command key" is the main modifier key used to issue commands in the + /// current platform. Specifically: + /// + /// - It is the `logo` or command key (⌘) on macOS + /// - It is the `control` key on other platforms + /// + /// [`ModifiersState`]: struct.ModifiersState.html + pub fn is_command_pressed(self) -> bool { + #[cfg(target_os = "macos")] + let is_pressed = self.logo; + + #[cfg(not(target_os = "macos"))] + let is_pressed = self.control; + + is_pressed + } + /// Returns true if the current [`ModifiersState`] has at least the same /// modifiers enabled as the given value, and false otherwise. /// diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index d149f9c6..e786eadd 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -123,7 +123,7 @@ impl Application for Example { Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - }) if modifiers.control => handle_hotkey(key_code), + }) if modifiers.is_command_pressed() => handle_hotkey(key_code), _ => None, }) }