iced/core/src/keyboard/modifiers.rs
2021-07-27 12:54:39 +07:00

79 lines
2.2 KiB
Rust

use bitflags::bitflags;
bitflags! {
/// The current state of the keyboard modifiers.
#[derive(Default)]
pub struct Modifiers: u32{
/// The "shift" key.
const SHIFT = 0b100 << 0;
// const LSHIFT = 0b010 << 0;
// const RSHIFT = 0b001 << 0;
//
/// The "control" key.
const CTRL = 0b100 << 3;
// const LCTRL = 0b010 << 3;
// const RCTRL = 0b001 << 3;
//
/// The "alt" key.
const ALT = 0b100 << 6;
// const LALT = 0b010 << 6;
// const RALT = 0b001 << 6;
//
/// The "windows" key on Windows, "command" key on Mac, and
/// "super" key on Linux.
const LOGO = 0b100 << 9;
// const LLOGO = 0b010 << 9;
// const RLOGO = 0b001 << 9;
}
}
impl Modifiers {
/// The "command" key.
///
/// This is normally the main modifier to be used for hotkeys.
///
/// On macOS, this is equivalent to `Self::LOGO`.
/// Ohterwise, this is equivalent to `Self::CTRL`.
pub const COMMAND: Self = if cfg!(target_os = "macos") {
Self::LOGO
} else {
Self::CTRL
};
/// Returns true if the [`SHIFT`] key is pressed in the [`Modifiers`].
pub fn shift(self) -> bool {
self.contains(Self::SHIFT)
}
/// Returns true if the [`CTRL`] key is pressed in the [`Modifiers`].
pub fn control(self) -> bool {
self.contains(Self::CTRL)
}
/// Returns true if the [`ALT`] key is pressed in the [`Modifiers`].
pub fn alt(self) -> bool {
self.contains(Self::ALT)
}
/// Returns true if the [`LOGO`] key is pressed in the [`Modifiers`].
pub fn logo(self) -> bool {
self.contains(Self::LOGO)
}
/// Returns true if a "command key" is pressed in the [`Modifiers`].
///
/// 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
pub fn command(self) -> bool {
#[cfg(target_os = "macos")]
let is_pressed = self.logo();
#[cfg(not(target_os = "macos"))]
let is_pressed = self.control();
is_pressed
}
}