Skip to content

Commit 38fd3c6

Browse files
authored
winit-core/keyboard: clarify modifier docs
Emphasize the difference between logical and physical state, reference sticky mods.
1 parent 5190472 commit 38fd3c6

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/platform_impl/linux/common/xkb/state.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ impl Drop for XkbState {
153153
}
154154
}
155155

156-
/// Represents the current state of the keyboard modifiers
156+
/// Represents the current logical state of the keyboard modifiers
157157
///
158158
/// Each field of this struct represents a modifier and is `true` if this modifier is active.
159159
///
160-
/// For some modifiers, this means that the key is currently pressed, others are toggled
161-
/// (like caps lock).
160+
/// For some modifiers, this means that the key is logically pressed, others are toggled (like Caps
161+
/// Lock). But physically the key can be in any state (for example, released Shift when
162+
/// it's active as a sticky modifier or released Caps Lock when it's toggled on)
162163
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
163164
pub struct ModifiersState {
164165
/// The "control" key

winit-core/src/event.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ pub struct KeyEvent {
808808
pub struct Modifiers {
809809
pub(crate) state: ModifiersState,
810810

811-
// NOTE: Currently pressed modifiers keys.
811+
// NOTE: Currently active modifiers keys (logically, but not necessarily physically, pressed).
812812
//
813813
// The field providing a metadata, it shouldn't be used as a source of truth.
814814
pub(crate) pressed_mods: ModifiersKeys,
@@ -820,47 +820,47 @@ impl Modifiers {
820820
Self { state, pressed_mods }
821821
}
822822

823-
/// The state of the modifiers.
823+
/// The logical state of the modifiers.
824824
pub fn state(&self) -> ModifiersState {
825825
self.state
826826
}
827827

828-
/// The state of the left shift key.
828+
/// The logical state of the left shift key.
829829
pub fn lshift_state(&self) -> ModifiersKeyState {
830830
self.mod_state(ModifiersKeys::LSHIFT)
831831
}
832832

833-
/// The state of the right shift key.
833+
/// The logical state of the right shift key.
834834
pub fn rshift_state(&self) -> ModifiersKeyState {
835835
self.mod_state(ModifiersKeys::RSHIFT)
836836
}
837837

838-
/// The state of the left alt key.
838+
/// The logical state of the left alt key.
839839
pub fn lalt_state(&self) -> ModifiersKeyState {
840840
self.mod_state(ModifiersKeys::LALT)
841841
}
842842

843-
/// The state of the right alt key.
843+
/// The logical state of the right alt key.
844844
pub fn ralt_state(&self) -> ModifiersKeyState {
845845
self.mod_state(ModifiersKeys::RALT)
846846
}
847847

848-
/// The state of the left control key.
848+
/// The logical state of the left control key.
849849
pub fn lcontrol_state(&self) -> ModifiersKeyState {
850850
self.mod_state(ModifiersKeys::LCONTROL)
851851
}
852852

853-
/// The state of the right control key.
853+
/// The logical state of the right control key.
854854
pub fn rcontrol_state(&self) -> ModifiersKeyState {
855855
self.mod_state(ModifiersKeys::RCONTROL)
856856
}
857857

858-
/// The state of the left super key.
858+
/// The logical state of the left super key.
859859
pub fn lsuper_state(&self) -> ModifiersKeyState {
860860
self.mod_state(ModifiersKeys::LMETA)
861861
}
862862

863-
/// The state of the right super key.
863+
/// The logical state of the right super key.
864864
pub fn rsuper_state(&self) -> ModifiersKeyState {
865865
self.mod_state(ModifiersKeys::RMETA)
866866
}

winit-core/src/keyboard.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,9 +1687,12 @@ pub enum KeyLocation {
16871687
}
16881688

16891689
bitflags! {
1690-
/// Represents the current state of the keyboard modifiers
1690+
/// Represents the current logical state of the keyboard modifiers
16911691
///
16921692
/// Each flag represents a modifier and is set if this modifier is active.
1693+
///
1694+
/// Note that the modifier key can be physically released with the modifier
1695+
/// still being marked as active, as in the case of sticky modifiers.
16931696
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16941697
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16951698
pub struct ModifiersState: u32 {
@@ -1707,34 +1710,39 @@ bitflags! {
17071710
}
17081711

17091712
impl ModifiersState {
1710-
/// Returns `true` if the shift key is pressed.
1713+
/// Returns whether the shift modifier is active.
17111714
pub fn shift_key(&self) -> bool {
17121715
self.intersects(Self::SHIFT)
17131716
}
17141717

1715-
/// Returns `true` if the control key is pressed.
1718+
/// Returns whether the control modifier is active.
17161719
pub fn control_key(&self) -> bool {
17171720
self.intersects(Self::CONTROL)
17181721
}
17191722

1720-
/// Returns `true` if the alt key is pressed.
1723+
/// Returns whether the alt modifier is active.
17211724
pub fn alt_key(&self) -> bool {
17221725
self.intersects(Self::ALT)
17231726
}
17241727

1725-
/// Returns `true` if the meta key is pressed.
1728+
/// Returns whether the meta modifier is active.
17261729
pub fn meta_key(&self) -> bool {
17271730
self.intersects(Self::META)
17281731
}
17291732
}
17301733

1731-
/// The state of the particular modifiers key.
1734+
/// The logical state of the particular modifiers key.
17321735
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
17331736
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17341737
pub enum ModifiersKeyState {
1735-
/// The particular key is pressed.
1738+
/// The particular modifier is active or logically, but not necessarily physically, pressed.
17361739
Pressed,
17371740
/// The state of the key is unknown.
1741+
///
1742+
/// Can include cases when the key is active or logically pressed, for example, when a sticky
1743+
/// **Shift** is active, the OS might not preserve information that it was activated by
1744+
/// RightShift, so the state of [`ModifiersKeys::RSHIFT`] will be unknown while the state
1745+
/// of [`ModifiersState::SHIFT`] will be active.
17381746
#[default]
17391747
Unknown,
17401748
}

0 commit comments

Comments
 (0)