Processing mouse inputs
Like keyboard events, the Termion crate also supports the ability to listen for mouse events, track the mouse cursor location, and react to it in code. Let's see how to do this here.
Create a new source file called mouse-events.rs
under src/bin
.
Here is the code logic:
- Import the needed modules.
- Enable mouse support in the terminal.
- Clear the screen.
- Create an iterator over incoming events.
- Listen to mouse presses, release and hold events, and display the mouse cursor location on the terminal screen.
The code is explained in snippets corresponding to each of these points.
Let's first look at module imports:
- We're importing the
termion
crate modules for switching to raw mode, detecting the cursor position, and listening to mouse events:use std::io::{self, Write}; use termion::cursor::{self, DetectCursorPos}; use termion::event::*; use termion::input::{MouseTerminal, TermRead}; use termion::raw::IntoRawMode...