pub fn read() -> Result<Event>
Expand description

Reads a single Event.

This function blocks until an Event is available. Combine it with the poll function to get non-blocking reads.

Examples

Blocking read:

use crossterm::{event::read, Result};

fn print_events() -> Result<bool> {
    loop {
        // Blocks until an `Event` is available
        println!("{:?}", read()?);
    }
}

Non-blocking read:

use std::time::Duration;

use crossterm::{event::{read, poll}, Result};

fn print_events() -> Result<bool> {
    loop {
        if poll(Duration::from_millis(100))? {
            // It's guaranteed that `read` wont block, because `poll` returned
            // `Ok(true)`.
            println!("{:?}", read()?);
        } else {
            // Timeout expired, no `Event` is available
        }
    }
}