Skip to content

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

Open
rustbasic wants to merge 38 commits intoemilk:mainfrom
rustbasic:patch36
Open

These functions check whether a key has been pressed alone.#4328
rustbasic wants to merge 38 commits intoemilk:mainfrom
rustbasic:patch36

Conversation

@rustbasic
Copy link
Copy Markdown
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
Copy Markdown
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()
}

}

/// 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
Copy Markdown
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
Copy Markdown
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