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/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
Hide additional 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    }

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 + Send + Sync>

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,