Add feature cfgs

This commit is contained in:
Timon 2023-04-08 16:15:21 +02:00
parent f580717905
commit 11160d11a0
3 changed files with 18 additions and 6 deletions

View File

@ -256,6 +256,7 @@ bitflags! {
/// Add extra events with [`KeyEvent.kind`] set to [`KeyEventKind::Repeat`] or /// Add extra events with [`KeyEvent.kind`] set to [`KeyEventKind::Repeat`] or
/// [`KeyEventKind::Release`] when keys are autorepeated or released. /// [`KeyEventKind::Release`] when keys are autorepeated or released.
/// IMPORTANT: Requires feature `event-kind` to be enabled. /// IMPORTANT: Requires feature `event-kind` to be enabled.
#[cfg(feature="event-kind")]
const REPORT_EVENT_TYPES = 0b0000_0010; const REPORT_EVENT_TYPES = 0b0000_0010;
// Send [alternate keycodes](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#key-codes) // Send [alternate keycodes](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#key-codes)
// in addition to the base keycode. The alternate keycode overrides the base keycode in // in addition to the base keycode. The alternate keycode overrides the base keycode in
@ -614,7 +615,9 @@ bitflags! {
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] #[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum KeyEventKind { pub enum KeyEventKind {
Press, Press,
#[cfg(feature = "event-kind")]
Repeat, Repeat,
#[cfg(feature = "event-kind")]
Release, Release,
} }
@ -650,6 +653,7 @@ pub struct KeyEvent {
/// Additional key modifiers. /// Additional key modifiers.
pub modifiers: KeyModifiers, pub modifiers: KeyModifiers,
/// Kind of event. /// Kind of event.
/// By default `KeyEventKind::Press`.
/// ///
/// Only set if: /// Only set if:
/// - Unix: [`KeyboardEnhancementFlags::REPORT_EVENT_TYPES`] has been enabled with [`PushKeyboardEnhancementFlags`]. /// - Unix: [`KeyboardEnhancementFlags::REPORT_EVENT_TYPES`] has been enabled with [`PushKeyboardEnhancementFlags`].

View File

@ -339,6 +339,7 @@ fn parse_modifiers_to_state(mask: u8) -> KeyEventState {
fn parse_key_event_kind(kind: u8) -> KeyEventKind { fn parse_key_event_kind(kind: u8) -> KeyEventKind {
match kind { match kind {
1 => KeyEventKind::Press, 1 => KeyEventKind::Press,
#[cfg(feature = "event-kind")]
2 => KeyEventKind::Repeat, 2 => KeyEventKind::Repeat,
#[cfg(feature = "event-kind")] #[cfg(feature = "event-kind")]
3 => KeyEventKind::Release, 3 => KeyEventKind::Release,
@ -1341,6 +1342,7 @@ mod tests {
KeyEventKind::Repeat, KeyEventKind::Repeat,
)))), )))),
); );
#[cfg(feature = "event-kind")]
assert_eq!( assert_eq!(
parse_csi_u_encoded_key_code(b"\x1B[97;1:3u").unwrap(), parse_csi_u_encoded_key_code(b"\x1B[97;1:3u").unwrap(),
Some(InternalEvent::Event(Event::Key(KeyEvent::new_with_kind( Some(InternalEvent::Event(Event::Key(KeyEvent::new_with_kind(
@ -1464,6 +1466,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature = "event-kind")]
fn test_parse_csi_special_key_code_with_types() { fn test_parse_csi_special_key_code_with_types() {
assert_eq!( assert_eq!(
parse_event(b"\x1B[;1:3B", false).unwrap(), parse_event(b"\x1B[;1:3B", false).unwrap(),
@ -1484,6 +1487,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature = "event-kind")]
fn test_parse_csi_numbered_escape_code_with_types() { fn test_parse_csi_numbered_escape_code_with_types() {
assert_eq!( assert_eq!(
parse_event(b"\x1B[5;1:3~", false).unwrap(), parse_event(b"\x1B[5;1:3~", false).unwrap(),

View File

@ -223,11 +223,13 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent>
let kind = if key_event.key_down { let kind = if key_event.key_down {
KeyEventKind::Press KeyEventKind::Press
} else { } else {
if cfg!(feature = "event-kind") { #[cfg(feature = "event-kind")]
{
KeyEventKind::Release KeyEventKind::Release
} else {
KeyEventKind::Press
} }
// Dont register key up event.
#[cfg(not(feature = "event-kind"))]
return None;
}; };
let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind); let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind);
return Some(WindowsKeyEvent::KeyEvent(key_event)); return Some(WindowsKeyEvent::KeyEvent(key_event));
@ -300,11 +302,13 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent>
let kind = if key_event.key_down { let kind = if key_event.key_down {
KeyEventKind::Press KeyEventKind::Press
} else { } else {
if cfg!(feature = "event-kind") { #[cfg(feature = "event-kind")]
{
KeyEventKind::Release KeyEventKind::Release
} else {
KeyEventKind::Press
} }
// Dont register key up event.
#[cfg(not(feature = "event-kind"))]
return None;
}; };
let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind); let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind);
return Some(WindowsKeyEvent::KeyEvent(key_event)); return Some(WindowsKeyEvent::KeyEvent(key_event));