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

Fix parsing of event types/modifiers with certain keys #716

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/event/sys/unix/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,16 @@ pub(crate) fn parse_csi_special_key_code(buffer: &[u8]) -> Result<Option<Interna
// This CSI sequence can be a list of semicolon-separated numbers.
let first = next_parsed::<u8>(&mut split)?;

let modifiers = if let Ok(modifier_mask) = next_parsed::<u8>(&mut split) {
parse_modifiers(modifier_mask)
} else {
KeyModifiers::NONE
};
let (modifiers, kind, state) =
if let Ok((modifier_mask, kind_code)) = modifier_and_kind_parsed(&mut split) {
(
parse_modifiers(modifier_mask),
parse_key_event_kind(kind_code),
parse_modifiers_to_state(modifier_mask),
)
} else {
(KeyModifiers::NONE, KeyEventKind::Press, KeyEventState::NONE)
};

let keycode = match first {
1 | 7 => KeyCode::Home,
Expand All @@ -558,7 +563,9 @@ pub(crate) fn parse_csi_special_key_code(buffer: &[u8]) -> Result<Option<Interna
_ => return Err(could_not_parse_event_error()),
};

let input_event = Event::Key(KeyEvent::new(keycode, modifiers));
let input_event = Event::Key(KeyEvent::new_with_kind_and_state(
keycode, modifiers, kind, state,
));

Ok(Some(InternalEvent::Event(input_event)))
}
Expand Down Expand Up @@ -1364,4 +1371,24 @@ mod tests {
)))),
);
}

#[test]
fn test_parse_csi_numbered_escape_code_with_types() {
assert_eq!(
parse_event(b"\x1B[5;1:3~", false).unwrap(),
Some(InternalEvent::Event(Event::Key(KeyEvent::new_with_kind(
KeyCode::PageUp,
KeyModifiers::empty(),
KeyEventKind::Release,
)))),
);
assert_eq!(
parse_event(b"\x1B[6;5:3~", false).unwrap(),
Some(InternalEvent::Event(Event::Key(KeyEvent::new_with_kind(
KeyCode::PageDown,
KeyModifiers::CONTROL,
KeyEventKind::Release,
)))),
);
}
}