-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Input combination / modifier keys abstraction #1654
Comments
Yeah, thanks. My current sample code looks like this: struct KeyboardModifierState {
shift: ElementState,
alt: ElementState,
control: ElementState,
}
impl Default for KeyboardModifierState {
fn default() -> Self {
Self {
shift: ElementState::Released,
alt: ElementState::Released,
control: ElementState::Released,
}
}
}
fn keyboard_input_system(
mut modifier_state: Local<KeyboardModifierState>,
mut eventreader_keyboard: Local<EventReader<KeyboardInput>>,
keyboard_input_events: Res<Events<KeyboardInput>>,
) {
for keyevent in eventreader_keyboard.iter(&keyboard_input_events) {
//println!("{:?}", keyevent);
match keyevent {
KeyboardInput {
key_code: Some(KeyCode::LShift),
state,
..
}
| KeyboardInput {
key_code: Some(KeyCode::RShift),
state,
..
} => modifier_state.shift = state.clone(),
KeyboardInput {
key_code: Some(KeyCode::LAlt),
state,
..
}
| KeyboardInput {
key_code: Some(KeyCode::RAlt),
state,
..
} => modifier_state.alt = state.clone(),
KeyboardInput {
key_code: Some(KeyCode::LControl),
state,
..
}
| KeyboardInput {
key_code: Some(KeyCode::RControl),
state,
..
} => modifier_state.control = state.clone(),
KeyboardInput {
key_code: Some(KeyCode::A),
state: ElementState::Pressed,
..
} => println!(
"A with Shift {:?} Alt {:?} Control {:?}",
modifier_state.shift, modifier_state.alt, modifier_state.control
),
_ => {
// ignore all else
}
}
}
} |
This has come up previously (#989 (comment)). Unless I'm missing a use case, I think Bevy fn process_input(input: Res<Input<KeyCode>>) {
let shift = input.pressed(KeyCode::LShift) || input.pressed(KeyCode::RShift);
let ctrl = input.pressed(KeyCode::LControl) || input.pressed(KeyCode::RControl);
if ctrl && shift && input.pressed(KeyCode::A) {
println!("ctrl + shift + a");
}
} |
Ah, I missed it! Thanks; I'll close this out. |
This PR adds a small example that shows how to use Keyboard modifiers, as shown in [this](#1654 (comment)) snippet. Fixes #1656. Co-authored-by: guimcaballero <[email protected]>
@cart I don't think this is quite the same ... your code is " I thought about using a FixedTimestep so I only get repeats every 0.1 seconds or whatever, but that doesn't work because it only fires if the keys are pressed on that time through. So with the "is currently pressed" approach, I'm moving from the ugly key match thing to a different complicated thing where I check what's pressed constantly but only actually do something about it 20 times a second (or whatever). (My specific use case is WASD movement with shift to slow down and ctrl to speed up.) The nicest would be to have the KeyboardInput event show the state of the modifiers at the time they were pressed. (See for example rust-sdl2. |
Reopening to see if we can address this more fully. |
@mattdm what are you trying to achieve? You have the |
Try it; this won't work to get the same key-repeat behavior I describe or that my code which tracks the state does. Also, this would be nice simply because it is similar to functionality offered by other frameworks -- see the rust-sdl2 link above -- which makes porting easier. |
What problem does this solve or what need does it fill?
Modifier keys are very commonly used in both games and applications. The existing approach to this is very manual, and heavy on boilerplate. See the cheatbook page for a good overview of how to work with input right now.
What solution would you like?
Expose a first-class solution for modifier keys in bevy_input. I'm not immediately sure what this would look like, so suggestions are very welcome.
What alternative(s) have you considered?
Use match statements everywhere, repeatedly. Iterate through eventreader (because that says it is in order) and maintain my own modifier key state as a local resource (credit to
@mattdm
on Discord.Additional context
None.
The text was updated successfully, but these errors were encountered: