I'm trying to detect key releasing instead of pressing using crossterm. I'm using the basic example named event-stream-tokio. I need tokio runtime for the project.
Link to the example code: https://github.com/crossterm-rs/crossterm/blob/master/examples/event-stream-tokio.rs
Anyway, when I'm trying to amend the example to catch key release and not press - I'm not able to do so. Only the press kind is detected.
Does anybody know how to make it work?
Here is my changed function in that example:
async fn print_events() {
let mut reader = EventStream::new();
loop {
let mut delay = Delay::new(Duration::from_millis(1_000)).fuse();
let mut event = reader.next().fuse();
select! {
_ = delay => { println!(".\r"); },
maybe_event = event => {
match maybe_event {
Some(Ok(event)) => {
println!("Event::{:?}\r", event);
if event == Event::Key(
// Here I changed the example
KeyEvent::new_with_kind(
KeyCode::Char('c'),
KeyModifiers::NONE,
KeyEventKind::Release
)) {
println!("c key was released!. position is: {:?}\r", position());
}
if event == Event::Key(KeyCode::Esc.into()) {
break;
}
}
Some(Err(e)) => println!("Error: {:?}\r", e),
None => break,
}
}
};
}
}
Printing the position of the cursor is not important. It's only part of their example.
Thanks!
CodePudding user response:
The default behavior of terminals is that what you get is not a stream of key state events but a stream of text — of characters typed. There is no concept of key-up.
However, the kitty keyboard protocol is a set of extensions which some terminals support to allow reporting key-up events, and that is what crossterm
's KeyEventKind
is about. You have to enable the features by using PushKeyboardEnhancementFlags
, and you have to be using a terminal that implements that extension. Otherwise you will never see a release event.