Skip to content

Commit ec63a6c

Browse files
committed
fix(ui): ignore key release events so keys do not fire twice on Windows
On Windows crossterm reports both a Press and a Release event for a single physical keypress ("Add key release event for windows", crossterm 0.26.0+). handle_event dispatched every Event::Key without checking KeyEventKind, so each tap ran its action twice: pressing Space paused and then immediately resumed playback, making manual playback control unusable. Every other key was affected the same way (h/l stepped two lines, q quit twice, etc.). Filter on the press at the single dispatch point, which covers all keys. Linux and macOS are unaffected because they do not emit release events. Verified on Windows 10 25H2 by driving a real ConPTY session: one physical 'p' keypress produced both a Press and a Release event. A differential test that sends a single Space and measures output for the following second shows 32,803 bytes of animation continuing without the fix vs 126 bytes (staying paused) with it. Fixes #250
1 parent e756f0e commit ec63a6c

1 file changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/ui.rs‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,13 @@ impl<'a> UI<'a> {
565565
fn handle_event(&mut self, event: Event) {
566566
self.force_redraw = true;
567567
if let Event::Key(key) = event {
568-
self.handle_key_event(key);
568+
// On Windows, crossterm emits both a Press and a Release event for a single
569+
// physical keypress ("Add key release event for windows", crossterm 0.26.0+).
570+
// Acting on both makes every key fire twice, so e.g. Space pauses and then
571+
// immediately resumes. Only act on the initial press.
572+
if key.kind == event::KeyEventKind::Press {
573+
self.handle_key_event(key);
574+
}
569575
}
570576
}
571577

@@ -1266,6 +1272,69 @@ mod tests {
12661272
assert!(rendered.contains("Exit"));
12671273
}
12681274

1275+
/// Regression test for key release events on Windows.
1276+
///
1277+
/// crossterm reports both a Press and a Release event for a single physical
1278+
/// keypress on Windows, so acting on every event makes one tap fire an action
1279+
/// twice. A single Space would pause and then instantly resume.
1280+
#[test]
1281+
fn key_release_events_must_not_trigger_a_second_action() {
1282+
let mut ui = test_ui();
1283+
assert_eq!(ui.playback_state, PlaybackState::Playing);
1284+
1285+
let press = event::KeyEvent::new_with_kind(
1286+
KeyCode::Char(' '),
1287+
KeyModifiers::NONE,
1288+
event::KeyEventKind::Press,
1289+
);
1290+
let release = event::KeyEvent::new_with_kind(
1291+
KeyCode::Char(' '),
1292+
KeyModifiers::NONE,
1293+
event::KeyEventKind::Release,
1294+
);
1295+
1296+
ui.handle_event(Event::Key(press));
1297+
assert_eq!(ui.playback_state, PlaybackState::Paused);
1298+
1299+
// The release of the same physical keypress must be ignored, otherwise
1300+
// playback resumes immediately.
1301+
ui.handle_event(Event::Key(release));
1302+
assert_eq!(ui.playback_state, PlaybackState::Paused);
1303+
}
1304+
1305+
#[test]
1306+
fn key_release_events_must_not_quit_the_app() {
1307+
let mut ui = test_ui();
1308+
let release = event::KeyEvent::new_with_kind(
1309+
KeyCode::Char('q'),
1310+
KeyModifiers::NONE,
1311+
event::KeyEventKind::Release,
1312+
);
1313+
1314+
ui.handle_event(Event::Key(release));
1315+
assert_ne!(ui.state, UIState::Finished);
1316+
}
1317+
1318+
#[test]
1319+
fn key_release_events_must_not_step_an_extra_line() {
1320+
let mut ui = test_ui();
1321+
let press = event::KeyEvent::new_with_kind(
1322+
KeyCode::Char('l'),
1323+
KeyModifiers::NONE,
1324+
event::KeyEventKind::Press,
1325+
);
1326+
let release = event::KeyEvent::new_with_kind(
1327+
KeyCode::Char('l'),
1328+
KeyModifiers::NONE,
1329+
event::KeyEventKind::Release,
1330+
);
1331+
1332+
ui.handle_event(Event::Key(press));
1333+
let after_press = ui.playback_state;
1334+
ui.handle_event(Event::Key(release));
1335+
assert_eq!(ui.playback_state, after_press);
1336+
}
1337+
12691338
#[test]
12701339
fn handle_event_dispatches_keys_and_ignores_non_key_events() {
12711340
let mut ignored_ui = test_ui();

0 commit comments

Comments
 (0)