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§
§impl Modifiers
impl Modifiers
pub const NONE: Modifiers = _
pub const ALT: Modifiers = _
pub const CTRL: Modifiers = _
pub const SHIFT: Modifiers = _
pub const fn plus(self, rhs: Modifiers) -> Modifiers
pub const fn plus(self, rhs: Modifiers) -> Modifiers
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),
);
pub fn is_none(&self) -> bool
pub fn any(&self) -> bool
pub fn all(&self) -> bool
pub fn shift_only(&self) -> bool
pub fn shift_only(&self) -> bool
Is shift the only pressed button?
pub fn command_only(&self) -> bool
pub fn command_only(&self) -> bool
true if only Self::ctrl
or only Self::mac_cmd
is pressed.
pub fn matches_logically(&self, pattern: Modifiers) -> bool
pub fn matches_logically(&self, pattern: Modifiers) -> 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));
pub fn matches_exact(&self, pattern: Modifiers) -> bool
pub fn matches_exact(&self, pattern: Modifiers) -> 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));
pub fn matches(&self, pattern: Modifiers) -> bool
matches_exact
, but maybe you want to use matches_logically
insteadpub fn cmd_ctrl_matches(&self, pattern: Modifiers) -> bool
pub fn cmd_ctrl_matches(&self, pattern: Modifiers) -> 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
.
pub fn contains(&self, query: Modifiers) -> bool
pub fn contains(&self, query: Modifiers) -> 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§
§impl<'de> Deserialize<'de> for Modifiers
impl<'de> Deserialize<'de> for Modifiers
§fn deserialize<__D>(
__deserializer: __D
) -> Result<Modifiers, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Modifiers, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl Serialize for Modifiers
impl Serialize for Modifiers
§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Copy for Modifiers
impl Eq for Modifiers
impl StructuralPartialEq for Modifiers
Auto Trait Implementations§
impl Freeze for Modifiers
impl RefUnwindSafe for Modifiers
impl Send for Modifiers
impl Sync for Modifiers
impl Unpin for Modifiers
impl UnwindSafe for Modifiers
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request