Skip to content

Commit

Permalink
fix: ignore release and repeat key events
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Jun 7, 2023
1 parent aadbd54 commit 7821c98
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use test::{results::Results, Test};

use crossterm::{
self, cursor,
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
event::{self, Event, KeyCode, KeyEvent, KeyModifiers, KeyEventKind},
execute, terminal,
};
use rand::{seq::SliceRandom, thread_rng};
Expand Down Expand Up @@ -220,11 +220,13 @@ fn main() -> crossterm::Result<()> {
match event {
Event::Key(KeyEvent {
code: KeyCode::Char('c'),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::CONTROL,
..
}) => break,
Event::Key(KeyEvent {
code: KeyCode::Esc,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
..
}) => match state {
Expand All @@ -248,6 +250,7 @@ fn main() -> crossterm::Result<()> {
State::Results(_) => match event {
Event::Key(KeyEvent {
code: KeyCode::Char('r'),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
..
}) => {
Expand All @@ -257,6 +260,7 @@ fn main() -> crossterm::Result<()> {
}
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
..
}) => break,
Expand Down
6 changes: 5 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod results;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, KeyEventKind};
use std::fmt;
use std::time::Instant;

Expand Down Expand Up @@ -53,6 +53,10 @@ impl Test {
}

pub fn handle_key(&mut self, key: KeyEvent) {
if key.kind != KeyEventKind::Press {
return;
}

let word = &mut self.words[self.current_word];
match key.code {
KeyCode::Char(' ') | KeyCode::Enter => {
Expand Down

0 comments on commit 7821c98

Please sign in to comment.