Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event::read incorrectly returns Esc key press event while scrolling the mouse wheel #854

Open
unixzii opened this issue Jan 6, 2024 · 1 comment

Comments

@unixzii
Copy link

unixzii commented Jan 6, 2024

Describe the bug
event::read sometimes returns Esc key press event while scrolling the mouse wheel very fast. This will likely happen on macOS if the user is using a trackpad (which can produce high frequency events because of the inertia simulation).

Precondition:
The terminal is in a mode that translates mouse wheel events into arrow key events.

To Reproduce
The minimal reproducible example:

use anyhow::Result;
use crosstermion::terminal::AlternateRawScreen;

fn main() -> Result<()> {
    // Help to get into alternative screen mode, this should not
    // affect the final result.
    let _alt_screen = AlternateRawScreen::try_from(io::stderr())?;

    while let Ok(event) = crossterm::event::read() {
        match event {
            crossterm::event::Event::Key(key) => {
                if key.code == crossterm::event::KeyCode::Esc {
                    panic!("wrong event received");
                }
            }
            _ => {}
        }
    }
}

Steps to reproduce the behavior:

  1. Build and run the example program.
  2. Scroll very fast using the trackpad (or Logitech MX Master mouse, which will also work).
  3. See the program panic.

Expected behavior
The program should not receive Esc key press events when the user didn't press Esc key.

OS
Verified on macOS 13.6

Terminal/Console

  • Warp
  • Visual Studio Code terminal
@unixzii
Copy link
Author

unixzii commented Jan 6, 2024

I noticed that the parser parsed Esc key press because the tty buffer is insufficient.
image

The byte sequence read from stdin in one poll ends with 27, which is cut off. Maybe there are some bugs in the terminal emulator that the control sequence is not written atomically. Or the operating system doesn't give us the whole buffer at once.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant