[][src]Struct crossterm_input::AsyncReader

pub struct AsyncReader { /* fields omitted */ }

An asynchronous input reader (not blocking).

AsyncReader implements the Iterator trait. Documentation says:

An iterator has a method, next, which when called, returns an Option<Item>. next will return Some(Item) as long as there are elements, and once they've all been exhausted, will return None to indicate that iteration is finished. Individual iterators may choose to resume iteration, and so calling next again may or may not eventually start returning Some(Item) again at some point.

AsyncReader is an individual iterator and it doesn't use None to indicate that the iteration is finished. You can expect additional Some(InputEvent) after calling next even if you have already received None.

Notes

  • It requires enabled raw mode (see the crossterm_screen crate documentation to learn more).
  • A thread is spawned/reused to read the input.
  • The reading thread is cleaned up when you drop the AsyncReader.
  • See the SyncReader if you want a blocking, or a less resource hungry reader.

Examples

use std::{thread, time::Duration};

use crossterm_input::{input, InputEvent, KeyEvent, RawScreen};

fn main() {
    println!("Press 'ESC' to quit.");

    // Enable raw mode and keep the `_raw` around otherwise the raw mode will be disabled
    let _raw = RawScreen::into_raw_mode();

    // Create an input from our screen
    let input = input();

    // Create an async reader
    let mut reader = input.read_async();

    loop {
        if let Some(event) = reader.next() { // Not a blocking call
            match event {
                InputEvent::Keyboard(KeyEvent::Esc) => {
                    println!("Program closing ...");
                    break;
                 }
                 InputEvent::Mouse(event) => { /* Mouse event */ }
                 _ => { /* Other events */ }
            }
        }
        thread::sleep(Duration::from_millis(50));
    }
} // `reader` dropped <- thread cleaned up, `_raw` dropped <- raw mode disabled

Methods

impl AsyncReader[src]

pub fn stop(&mut self)[src]

Stops the input reader.

Notes

  • You don't need to call this method, because it will be automatically called when the AsyncReader is dropped.

Trait Implementations

impl Iterator for AsyncReader[src]

type Item = InputEvent

The type of the elements being iterated over.

fn next(&mut self) -> Option<Self::Item>[src]

Tries to read the next input event (not blocking).

None doesn't mean that the iteration is finished. See the AsyncReader documentation for more information.

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]