Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

These functions check whether a key has been pressed alone. #4328

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f0b7737
Update memory.rs
rustbasic Mar 23, 2024
3b98d7f
Update epi_integration.rs
rustbasic Mar 23, 2024
af5a7bb
Update memory.rs
rustbasic Mar 23, 2024
7f66b56
Update memory.rs
rustbasic Mar 23, 2024
6396c4a
Update epi_integration.rs
rustbasic Mar 23, 2024
9fe42ff
Merge branch 'emilk:master' into master
rustbasic Mar 25, 2024
d7673fe
Merge branch 'emilk:master' into master
rustbasic Mar 25, 2024
a7edc53
Merge branch 'emilk:master' into master
rustbasic Mar 26, 2024
2432784
Merge branch 'emilk:master' into master
rustbasic Mar 27, 2024
9b6209b
Merge branch 'emilk:master' into master
rustbasic Mar 27, 2024
f3687f6
Merge branch 'emilk:master' into master
rustbasic Mar 28, 2024
78880de
Merge branch 'emilk:master' into master
rustbasic Mar 29, 2024
01ba2ec
Merge branch 'emilk:master' into master
rustbasic Mar 29, 2024
0ae2451
Merge branch 'emilk:master' into master
rustbasic Mar 29, 2024
e6c84ce
Merge branch 'emilk:master' into master
rustbasic Mar 30, 2024
821dff0
Update epi_integration.rs
rustbasic Mar 30, 2024
eecfafd
Merge branch 'emilk:master' into master
rustbasic Mar 30, 2024
cbb5ac7
Merge branch 'emilk:master' into master
rustbasic Mar 30, 2024
07b5143
Merge branch 'emilk:master' into master
rustbasic Mar 31, 2024
3313633
Update epi_integration.rs
rustbasic Mar 31, 2024
90b968c
Merge branch 'emilk:master' into master
rustbasic Apr 1, 2024
13af44f
Merge branch 'emilk:master' into master
rustbasic Apr 1, 2024
5d95f09
Merge branch 'emilk:master' into master
rustbasic Apr 1, 2024
4be440a
Merge branch 'emilk:master' into master
rustbasic Apr 1, 2024
1ef0e10
Merge branch 'emilk:master' into master
rustbasic Apr 2, 2024
6541324
Merge branch 'emilk:master' into master
rustbasic Apr 2, 2024
8abc161
Merge branch 'emilk:master' into master
rustbasic Apr 3, 2024
1b21742
Merge branch 'emilk:master' into master
rustbasic Apr 4, 2024
052bb68
Merge branch 'emilk:master' into master
rustbasic Apr 5, 2024
b1a0b89
Update input_state.rs
rustbasic Apr 5, 2024
e99b446
Update input_state.rs
rustbasic Apr 5, 2024
8f66fdf
Update input.rs
rustbasic Apr 7, 2024
9525357
Merge branch 'emilk:master' into patch36
rustbasic Apr 19, 2024
ff1eab8
Merge branch 'emilk:master' into patch36
rustbasic Apr 21, 2024
d292a6d
Merge branch 'emilk:master' into patch36
rustbasic Apr 22, 2024
7daae80
Merge branch 'emilk:master' into patch36
rustbasic Apr 22, 2024
eba4eeb
Merge branch 'emilk:master' into patch36
rustbasic Apr 23, 2024
35b38ba
Merge branch 'emilk:master' into patch36
rustbasic Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ impl Modifiers {
}

/// Checks that the `ctrl/cmd` matches, and that the `shift/alt` of the argument is a subset
/// of the pressed ksey (`self`).
/// of the pressed key (`self`).
///
/// This means that if the pattern has not set `shift`, then `self` can have `shift` set or not.
///
Expand Down
22 changes: 22 additions & 0 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ impl InputState {
self.consume_key(modifiers, logical_key)
}

/// Verifies that all modifier keys (e.g. Ctrl, Alt) are not pressed.
pub fn modifiers_not_pressed(&self) -> bool {
self.modifiers.is_none()
}

/// Was only the given key pressed this frame, with no modifiers?
pub fn key_pressed_only(&self, desired_key: Key) -> bool {
self.num_presses(desired_key) > 0 && self.modifiers_not_pressed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if one day key_pressed might be modified -we could easily forget this - fix this using the function here, too

pub fn key_pressed_only(&self, desired_key: Key) -> bool {
        key_pressed(desired_key) && self.modifiers_not_pressed()
}

}

/// Was the given key pressed this frame?
///
/// Includes key-repeat events.
Expand All @@ -416,6 +426,18 @@ impl InputState {
.count()
}

/// Is the given key currently held down and no other keys are held down, including modifier keys?
pub fn key_down_exclusive(&self, desired_key: Key) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above:

pub fn key_down_exclusive(&self, desired_key: Key) -> bool {
       key_down(desired_key) && self.keys_down.len() == 1
}

self.keys_down.contains(&desired_key)
&& self.keys_down.len() == 1
&& self.modifiers_not_pressed()
}

/// Is the given key currently held down and no other keys are held down?
pub fn key_down_only(&self, desired_key: Key) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above:

pub fn key_down_only(&self, desired_key: Key) -> bool {
    key_down(desired_key) && self.modifiers_not_pressed()
    }

self.keys_down.contains(&desired_key) && self.modifiers_not_pressed()
}

/// Is the given key currently held down?
pub fn key_down(&self, desired_key: Key) -> bool {
self.keys_down.contains(&desired_key)
Expand Down
Loading