Repository: esp-rs/espflash (language: rust)

Issue:
Serial Monitor : Data is sent both for key press and release events
When trying out a simple serial echo with my new esp32, I found out it received twice the letters I typed when using espflash monitor.
However, when using PuTTY, it only received them once.

Using the original windows console allowed me to pinpoint the issue, keys are sent for press and for release event.
Adding a simple `dbg!(key)` here https://github.com/esp-rs/espflash/blob/45946f84e0b54043f4ac63bd520adccd36bf63a0/espflash/src/cli/monitor/mod.rs#L117-L124 showed me this (the "Read" are my echo program running on the esp)
```
[espflash\src\cli\monitor\mod.rs:119:5] &key = KeyEvent {
    code: Char(
        's',
    ),
    modifiers: KeyModifiers(
        0x0,
    ),
    kind: Press,
    state: KeyEventState(
        0x0,
    ),
}
Read: 's'
[espflash\src\cli\monitor\mod.rs:119:5] &key = KeyEvent {
    code: Char(
        's',
    ),
    modifiers: KeyModifiers(
        0x0,
    ),
    kind: Release,
    state: KeyEventState(
        0x0,
    ),
}
Read: 's'
```

I am not sure if this is intended, this can be fixed simply by checking if it is a key press or a key release.

I also want to add that this was difficult to find because inside some terminals, like the RustRover terminal or the new windows terminal, key presses were sending press and release instantaneously, even if the key was not yet released. The old windows terminal however correctly waited for me to release the key before sending the release event. Maybe this is due to a bug in an upstream library ?


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ffc13d6e..514ab220 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed
 - Downgrade crossterm and update time crates (#659)
+- Monitor now only sends key presses on key down events
 
 ### Changed
 
diff --git a/espflash/src/cli/monitor/mod.rs b/espflash/src/cli/monitor/mod.rs
index 08f55bf4..1d733639 100644
--- a/espflash/src/cli/monitor/mod.rs
+++ b/espflash/src/cli/monitor/mod.rs
@@ -15,6 +15,7 @@ use std::{
     time::Duration,
 };
 
+use crossterm::event::KeyEventKind;
 use crossterm::{
     event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
     terminal::{disable_raw_mode, enable_raw_mode},
@@ -116,20 +117,22 @@ pub fn monitor(
 
         if interactive_mode && poll(Duration::from_secs(0)).into_diagnostic()? {
             if let Event::Key(key) = read().into_diagnostic()? {
-                if key.modifiers.contains(KeyModifiers::CONTROL) {
-                    match key.code {
-                        KeyCode::Char('c') => break,
-                        KeyCode::Char('r') => {
-                            reset_after_flash(&mut serial, pid).into_diagnostic()?;
-                            continue;
+                if key.kind == KeyEventKind::Press {
+                    if key.modifiers.contains(KeyModifiers::CONTROL) {
+                        match key.code {
+                            KeyCode::Char('c') => break,
+                            KeyCode::Char('r') => {
+                                reset_after_flash(&mut serial, pid).into_diagnostic()?;
+                                continue;
+                            }
+                            _ => {}
                         }
-                        _ => {}
                     }
-                }
 
-                if let Some(bytes) = handle_key_event(key) {
-                    serial.write_all(&bytes).into_diagnostic()?;
-                    serial.flush().into_diagnostic()?;
+                    if let Some(bytes) = handle_key_event(key) {
+                        serial.write_all(&bytes).into_diagnostic()?;
+                        serial.flush().into_diagnostic()?;
+                    }
                 }
             }
         }


Test patch: