Skip to main content

ButtonEvents

Struct ButtonEvents 

Source
pub struct ButtonEvents { /* private fields */ }
Expand description

A stream of top button press and release events.

Subscribers should generally poll for events frequently as events are stored in a circular buffer and a subscriber can lose events if too many are generated before being polled.

See also the following alternative ways to get the pressed/released state of buttons:

Implementations§

Source§

impl ButtonEvents

Source

pub fn subscribe() -> ButtonEvents

Subscribe to all button events that take place after this call to subscribe.

Events before subscribe will not be delivered.

§Panics

This function should ordinarily never panic, but may if the library has not been initialized.

§Examples
let mut button_events = ButtonEvents::subscribe();
Source

pub async fn next(&mut self) -> ButtonEvent

Returns a Future yielding the next ButtonEvent.

§Panics

This function should ordinarily never panic, but will if it is called after the internal broadcast::Receiver is closed.

Source

pub async fn next_or_output<F>( &mut self, future: F, ) -> Result<ButtonEvent, <F as Future>::Output>
where F: Future,

Waits for either a ButtonEvent to be received, or for future to complete, and returns whichever is received first.

§Errors

This function will return Err(F::Output) if future completes before a ButtonEvent is received.

§Examples
let mut button_events = ButtonEvents::subscribe();
let my_future = async {
    sleep_ms(100).await;
    "there have been no button events for 100ms"
};

match button_events.next_or_output(my_future).await {
    Ok(event) => println!("{event:?}"),
    Err(f_out) => println!("{f_out}"),
}
Source

pub async fn next_or_timeout_after( &mut self, timeout: Duration, ) -> Option<ButtonEvent>

Returns Some if self receives an event before timeout elapses. Otherwise, returns None.

Source

pub async fn next_or_completed<F>(&mut self, future: F) -> Option<ButtonEvent>
where F: Future,

Runs future while waiting for a new ButtonEvent, and returns Some if an event occurs before future completes. Otherwise, this function returns None.

Source

pub async fn peek_next_or_output<F>( &mut self, future: F, ) -> Result<ButtonEvent, <F as Future>::Output>
where F: Future,

The same as ButtonEvents::next_or_output, but does not consume an event if one is received.

§Errors

This function will return Err(F::Output) if future completes before a ButtonEvent is received.

§Examples
let mut button_events = ButtonEvents::subscribe();
let my_future = async {
    sleep_ms(100).await;
    "there have been no button events for 100ms"
};

match button_events.peek_next_or_output(my_future).await {
    Ok(event) => {
        println!("{event:?}");
        // We know there's an event here; unwrap it.
        let ev = button_events.try_next().unwrap();
        println!("{ev:?}");
        assert_eq!(event == ev);
    },
    Err(f_out) => println!("{f_out}"),
}
Source

pub async fn peek_next_or_timeout_after( &mut self, timeout: Duration, ) -> Option<ButtonEvent>

Like next_or_timeout_after, but peeks the event instead of consuming it.

Source

pub async fn peek_next_or_completed<F>( &mut self, future: F, ) -> Option<ButtonEvent>
where F: Future,

Cancels the future if a button event is generated.

Source

pub async fn peek_next(&mut self) -> ButtonEvent

Waits for the next ButtonEvent and returns it without consuming it. Repeated calls to this function will have the same return value.

See also ButtonEvents::try_peek_next

Source

pub fn try_next(&mut self) -> Option<ButtonEvent>

Returns the next ButtonEvent if one has been received, otherwise returns None.

§Panics

This function will panic if the underlying broadcast::Receiver is closed.

Source

pub fn try_peek_next(&mut self) -> Option<ButtonEvent>

Returns Some if an event has been received, without consuming it or waiting. Otherwise returns None.

See also ButtonEvents::peek_next

Source

pub fn clear_pending(&mut self)

Clears all pending button events.

Any ButtonEvent returned from a call to next, peek_next, etc via self are guaranteed to have been received after this function returns.

Source

pub fn only_report_press_events(&mut self)

Only press events will be returned from next.

Released events will still be relected in the state of currently_pressed.

Source

pub fn report_press_and_release_events(&mut self)

Report both press and release events.

Source

pub fn only_report_release_events(&mut self)

Only release events will be returned from next.

Source

pub fn ignore_presses_within(&mut self, duration: Duration)

If one or more press events (not release events) follows a press event within duration, do not report them.

Source

pub fn remove_press_within_filter(&mut self)

Reset to the default behavior of not ignoring press events within a duration specified to ignore_presses_within.

Source

pub fn filter_by_buttons(&mut self, buttons: Buttons)

Filter out events that don’t come from a button in buttons.

Source

pub fn remove_button_filter(&mut self)

Stop filtering by button. This is the same as filter_by_buttons(Buttons::all()).

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V