Skip to content

fix(event): make KeyEvent ordering agree with its equality - #1159

Open
NgoQuocViet2001 wants to merge 1 commit into
crossterm-rs:masterfrom
NgoQuocViet2001:fix-key-event-ordering
Open

NgoQuocViet2001 wants to merge 1 commit into
crossterm-rs:masterfrom
NgoQuocViet2001:fix-key-event-ordering

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown

KeyEvent compares and hashes its case-normalized form (#541), so KeyEvent::new(Char('D'), NONE), (Char('D'), SHIFT) and (Char('d'), SHIFT) are all equal. PartialOrd and Ord were still derived on the raw fields, so ('D', NONE).cmp(&('D', SHIFT)) is Less and ('d', SHIFT).cmp(&('D', SHIFT)) is Greater. A BTreeMap<KeyEvent, _> holding KeyCode::Char('D').into() returns None for the ('D', SHIFT) event reported for Shift+d, while a HashMap finds it. Event::Key inherits this through its derived Ord.

This implements Ord on the same normalized fields that eq and hash use, in the old derive's field order, and makes PartialOrd delegate to it. Ord stays, since #951 added it for ordered keybinding maps, and the change is not breaking. Events that normalize_case leaves unchanged keep their relative order. ('d', SHIFT) and ('D', NONE) now sort together with the equal ('D', SHIFT). The fields are destructured exhaustively, as in eq and hash, so a new field such as the one in #1074 must be added to cmp too.

Ran:

  • cargo test --locked --lib --no-default-features --features events -- --test-threads 1 key_event: the new test fails on master (Greater vs Equal) and passes with the fix
  • cargo test --locked --all-targets --all-features -- --test-threads 1: all pass (93 lib tests)
  • cargo test --locked --doc --all-features: 63 passed

Checked:

  • cargo clippy --locked --all-targets --all-features -- -D warnings and the same with --no-default-features --features events: clean
  • cargo fmt --all -- --check and RUSTDOCFLAGS='-D warnings' cargo doc --locked --all-features --no-deps: clean

`KeyEvent` compares and hashes its case-normalized form, so
`Char('D')`, `Char('D')` with `SHIFT` and `Char('d')` with `SHIFT` are
equal. `PartialOrd` and `Ord` were still derived and compared the raw
fields, so those equal events ordered as `Less` or `Greater`, and a
`BTreeMap` or `BTreeSet` keyed by `KeyEvent` or `Event` missed
Shift+letter bindings that a `HashMap` found.

Implement `Ord` on the same normalized fields, in the same field order
as the old derive, and have `PartialOrd` delegate to it. `Event`'s
derived ordering picks this up for `Event::Key`.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused change aligns ordering with equality and hashing, preserves field precedence, and includes regression coverage with no blocking issues identified.

No open issues 🎉

What changed in this PR

This PR aligns KeyEvent ordering with its existing case-normalized equality and hashing, fixing ordered keybinding lookups while preserving ordering for unchanged events.

Changes:

  • Replace derived ordering with normalized field comparison, with PartialOrd delegating to Ord.
  • Add regression coverage for equivalent keys, Event::Key ordering, and BTreeSet lookups.
  • Document the fix in the changelog.
File Description
src/​event/​key_event.rs Implement consistent ordering and add regression tests.
CHANGELOG.md Describe the ordering mismatch and affected keybinding lookups.

Review effort: Balanced

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

2 participants