crossterm_input 0.2.0

A cross-platform library for reading userinput.

Crossterm Input | cross-platform input reading .

Lines of Code Latest Version MIT docs

This crate allows you to read the user input cross-platform. It supports all UNIX and windows terminals down to windows 7 (not all terminals are tested see Tested Terminals for more info)

This crate is a sub-crate of crossterm to read the user input and can be used individually.

Other sub-crates are:

When you want to use other modules as well you might want to use crossterm with feature flags.

Table of contents:

Getting Started

This documentation is only for crossterm_input version 0.2 if you have an older version I suggest you check the Upgrade Manual. Also, check out the examples folders with detailed examples for all functionalities of this crate.

Add the crossterm_input package to your Cargo.toml file.

[dependencies]
`crossterm_input` = "0.2"

And import the crossterm_input modules you want to use.

extern crate crossterm_input;

pub use crossterm_input::{input, AsyncReader, KeyEvent, TerminalInput};

Useful Links

Features

These are the features of this crate:

  • Cross-platform
  • Everything is multithreaded (Send, Sync)
  • Detailed documentation on every item
  • Input
    • Read character
    • Read line
    • Read key input events async / sync (ALT + Key, CTRL + Key, FN, Arrows, ESC, BackSpace, HOME, DELETE. INSERT, PAGEUP/DOWN, and more)
    • Read mouse input events (Press, Release, Position, Button)

Examples

The examples folder has more complete and verbose examples, please have a look at that as well.

Good documentation could be found in the docs as well in the book.

available imports

use crossterm_input::{
    input, InputEvent, KeyEvent, MouseButton, MouseEvent, TerminalInput, AsyncReader, SyncReader, Screen
};

Simple Readings

let mut input = input();

 match input.read_char() {
    Ok(s) => println!("char typed: {}", s),
    Err(e) => println!("char error : {}", e),
 }
 
 match input.read_line() {
     Ok(s) => println!("string typed: {}", s),
     Err(e) => println!("error: {}", e),
 }

Read input events synchronously or asynchronously.

// make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's self and allows crossterm to read the input and pass it back to you.
let screen = Screen::new(true);
    
let mut input = input();

// either read the input synchronously 
let stdin = input.read_sync();
 
// or asynchronously
let stdin = input.read_async();

if let Some(key_event) = stdin.next() {
     match key_event {
         InputEvent::Keyboard(event: KeyEvent) => matche vent { /* check key event */ }
         InputEvent::Mouse(event: MouseEvent) => match event { /* check mouse event */ }
     }
 }

Enable mouse input events.

let input = input();

// enable mouse events to be captured.
input.enable_mouse_mode().unwrap();

// disable mouse events to be captured.
input.disable_mouse_mode().unwrap();

Tested terminals

  • Windows Powershell
    • Windows 10 (pro)
  • Windows CMD
    • Windows 10 (pro)
    • Windows 8.1 (N)
  • Ubuntu Desktop Terminal
    • Ubuntu 17.10
  • (Arch, Manjaro) KDE Konsole
  • Linux Mint

This crate supports all Unix terminals and windows terminals down to Windows 7 but not all of them have been tested. If you have used this library for a terminal other than the above list without issues feel free to add it to the above list, I really would appreciate it.

Notice

This library is average stable now, I don't expect it to not to change that much. If there are any changes that will affect previous versions I will describe what to change to upgrade.

Contributing

I highly appreciate it when you are contributing to this crate. Also Since my native language is not English my grammar and sentence order will not be perfect. So improving this by correcting these mistakes will help both me and the reader of the docs.

Authors

  • Timon Post - Project Owner & creator
  • Dave Ho - Contributor

License

This project is licensed under the MIT License - see the LICENSE.md file for details