Function crossterm::event::read

source ·
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;
use std::io;

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

Non-blocking read:

use std::time::Duration;
use std::io;

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

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