Skip to content

Commit

Permalink
fix: only respond to key press events
Browse files Browse the repository at this point in the history
Crossterm 0.26+ reports key release events on windows, which results in
the application seeing multiple events for a single key press.

See ratatui/ratatui#347 for more info.

Fixes #30
  • Loading branch information
joshka committed Dec 31, 2023
1 parent a8a978e commit 3ce170a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/util/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::{Duration, Instant};

use crossterm::event::{poll, read, Event, KeyCode, KeyEvent};
use crossterm::event::{poll, read, Event, KeyCode, KeyEvent, KeyEventKind};

pub enum CsvlensEvent<I> {
Input(I),
Expand Down Expand Up @@ -42,16 +42,17 @@ impl CsvlensEvents {
pub fn next(&self) -> std::io::Result<CsvlensEvent<KeyEvent>> {
let now = Instant::now();
match poll(self.tick_rate) {
Ok(true) => {
if let Event::Key(event) = read()? {
Ok(true) => match read()? {
Event::Key(event) if event.kind == KeyEventKind::Press => {
Ok(CsvlensEvent::Input(event))
} else {
}
_ => {
let time_spent = now.elapsed();
let rest = self.tick_rate - time_spent;

Self { tick_rate: rest }.next()
}
}
},
Ok(false) => Ok(CsvlensEvent::Tick),
Err(_) => todo!(),
}
Expand Down

0 comments on commit 3ce170a

Please sign in to comment.