pub struct KeyInputHandler {
pub show_ui: bool,
/* private fields */
}Fields§
§show_ui: boolImplementations§
Source§impl KeyInputHandler
impl KeyInputHandler
pub fn new() -> Self
Sourcepub fn handle_keyboard_input(
&mut self,
window: &Window,
event: &KeyEvent,
) -> bool
pub fn handle_keyboard_input( &mut self, window: &Window, event: &KeyEvent, ) -> bool
Examples found in repository?
examples/fluidsim.rs (line 412)
398 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
399 if self.base.egui_state.on_window_event(core.window(), event).consumed {
400 return true;
401 }
402 if self.base.handle_mouse_input(core, event, false) {
403 if let WindowEvent::MouseInput { state, button, .. } = event {
404 if *button == winit::event::MouseButton::Left && state.is_pressed() {
405 self.current_color = Self::generate_color();
406 }
407 }
408 return true;
409 }
410
411 if let WindowEvent::KeyboardInput { event, .. } = event {
412 return self.base.key_handler.handle_keyboard_input(core.window(), event);
413 }
414
415 false
416 }More examples
examples/veridisquo.rs (line 226)
199 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
200 if self
201 .base
202 .egui_state
203 .on_window_event(core.window(), event)
204 .consumed
205 {
206 return true;
207 }
208
209 if let WindowEvent::KeyboardInput { event, .. } = event {
210 if event.state == winit::event::ElementState::Pressed {
211 if let winit::keyboard::Key::Character(ref s) = event.logical_key {
212 if s.as_str() == "r" || s.as_str() == "R" {
213 self.base.start_time = std::time::Instant::now();
214 // Reset audio stream
215 if let Some(ref mut stream) = self.pcm_stream {
216 let _ = stream.stop();
217 let _ = stream.start();
218 }
219 return true;
220 }
221 }
222 }
223 return self
224 .base
225 .key_handler
226 .handle_keyboard_input(core.window(), event);
227 }
228
229 false
230 }examples/blockgame.rs (line 248)
208 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
209 let ui_handled = self
210 .base
211 .egui_state
212 .on_window_event(core.window(), event)
213 .consumed;
214
215 if self.base.handle_mouse_input(core, event, ui_handled) {
216 return true;
217 }
218
219 if let WindowEvent::KeyboardInput { event, .. } = event {
220 if let winit::keyboard::PhysicalKey::Code(key_code) = event.physical_key {
221 if event.state == ElementState::Pressed {
222 let camera_speed = 0.5;
223
224 match key_code {
225 winit::keyboard::KeyCode::KeyQ => {
226 self.game_params.camera_height += camera_speed;
227 return true;
228 }
229 winit::keyboard::KeyCode::KeyE => {
230 self.game_params.camera_height -= camera_speed;
231 return true;
232 }
233 winit::keyboard::KeyCode::KeyW => {
234 self.game_params.camera_angle += 0.1;
235 return true;
236 }
237 winit::keyboard::KeyCode::KeyS => {
238 self.game_params.camera_angle -= 0.1;
239 return true;
240 }
241 _ => {}
242 }
243 }
244 }
245 return self
246 .base
247 .key_handler
248 .handle_keyboard_input(core.window(), event);
249 }
250
251 false
252 }examples/synth.rs (line 347)
302 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
303 if self
304 .base
305 .egui_state
306 .on_window_event(core.window(), event)
307 .consumed
308 {
309 return true;
310 }
311
312 if let WindowEvent::KeyboardInput { event, .. } = event {
313 if let winit::keyboard::Key::Character(ref s) = event.logical_key {
314 if let Some(key_index) = s.chars().next().and_then(|c| c.to_digit(10)) {
315 if (1..=9).contains(&key_index) {
316 let index = (key_index - 1) as usize;
317
318 let current_time = self.base.controls.get_time(&self.base.start_time);
319 if event.state == winit::event::ElementState::Pressed && !self.keys_held[index] {
320 self.keys_held[index] = true;
321 let has_previous = self.current_params.key_states[index / 4][index % 4] > 0.0;
322 let in_release = self.current_params.key_decay[index / 4][index % 4] > 0.0;
323 if has_previous && in_release {
324 // Retrigger: just cancel the release, note continues from current level
325 self.set_key_release_time(index, 0.0);
326 } else {
327 // Fresh note
328 self.set_key_press_time(index, current_time);
329 self.set_key_release_time(index, 0.0);
330 }
331 self.compute_shader
332 .set_custom_params(self.current_params, &core.queue);
333 } else if event.state == winit::event::ElementState::Released {
334 self.keys_held[index] = false;
335 // Store release time — shader ADSR handles the fade
336 self.set_key_release_time(index, current_time);
337 self.compute_shader
338 .set_custom_params(self.current_params, &core.queue);
339 }
340 return true;
341 }
342 }
343 }
344 return self
345 .base
346 .key_handler
347 .handle_keyboard_input(core.window(), event);
348 }
349
350 false
351 }examples/gaussian3d.rs (line 437)
431 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
432 if self.base.egui_state.on_window_event(core.window(), event).consumed {
433 return true;
434 }
435
436 if let WindowEvent::KeyboardInput { event, .. } = event {
437 if self.base.key_handler.handle_keyboard_input(core.window(), event) {
438 return true;
439 }
440 if let winit::keyboard::Key::Character(ch) = &event.logical_key {
441 let key = ch.as_str().to_lowercase();
442 match event.state {
443 winit::event::ElementState::Pressed => {
444 if key == "r" {
445 self.camera.reset();
446 self.sorter.force_sort();
447 return true;
448 }
449 if matches!(key.as_str(), "w" | "a" | "s" | "d" | "q" | "e") {
450 self.camera.keys_held.insert(key);
451 return true;
452 }
453 }
454 winit::event::ElementState::Released => {
455 self.camera.keys_held.remove(&key);
456 }
457 }
458 }
459 }
460
461 if let WindowEvent::MouseInput { state, button, .. } = event {
462 if *button == winit::event::MouseButton::Left {
463 self.camera.is_dragging = *state == winit::event::ElementState::Pressed;
464 return true;
465 }
466 }
467
468 if let WindowEvent::CursorMoved { position, .. } = event {
469 let x = position.x as f32;
470 let y = position.y as f32;
471 if self.camera.is_dragging {
472 let dx = x - self.camera.last_mouse[0];
473 let dy = y - self.camera.last_mouse[1];
474 self.camera.yaw += dx * 0.01;
475 self.camera.pitch = (self.camera.pitch + dy * 0.01).clamp(-1.5, 1.5);
476 }
477 self.camera.last_mouse = [x, y];
478 return self.camera.is_dragging;
479 }
480
481 if let WindowEvent::MouseWheel { delta, .. } = event {
482 let d = match delta {
483 winit::event::MouseScrollDelta::LineDelta(_, y) => *y,
484 winit::event::MouseScrollDelta::PixelDelta(p) => (p.y as f32 / 100.0).clamp(-3.0, 3.0)};
485 let factor = (1.0 + d * 0.1).clamp(0.5, 2.0);
486 self.camera.distance = (self.camera.distance * factor).clamp(0.1, 500.0);
487 return true;
488 }
489
490 if let WindowEvent::DroppedFile(path) = event {
491 if path.extension().map(|e| e == "ply").unwrap_or(false) {
492 self.load_ply(core, path);
493 }
494 return true;
495 }
496
497 false
498 }examples/mandelbulb.rs (line 666)
582 fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
583 if self
584 .base
585 .egui_state
586 .on_window_event(core.window(), event)
587 .consumed
588 {
589 return true;
590 }
591
592 if self.base.handle_mouse_input(core, event, false) {
593 return true;
594 }
595
596 if let WindowEvent::KeyboardInput { event, .. } = event {
597 if let winit::keyboard::Key::Character(ch) = &event.logical_key {
598 match ch.as_str() {
599 " " => {
600 if event.state == winit::event::ElementState::Released {
601 self.current_params.accumulate = 1 - self.current_params.accumulate;
602 self.should_reset_accumulation = true;
603 self.compute_shader
604 .set_custom_params(self.current_params, &core.queue);
605 return true;
606 }
607 }
608 "m" | "M" => {
609 if event.state == winit::event::ElementState::Released {
610 self.mouse_enabled = !self.mouse_enabled;
611 self.mouse_initialized = false;
612 return true;
613 }
614 }
615 "w" | "W" => {
616 if event.state == winit::event::ElementState::Pressed {
617 self.accumulated_rotation[1] -= 0.1;
618 self.should_reset_accumulation = true;
619 return true;
620 }
621 }
622 "s" | "S" => {
623 if event.state == winit::event::ElementState::Pressed {
624 self.accumulated_rotation[1] += 0.1;
625 self.should_reset_accumulation = true;
626 return true;
627 }
628 }
629 "a" | "A" => {
630 if event.state == winit::event::ElementState::Pressed {
631 self.accumulated_rotation[0] -= 0.1;
632 self.should_reset_accumulation = true;
633 return true;
634 }
635 }
636 "d" | "D" => {
637 if event.state == winit::event::ElementState::Pressed {
638 self.accumulated_rotation[0] += 0.1;
639 self.should_reset_accumulation = true;
640 return true;
641 }
642 }
643 "q" | "Q" => {
644 if event.state == winit::event::ElementState::Pressed {
645 self.accumulated_rotation[2] -= 0.1;
646 self.should_reset_accumulation = true;
647 return true;
648 }
649 }
650 "e" | "E" => {
651 if event.state == winit::event::ElementState::Pressed {
652 self.accumulated_rotation[2] += 0.1;
653 self.should_reset_accumulation = true;
654 return true;
655 }
656 }
657 _ => {}
658 }
659 }
660 }
661
662 if let WindowEvent::KeyboardInput { event, .. } = event {
663 if self
664 .base
665 .key_handler
666 .handle_keyboard_input(core.window(), event)
667 {
668 return true;
669 }
670 }
671
672 false
673 }Additional examples can be found in:
Trait Implementations§
Auto Trait Implementations§
impl Freeze for KeyInputHandler
impl RefUnwindSafe for KeyInputHandler
impl Send for KeyInputHandler
impl Sync for KeyInputHandler
impl Unpin for KeyInputHandler
impl UnsafeUnpin for KeyInputHandler
impl UnwindSafe for KeyInputHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().