From 11160d11a01142752669bca48527ca0a479b61df Mon Sep 17 00:00:00 2001 From: Timon Date: Sat, 8 Apr 2023 16:15:21 +0200 Subject: [PATCH] Add feature cfgs --- src/event.rs | 4 ++++ src/event/sys/unix/parse.rs | 4 ++++ src/event/sys/windows/parse.rs | 16 ++++++++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/event.rs b/src/event.rs index 30194ead..e2e4ca9c 100644 --- a/src/event.rs +++ b/src/event.rs @@ -256,6 +256,7 @@ bitflags! { /// Add extra events with [`KeyEvent.kind`] set to [`KeyEventKind::Repeat`] or /// [`KeyEventKind::Release`] when keys are autorepeated or released. /// IMPORTANT: Requires feature `event-kind` to be enabled. + #[cfg(feature="event-kind")] const REPORT_EVENT_TYPES = 0b0000_0010; // 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 @@ -614,7 +615,9 @@ bitflags! { #[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)] pub enum KeyEventKind { Press, + #[cfg(feature = "event-kind")] Repeat, + #[cfg(feature = "event-kind")] Release, } @@ -650,6 +653,7 @@ pub struct KeyEvent { /// Additional key modifiers. pub modifiers: KeyModifiers, /// Kind of event. + /// By default `KeyEventKind::Press`. /// /// Only set if: /// - Unix: [`KeyboardEnhancementFlags::REPORT_EVENT_TYPES`] has been enabled with [`PushKeyboardEnhancementFlags`]. diff --git a/src/event/sys/unix/parse.rs b/src/event/sys/unix/parse.rs index 93b51686..26fc6e90 100644 --- a/src/event/sys/unix/parse.rs +++ b/src/event/sys/unix/parse.rs @@ -339,6 +339,7 @@ fn parse_modifiers_to_state(mask: u8) -> KeyEventState { fn parse_key_event_kind(kind: u8) -> KeyEventKind { match kind { 1 => KeyEventKind::Press, + #[cfg(feature = "event-kind")] 2 => KeyEventKind::Repeat, #[cfg(feature = "event-kind")] 3 => KeyEventKind::Release, @@ -1341,6 +1342,7 @@ mod tests { KeyEventKind::Repeat, )))), ); + #[cfg(feature = "event-kind")] assert_eq!( parse_csi_u_encoded_key_code(b"\x1B[97;1:3u").unwrap(), Some(InternalEvent::Event(Event::Key(KeyEvent::new_with_kind( @@ -1464,6 +1466,7 @@ mod tests { } #[test] + #[cfg(feature = "event-kind")] fn test_parse_csi_special_key_code_with_types() { assert_eq!( parse_event(b"\x1B[;1:3B", false).unwrap(), @@ -1484,6 +1487,7 @@ mod tests { } #[test] + #[cfg(feature = "event-kind")] fn test_parse_csi_numbered_escape_code_with_types() { assert_eq!( parse_event(b"\x1B[5;1:3~", false).unwrap(), diff --git a/src/event/sys/windows/parse.rs b/src/event/sys/windows/parse.rs index 9ea94c19..6c2d9cdf 100644 --- a/src/event/sys/windows/parse.rs +++ b/src/event/sys/windows/parse.rs @@ -223,11 +223,13 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option let kind = if key_event.key_down { KeyEventKind::Press } else { - if cfg!(feature = "event-kind") { + #[cfg(feature = "event-kind")] + { 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); return Some(WindowsKeyEvent::KeyEvent(key_event)); @@ -300,11 +302,13 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option let kind = if key_event.key_down { KeyEventKind::Press } else { - if cfg!(feature = "event-kind") { + #[cfg(feature = "event-kind")] + { 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); return Some(WindowsKeyEvent::KeyEvent(key_event));