Module gilrs::ev::filter

source ·
Expand description

Alter events in various ways.

This modules contains “event filters” that can change, drop or create new events. To use them, import Filter trait and call filter() function on Option<Event>. Because filter also returns Option<Event> you can combine multiple filters by using filter() function on returned event.

Filters in this modules have public fields that can be used to configure their behaviour. You can also create them with default values using new() method. If filter is not configurable, it is implemented as function (for example deadzone()).

§Example

use gilrs::{GilrsBuilder, Filter};
use gilrs::ev::filter::{Jitter, Repeat, deadzone};

let mut gilrs = GilrsBuilder::new().with_default_filters(false).build().unwrap();
let jitter = Jitter { threshold: 0.02 };
let repeat = Repeat::new();

// Event loop
loop {
    while let Some(event) = gilrs
        .next_event()
        .filter_ev(&jitter, &mut gilrs)
        .filter_ev(&deadzone, &mut gilrs)
        .filter_ev(&repeat, &mut gilrs)
    {
        gilrs.update(&event);
        println!("{:?}", event);
    }
}

§Implementing custom filters

If you want to implement your own filters, you will have to implement FilterFn trait. Do not return None if you got Some(event). If you want to discard an event, uses EventType::Dropped. Returning None means that there are no more events to process and will end while let loop.

§Example

Example implementations of filter that will drop all events with Unknown axis or button.

use gilrs::ev::filter::FilterFn;
use gilrs::{Gilrs, Event, EventType, Button, Axis, Filter};

struct UnknownSlayer;

impl FilterFn for UnknownSlayer {
    fn filter(&self, ev: Option<Event>, _gilrs: &mut Gilrs) -> Option<Event> {
        match ev {
            Some(Event { event: EventType::ButtonPressed(Button::Unknown, ..), id, .. })
            | Some(Event { event: EventType::ButtonReleased(Button::Unknown, ..), id, .. })
            | Some(Event { event: EventType::AxisChanged(Axis::Unknown, ..), id, .. })
            => Some(Event::new(id, EventType::Dropped)),
            _ => ev,
        }
    }
}

FilterFn is also implemented for all Fn(Option<Event>, &Gilrs) -> Option<Event>, so above example could be simplified to passing closure to filter() function.

Structs§

  • Discard axis events that changed less than threshold.
  • Repeats pressed keys.

Traits§

Functions§