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

Conversation

rustbasic
Copy link
Contributor

@rustbasic rustbasic commented Apr 5, 2024

These functions check whether a key has been pressed alone.

Add Functions :

modifiers_not_pressed()
key_pressed_only()
key_down_only()
key_down_exclusive()

For Test Code :

            if ui.input(|i| i.modifiers_not_pressed()) {
                ui.label("i.modifiers_not_pressed() : true");
            } else {
                ui.label("i.modifiers_not_pressed() : false");
            }

            if ui.input(|i| i.key_pressed(Key::A)) {
                ui.label("i.key_pressed(Key::A) : true");
            } else {
                ui.label("i.key_pressed(Key::A) : false");
            }

            if ui.input(|i| i.key_pressed_only(Key::A)) {
                ui.label("i.key_pressed_only(Key::A) : true");
            } else {
                ui.label("i.key_pressed_only(Key::A) : false");
            }

            if ui.input(|i| i.key_down(Key::A)) {
                ui.label("i.key_down(Key::A) : true");
            } else {
                ui.label("i.key_down(Key::A) : false");
            }

            if ui.input(|i| i.key_down_only(Key::A)) {
                ui.label("i.key_down_only(Key::A) : true");
            } else {
                ui.label("i.key_down_only(Key::A) : false");
            }

            if ui.input(|i| i.key_down_exclusive(Key::A)) {
                ui.label("i.key_down_exclusive(Key::A) : true");
            } else {
                ui.label("i.key_down_exclusive(Key::A) : false");
            }


/// 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()
}

@@ -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
}

}

/// 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()
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants