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

Duplicate Key Events on Windows #347

Closed
dev-ardi opened this issue Jul 26, 2023 · 7 comments
Closed

Duplicate Key Events on Windows #347

dev-ardi opened this issue Jul 26, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@dev-ardi
Copy link

dev-ardi commented Jul 26, 2023

Issue summary

On Windows, Crossterm will report both KeyPress and KeyRelease events. To avoid registering an event twice, ensure that you check the kind field of the KeyEvent. See also:

Description

When I type any keyboard input the event fires twice: Once on press and once on release.

2023-07-26.06-29-42.mp4

To Reproduce

https://github.com/fdehau/tui-rs/blob/master/examples/user_input.rs

Environment

  • OS: Windows
  • Terminal Emulator: conhost, alacritty
  • Crate version: ratatui = "0.22.0"
  • Backend: crossterm

Additional context

It works for some reason in another project where it was failing until I refactored the display logic to another function

@dev-ardi dev-ardi added the bug Something isn't working label Jul 26, 2023
@joshka
Copy link
Member

joshka commented Jul 26, 2023

Hi there. This has come up a few times. See https://docs.rs/crossterm/latest/crossterm/event/struct.KeyEvent.html
To fix this, check KeyEvent.kind == KeyEventKind.Press (on windows).

That's a probably a difficult one as ratatui doesn't have any code to handle keyboard input (other than our examples). Can you help us out (and perhaps the next person who hits this) by noting where in the docs you looked for this and didn't find enough info?

@dev-ardi
Copy link
Author

Honestly the only docs I read were the readme.md and the examples, and also the relevant data structures and methods that I used.

But the examples are wrong on windows though!

@mindoodoo
Copy link
Member

But the examples are wrong on windows though!

Could you elaborate on why they are wrong ?

@dev-ardi
Copy link
Author

Well the expected behaviour would be that the down key only moves down by one but it moves down by two!

@dev-ardi
Copy link
Author

Ah, I'm very dumb. I took the example from tui-rs not ratatui. I'm sorry about that!

@mindoodoo
Copy link
Member

So that fixes your issue ?

@dev-ardi
Copy link
Author

dev-ardi commented Jul 27, 2023

Yes. tui-rs should be officially deprecated at this point...

@joshka joshka changed the title Input gets duplicated (event fires twice) Crossterm / Windows sees key events twice Aug 29, 2023
@joshka joshka pinned this issue Aug 29, 2023
@joshka joshka changed the title Crossterm / Windows sees key events twice Duplicate Key Events on Windows Aug 29, 2023
@kdheepak kdheepak unpinned this issue Sep 1, 2023
@joshka joshka pinned this issue Sep 1, 2023
joshka added a commit to joshka/csvlens that referenced this issue Dec 31, 2023
Crossterm 0.26+ reports key release events on windows, which results in
the application seeing multiple events for a single key press.

See ratatui-org/ratatui#347 for more info.

Fixes YS-L#30
YS-L pushed a commit to YS-L/csvlens that referenced this issue Dec 31, 2023
* Migrate to Ratatui

Ratatui is a replacement for tui-rs that is actively maintained.
See https://ratatui.rs/ for more info.

* fix: only respond to key press events

Crossterm 0.26+ reports key release events on windows, which results in
the application seeing multiple events for a single key press.

See ratatui-org/ratatui#347 for more info.

Fixes #30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants