Skip to content

Commit fc9bf49

Browse files
authored
fix(windows): handle spurious key events in command picker (voidzero-dev#1100)
## Problem On Windows, running `vp` directly in Windows Terminal (PowerShell or CMD) causes the command picker to immediately receive an Enter key Release event, which incorrectly triggers the `create` subcommand without user input. This issue does **not** occur in VS Code Terminal. ![PowerShell](https://github.com/user-attachments/assets/ae63172a-6fb2-4738-be3a-95134b23b948) Additionally, when running under `conhost.exe`, user input characters are duplicated. ![conhost](https://github.com/user-attachments/assets/50832218-e3c1-42d8-a85f-c1b0859ef968) ## Root Cause This is caused by a known Windows Terminal issue that key events are generated simultaneously. ## Fix Check `KeyEventKind` before `handle_key_event` ## Related Issues microsoft/terminal#8440 crossterm-rs/crossterm#978
1 parent c75d8df commit fc9bf49

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

crates/vite_global_cli/src/command_picker.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use crossterm::{
99
cursor,
10-
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
10+
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
1111
execute,
1212
style::{Attribute, Print, ResetColor, SetAttribute, SetForegroundColor},
1313
terminal::{self, ClearType},
@@ -196,25 +196,28 @@ fn run_picker(command_order: &[usize]) -> io::Result<Option<PickedCommand>> {
196196
}
197197

198198
match event::read() {
199-
Ok(Event::Key(KeyEvent { code, modifiers, .. })) => {
200-
match handle_key_event(
201-
code,
202-
modifiers,
203-
&mut query,
204-
&mut selected_position,
205-
filtered_indices.len(),
206-
) {
207-
ControlFlow::Continue(()) => continue,
208-
ControlFlow::Break(Some(())) => {
209-
let Some(index) = filtered_indices.get(selected_position).copied() else {
210-
continue;
211-
};
212-
break Ok(Some(PickedCommand {
213-
command: COMMANDS[index].command,
214-
append_help: COMMANDS[index].append_help,
215-
}));
199+
Ok(Event::Key(KeyEvent { code, modifiers, kind, .. })) => {
200+
if kind == KeyEventKind::Press {
201+
match handle_key_event(
202+
code,
203+
modifiers,
204+
&mut query,
205+
&mut selected_position,
206+
filtered_indices.len(),
207+
) {
208+
ControlFlow::Continue(()) => continue,
209+
ControlFlow::Break(Some(())) => {
210+
let Some(index) = filtered_indices.get(selected_position).copied()
211+
else {
212+
continue;
213+
};
214+
break Ok(Some(PickedCommand {
215+
command: COMMANDS[index].command,
216+
append_help: COMMANDS[index].append_help,
217+
}));
218+
}
219+
ControlFlow::Break(None) => break Ok(None),
216220
}
217-
ControlFlow::Break(None) => break Ok(None),
218221
}
219222
}
220223
Ok(_) => continue,

0 commit comments

Comments
 (0)