egui

Struct Modifiers

Source
pub struct Modifiers {
    pub alt: bool,
    pub ctrl: bool,
    pub shift: bool,
    pub mac_cmd: bool,
    pub command: bool,
}
Expand description

State of the modifier keys. These must be fed to egui.

The best way to compare Modifiers is by using Modifiers::matches.

NOTE: For cross-platform uses, ALT+SHIFT is a bad combination of modifiers as on mac that is how you type special characters, so those key presses are usually not reported to egui.

Fields§

§alt: bool

Either of the alt keys are down (option ⌥ on Mac).

§ctrl: bool

Either of the control keys are down. When checking for keyboard shortcuts, consider using Self::command instead.

§shift: bool

Either of the shift keys are down.

§mac_cmd: bool

The Mac ⌘ Command key. Should always be set to false on other platforms.

§command: bool

On Windows and Linux, set this to the same value as ctrl. On Mac, this should be set whenever one of the ⌘ Command keys are down (same as mac_cmd). This is so that egui can, for instance, select all text by checking for command + A and it will work on both Mac and Windows.

Implementations§

Source§

impl Modifiers

Source

pub const NONE: Self = _

Source

pub const ALT: Self = _

Source

pub const CTRL: Self = _

Source

pub const SHIFT: Self = _

Source

pub const MAC_CMD: Self = _

The Mac ⌘ Command key

Source

pub const COMMAND: Self = _

On Mac: ⌘ Command key, elsewhere: Ctrl key

Source

pub const fn plus(self, rhs: Self) -> Self

assert_eq!(
    Modifiers::CTRL | Modifiers::ALT,
    Modifiers { ctrl: true, alt: true, ..Default::default() }
);
assert_eq!(
    Modifiers::ALT.plus(Modifiers::CTRL),
    Modifiers::CTRL.plus(Modifiers::ALT),
);
assert_eq!(
    Modifiers::CTRL | Modifiers::ALT,
    Modifiers::CTRL.plus(Modifiers::ALT),
);
Source

pub fn is_none(&self) -> bool

Source

pub fn any(&self) -> bool

Source

pub fn all(&self) -> bool

Source

pub fn shift_only(&self) -> bool

Is shift the only pressed button?

Source

pub fn command_only(&self) -> bool

true if only Self::ctrl or only Self::mac_cmd is pressed.

Source

pub fn matches_logically(&self, pattern: Self) -> bool

Checks that the ctrl/cmd matches, and that the shift/alt of the argument is a subset of the pressed key (self).

This means that if the pattern has not set shift, then self can have shift set or not.

The reason is that many logical keys require shift or alt on some keyboard layouts. For instance, in order to press + on an English keyboard, you need to press shift and =, but a Swedish keyboard has dedicated + key. So if you want to make a KeyboardShortcut looking for Cmd + +, it makes sense to ignore the shift key. Similarly, the Alt key is sometimes used to type special characters.

However, if the pattern (the argument) explicitly requires the shift or alt keys to be pressed, then they must be pressed.

§Example:
if pressed_modifiers.matches(Modifiers::ALT | Modifiers::SHIFT) {
    // Alt and Shift are pressed, and nothing else
}
§Behavior:
assert!(Modifiers::CTRL.matches_logically(Modifiers::CTRL));
assert!(!Modifiers::CTRL.matches_logically(Modifiers::CTRL | Modifiers::SHIFT));
assert!((Modifiers::CTRL | Modifiers::SHIFT).matches_logically(Modifiers::CTRL));
assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::CTRL));
assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
assert!((Modifiers::MAC_CMD | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
assert!(!Modifiers::COMMAND.matches_logically(Modifiers::MAC_CMD));
Source

pub fn matches_exact(&self, pattern: Self) -> bool

Check for equality but with proper handling of Self::command.

self here are the currently pressed modifiers, and the argument the pattern we are testing for.

Note that this will require the shift and alt keys match, even though these modifiers are sometimes required to produce some logical keys. For instance, to press + on an English keyboard, you need to press shift and =, but on a Swedish keyboard you can press the dedicated + key. Therefore, you often want to use Self::matches_logically instead.

§Example:
if pressed_modifiers.matches(Modifiers::ALT | Modifiers::SHIFT) {
    // Alt and Shift are pressed, and nothing else
}
§Behavior:
assert!(Modifiers::CTRL.matches(Modifiers::CTRL));
assert!(!Modifiers::CTRL.matches(Modifiers::CTRL | Modifiers::SHIFT));
assert!(!(Modifiers::CTRL | Modifiers::SHIFT).matches(Modifiers::CTRL));
assert!((Modifiers::CTRL | Modifiers::COMMAND).matches(Modifiers::CTRL));
assert!((Modifiers::CTRL | Modifiers::COMMAND).matches(Modifiers::COMMAND));
assert!((Modifiers::MAC_CMD | Modifiers::COMMAND).matches(Modifiers::COMMAND));
assert!(!Modifiers::COMMAND.matches(Modifiers::MAC_CMD));
Source

pub fn matches(&self, pattern: Self) -> bool

👎Deprecated: Renamed matches_exact, but maybe you want to use matches_logically instead
Source

pub fn cmd_ctrl_matches(&self, pattern: Self) -> bool

Checks only cmd/ctrl, not alt/shift.

self here are the currently pressed modifiers, and the argument the pattern we are testing for.

This takes care to properly handle the difference between Self::ctrl, Self::command and Self::mac_cmd.

Source

pub fn contains(&self, query: Self) -> bool

Whether another set of modifiers is contained in this set of modifiers with proper handling of Self::command.

assert!(Modifiers::default().contains(Modifiers::default()));
assert!(Modifiers::CTRL.contains(Modifiers::default()));
assert!(Modifiers::CTRL.contains(Modifiers::CTRL));
assert!(Modifiers::CTRL.contains(Modifiers::COMMAND));
assert!(Modifiers::MAC_CMD.contains(Modifiers::COMMAND));
assert!(Modifiers::COMMAND.contains(Modifiers::MAC_CMD));
assert!(Modifiers::COMMAND.contains(Modifiers::CTRL));
assert!(!(Modifiers::ALT | Modifiers::CTRL).contains(Modifiers::SHIFT));
assert!((Modifiers::CTRL | Modifiers::SHIFT).contains(Modifiers::CTRL));
assert!(!Modifiers::CTRL.contains(Modifiers::CTRL | Modifiers::SHIFT));

Trait Implementations§

Source§

impl BitOr for Modifiers

Source§

type Output = Modifiers

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
Source§

impl Clone for Modifiers

Source§

fn clone(&self) -> Modifiers

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Modifiers

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Modifiers

Source§

fn default() -> Modifiers

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Modifiers

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for Modifiers

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Modifiers

Source§

fn eq(&self, other: &Modifiers) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Modifiers

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Modifiers

Source§

impl Eq for Modifiers

Source§

impl StructuralPartialEq for Modifiers

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,