Skip to main content

KeyInputHandler

Struct KeyInputHandler 

Source
pub struct KeyInputHandler {
    pub show_ui: bool,
    /* private fields */
}

Fields§

§show_ui: bool

Implementations§

Source§

impl KeyInputHandler

Source

pub fn new() -> Self

Source

pub fn handle_keyboard_input( &mut self, window: &Window, event: &KeyEvent, ) -> bool

Examples found in repository?
examples/veridisquo.rs (line 221)
199    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
200        if self.base.forward_to_egui(core, event) {
201            return true;
202        }
203
204        if let WindowEvent::KeyboardInput { event, .. } = event {
205            if event.state == winit::event::ElementState::Pressed {
206                if let winit::keyboard::Key::Character(ref s) = event.logical_key {
207                    if s.as_str() == "r" || s.as_str() == "R" {
208                        self.base.start_time = std::time::Instant::now();
209                        // Reset audio stream
210                        if let Some(ref mut stream) = self.pcm_stream {
211                            let _ = stream.stop();
212                            let _ = stream.start();
213                        }
214                        return true;
215                    }
216                }
217            }
218            return self
219                .base
220                .key_handler
221                .handle_keyboard_input(core.window(), event);
222        }
223
224        false
225    }
More examples
Hide additional examples
examples/blockgame.rs (line 244)
208    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
209        let ui_handled = self.base.forward_to_egui(core, event);
210
211        if self.base.handle_mouse_input(core, event, ui_handled) {
212            return true;
213        }
214
215        if let WindowEvent::KeyboardInput { event, .. } = event {
216            if let winit::keyboard::PhysicalKey::Code(key_code) = event.physical_key {
217                if event.state == ElementState::Pressed {
218                    let camera_speed = 0.5;
219
220                    match key_code {
221                        winit::keyboard::KeyCode::KeyQ => {
222                            self.game_params.camera_height += camera_speed;
223                            return true;
224                        }
225                        winit::keyboard::KeyCode::KeyE => {
226                            self.game_params.camera_height -= camera_speed;
227                            return true;
228                        }
229                        winit::keyboard::KeyCode::KeyW => {
230                            self.game_params.camera_angle += 0.1;
231                            return true;
232                        }
233                        winit::keyboard::KeyCode::KeyS => {
234                            self.game_params.camera_angle -= 0.1;
235                            return true;
236                        }
237                        _ => {}
238                    }
239                }
240            }
241            return self
242                .base
243                .key_handler
244                .handle_keyboard_input(core.window(), event);
245        }
246
247        false
248    }
examples/synth.rs (line 342)
302    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
303        if self.base.forward_to_egui(core, event) {
304            return true;
305        }
306
307        if let WindowEvent::KeyboardInput { event, .. } = event {
308            if let winit::keyboard::Key::Character(ref s) = event.logical_key {
309                if let Some(key_index) = s.chars().next().and_then(|c| c.to_digit(10)) {
310                    if (1..=9).contains(&key_index) {
311                        let index = (key_index - 1) as usize;
312
313                        let current_time = self.base.controls.get_time(&self.base.start_time);
314                        if event.state == winit::event::ElementState::Pressed && !self.keys_held[index] {
315                            self.keys_held[index] = true;
316                            let has_previous = self.current_params.key_states[index / 4][index % 4] > 0.0;
317                            let in_release = self.current_params.key_decay[index / 4][index % 4] > 0.0;
318                            if has_previous && in_release {
319                                // Retrigger: just cancel the release, note continues from current level
320                                self.set_key_release_time(index, 0.0);
321                            } else {
322                                // Fresh note
323                                self.set_key_press_time(index, current_time);
324                                self.set_key_release_time(index, 0.0);
325                            }
326                            self.compute_shader
327                                .set_custom_params(self.current_params, &core.queue);
328                        } else if event.state == winit::event::ElementState::Released {
329                            self.keys_held[index] = false;
330                            // Store release time — shader ADSR handles the fade
331                            self.set_key_release_time(index, current_time);
332                            self.compute_shader
333                                .set_custom_params(self.current_params, &core.queue);
334                        }
335                        return true;
336                    }
337                }
338            }
339            return self
340                .base
341                .key_handler
342                .handle_keyboard_input(core.window(), event);
343        }
344
345        false
346    }
examples/gaussian3d.rs (line 437)
431    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
432        if self.base.forward_to_egui(core, event) {
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 661)
582    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
583        if self.base.forward_to_egui(core, event) {
584            return true;
585        }
586
587        if self.base.handle_mouse_input(core, event, false) {
588            return true;
589        }
590
591        if let WindowEvent::KeyboardInput { event, .. } = event {
592            if let winit::keyboard::Key::Character(ch) = &event.logical_key {
593                match ch.as_str() {
594                    " " => {
595                        if event.state == winit::event::ElementState::Released {
596                            self.current_params.accumulate = 1 - self.current_params.accumulate;
597                            self.should_reset_accumulation = true;
598                            self.compute_shader
599                                .set_custom_params(self.current_params, &core.queue);
600                            return true;
601                        }
602                    }
603                    "m" | "M" => {
604                        if event.state == winit::event::ElementState::Released {
605                            self.mouse_enabled = !self.mouse_enabled;
606                            self.mouse_initialized = false;
607                            return true;
608                        }
609                    }
610                    "w" | "W" => {
611                        if event.state == winit::event::ElementState::Pressed {
612                            self.accumulated_rotation[1] -= 0.1;
613                            self.should_reset_accumulation = true;
614                            return true;
615                        }
616                    }
617                    "s" | "S" => {
618                        if event.state == winit::event::ElementState::Pressed {
619                            self.accumulated_rotation[1] += 0.1;
620                            self.should_reset_accumulation = true;
621                            return true;
622                        }
623                    }
624                    "a" | "A" => {
625                        if event.state == winit::event::ElementState::Pressed {
626                            self.accumulated_rotation[0] -= 0.1;
627                            self.should_reset_accumulation = true;
628                            return true;
629                        }
630                    }
631                    "d" | "D" => {
632                        if event.state == winit::event::ElementState::Pressed {
633                            self.accumulated_rotation[0] += 0.1;
634                            self.should_reset_accumulation = true;
635                            return true;
636                        }
637                    }
638                    "q" | "Q" => {
639                        if event.state == winit::event::ElementState::Pressed {
640                            self.accumulated_rotation[2] -= 0.1;
641                            self.should_reset_accumulation = true;
642                            return true;
643                        }
644                    }
645                    "e" | "E" => {
646                        if event.state == winit::event::ElementState::Pressed {
647                            self.accumulated_rotation[2] += 0.1;
648                            self.should_reset_accumulation = true;
649                            return true;
650                        }
651                    }
652                    _ => {}
653                }
654            }
655        }
656
657        if let WindowEvent::KeyboardInput { event, .. } = event {
658            if self
659                .base
660                .key_handler
661                .handle_keyboard_input(core.window(), event)
662            {
663                return true;
664            }
665        }
666
667        false
668    }
examples/pathtracing.rs (line 544)
454    fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool {
455        if self.base.forward_to_egui(core, event) {
456            return true;
457        }
458
459        if let WindowEvent::KeyboardInput { event, .. } = event {
460            if let winit::keyboard::Key::Character(ch) = &event.logical_key {
461                match ch.as_str() {
462                    "w" | "W" => {
463                        self.camera_movement.forward =
464                            event.state == winit::event::ElementState::Pressed;
465                        self.should_reset_accumulation = true;
466                        return true;
467                    }
468                    "s" | "S" => {
469                        self.camera_movement.backward =
470                            event.state == winit::event::ElementState::Pressed;
471                        self.should_reset_accumulation = true;
472                        return true;
473                    }
474                    "a" | "A" => {
475                        self.camera_movement.left =
476                            event.state == winit::event::ElementState::Pressed;
477                        self.should_reset_accumulation = true;
478                        return true;
479                    }
480                    "d" | "D" => {
481                        self.camera_movement.right =
482                            event.state == winit::event::ElementState::Pressed;
483                        self.should_reset_accumulation = true;
484                        return true;
485                    }
486                    "q" | "Q" => {
487                        self.camera_movement.down =
488                            event.state == winit::event::ElementState::Pressed;
489                        self.should_reset_accumulation = true;
490                        return true;
491                    }
492                    "e" | "E" => {
493                        self.camera_movement.up =
494                            event.state == winit::event::ElementState::Pressed;
495                        self.should_reset_accumulation = true;
496                        return true;
497                    }
498                    " " => {
499                        if event.state == winit::event::ElementState::Released {
500                            self.current_params.accumulate = 1 - self.current_params.accumulate;
501                            self.should_reset_accumulation = true;
502                            self.compute_shader
503                                .set_custom_params(self.current_params, &core.queue);
504                            return true;
505                        }
506                    }
507                    _ => {}
508                }
509            }
510        }
511
512        if let WindowEvent::CursorMoved { position, .. } = event {
513            let x = position.x as f32;
514            let y = position.y as f32;
515
516            self.base.handle_mouse_input(core, event, false);
517
518            if self.camera_movement.handle_mouse_movement(x, y) {
519                self.should_reset_accumulation = true;
520                return true;
521            }
522        }
523
524        if let WindowEvent::MouseInput { state, button, .. } = event {
525            if *button == winit::event::MouseButton::Right
526                && *state == winit::event::ElementState::Released
527            {
528                self.camera_movement.toggle_mouse_look();
529                return true;
530            }
531        }
532
533        if let WindowEvent::DroppedFile(path) = event {
534            if let Err(e) = self.base.load_media(core, path) {
535                error!("Failed to load dropped file: {e:?}");
536            }
537            return true;
538        }
539
540        if let WindowEvent::KeyboardInput { event, .. } = event {
541            if self
542                .base
543                .key_handler
544                .handle_keyboard_input(core.window(), event)
545            {
546                return true;
547            }
548        }
549
550        false
551    }

Trait Implementations§

Source§

impl Default for KeyInputHandler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

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().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(value: T, _simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,