Skip to content

Commit

Permalink
Merge pull request #110 from viruscamp/ignore-key-release
Browse files Browse the repository at this point in the history
fix #109 typing twice in windows with crossterm-0.26+
  • Loading branch information
sigmaSd committed Mar 2, 2023
2 parents 08f32cb + 696a8d6 commit 6297477
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/irust/src/irust.rs
Expand Up @@ -179,6 +179,10 @@ impl IRust {
self.execute(Command::HandleCtrlC)?;
}
Event::Key(key_event) => match key_event {
KeyEvent {
kind: KeyEventKind::Release,
..
} => (),
KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
Expand Down
14 changes: 13 additions & 1 deletion crates/irust/src/irust/engine.rs
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, io::Write};

use crossterm::{
cursor::SetCursorStyle,
event::{Event, KeyCode, KeyEvent, KeyModifiers},
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
style::Color,
terminal::ClearType,
};
Expand Down Expand Up @@ -369,6 +369,10 @@ impl IRust {

if let Ok(key_event) = crossterm::event::read() {
match key_event {
Event::Key(KeyEvent {
kind: KeyEventKind::Release,
..
}) => continue,
Event::Key(KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
Expand Down Expand Up @@ -461,6 +465,10 @@ impl IRust {

if let Ok(key_event) = crossterm::event::read() {
match key_event {
Event::Key(KeyEvent {
kind: KeyEventKind::Release,
..
}) => (),
Event::Key(KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
Expand Down Expand Up @@ -695,6 +703,10 @@ impl IRust {
// 1 - wait for the macro key
let macro_key = loop {
match crossterm::event::read()? {
Event::Key(KeyEvent {
kind: KeyEventKind::Release,
..
}) => (),
Event::Key(KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
Expand Down
6 changes: 5 additions & 1 deletion crates/irust/src/utils.rs
Expand Up @@ -263,13 +263,17 @@ fn _balanced_quotes(s: &str) -> bool {
}

pub fn ctrlc_cancel(process: &mut std::process::Child) -> Result<()> {
use crossterm::event::{Event, KeyCode, KeyEvent};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
// Running a command as Command::new().output takes at minimum 1ms
// So Polling should take a similar order of magnitude
if let Ok(event) = crossterm::event::poll(std::time::Duration::from_millis(1)) {
if event {
if let Ok(event) = crossterm::event::read() {
match event {
Event::Key(KeyEvent {
kind: KeyEventKind::Release,
..
}) => (),
Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: crossterm::event::KeyModifiers::CONTROL,
Expand Down
6 changes: 5 additions & 1 deletion crates/printer/examples/shell.rs
@@ -1,4 +1,4 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use crossterm::style::Color;
use printer::{
buffer::Buffer,
Expand All @@ -17,6 +17,10 @@ fn main() -> Result<()> {
let inp = crossterm::event::read()?;
match inp {
crossterm::event::Event::Key(key) => match key {
KeyEvent {
kind: KeyEventKind::Release,
..
} => (),
KeyEvent {
code: KeyCode::Char(c),
modifiers: KeyModifiers::NONE,
Expand Down
4 changes: 4 additions & 0 deletions script_examples/irust_vim_dylib/src/script.rs
Expand Up @@ -34,6 +34,10 @@ impl Vim {

let cmd = (|| match event {
Event::Key(key) => match key {
KeyEvent {
kind: KeyEventKind::Release,
..
} => None,
KeyEvent {
code: KeyCode::Char(c),
modifiers,
Expand Down

0 comments on commit 6297477

Please sign in to comment.