Check only for partial match of modifier keys

This commit is contained in:
Héctor Ramón Jiménez 2020-03-18 07:10:36 +01:00
parent eba2ded88a
commit 50b02d41a0
2 changed files with 21 additions and 4 deletions

View file

@ -1,5 +1,5 @@
/// The current state of the keyboard modifiers.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ModifiersState {
/// Whether a shift key is pressed
pub shift: bool,
@ -13,3 +13,18 @@ pub struct ModifiersState {
/// Whether a logo key is pressed (e.g. windows key, command key...)
pub logo: bool,
}
impl ModifiersState {
/// Returns true if the current [`ModifiersState`] has at least the same
/// modifiers enabled as the given value, and false otherwise.
///
/// [`ModifiersState`]: struct.ModifiersState.html
pub fn matches(&self, modifiers: ModifiersState) -> bool {
let shift = !modifiers.shift || modifiers.shift && self.shift;
let control = !modifiers.control || modifiers.control && self.control;
let alt = !modifiers.alt || modifiers.alt && self.alt;
let logo = !modifiers.logo || modifiers.logo && self.logo;
shift && control && alt && logo
}
}