fast_fs/nav/key_input.rs
1// <FILE>crates/fast-fs/src/nav/key_input.rs</FILE> - <DESC>Framework-agnostic key input representation</DESC>
2// <VERS>VERSION: 0.1.0</VERS>
3// <WCTX>Implementing nav module PRD</WCTX>
4// <CLOG>Initial creation</CLOG>
5
6//! Framework-agnostic key input
7//!
8//! This module provides a `KeyInput` enum that represents keyboard input
9//! without depending on any specific UI framework. Consumers map their
10//! framework's key events to this type.
11
12/// Framework-agnostic key input representation.
13///
14/// Consumers map their framework's events to this type. For example:
15/// - crossterm: `KeyEvent` → `KeyInput`
16/// - winit: `KeyEvent` → `KeyInput`
17/// - egui: `Key` → `KeyInput`
18///
19/// # Keyboard Layout Considerations
20///
21/// When using `Shift(char)`, be aware that keyboard layouts vary:
22/// - US: `Shift+'n'` produces `'N'`
23/// - German: `Shift+'7'` produces `'/'`
24///
25/// For cross-layout compatibility, prefer:
26/// - Function keys (`F1`-`F12`) for important actions
27/// - Ctrl/Alt modifiers with navigation keys
28/// - Unshifted character keys for common actions
29#[derive(Debug, Clone, PartialEq, Eq, Hash)]
30pub enum KeyInput {
31 // Character input
32 /// A character key was pressed
33 Char(char),
34
35 // Navigation keys
36 /// Up arrow key
37 Up,
38 /// Down arrow key
39 Down,
40 /// Left arrow key
41 Left,
42 /// Right arrow key
43 Right,
44 /// Home key
45 Home,
46 /// End key
47 End,
48 /// Page Up key
49 PageUp,
50 /// Page Down key
51 PageDown,
52
53 // Action keys
54 /// Enter/Return key
55 Enter,
56 /// Backspace key
57 Backspace,
58 /// Delete key
59 Delete,
60 /// Escape key
61 Escape,
62 /// Tab key
63 Tab,
64
65 // Function keys
66 /// Function key (F1-F12)
67 F(u8),
68
69 // Modified character keys
70 /// Ctrl + character
71 Ctrl(char),
72 /// Alt + character
73 Alt(char),
74 /// Shift + character (for uppercase letters)
75 Shift(char),
76
77 // Modified navigation keys (for power users)
78 /// Ctrl + Up arrow
79 CtrlUp,
80 /// Ctrl + Down arrow
81 CtrlDown,
82 /// Ctrl + Left arrow
83 CtrlLeft,
84 /// Ctrl + Right arrow
85 CtrlRight,
86 /// Ctrl + Home
87 CtrlHome,
88 /// Ctrl + End
89 CtrlEnd,
90 /// Alt + Up arrow
91 AltUp,
92 /// Alt + Down arrow
93 AltDown,
94 /// Alt + Enter
95 AltEnter,
96 /// Shift + Up arrow
97 ShiftUp,
98 /// Shift + Down arrow
99 ShiftDown,
100}
101
102impl KeyInput {
103 /// Check if this is a character key (including modified characters)
104 pub fn is_char(&self) -> bool {
105 matches!(
106 self,
107 KeyInput::Char(_) | KeyInput::Ctrl(_) | KeyInput::Alt(_) | KeyInput::Shift(_)
108 )
109 }
110
111 /// Get the character if this is a character key
112 pub fn as_char(&self) -> Option<char> {
113 match self {
114 KeyInput::Char(c) | KeyInput::Shift(c) => Some(*c),
115 _ => None,
116 }
117 }
118
119 /// Check if this key has Ctrl modifier
120 pub fn has_ctrl(&self) -> bool {
121 matches!(
122 self,
123 KeyInput::Ctrl(_)
124 | KeyInput::CtrlUp
125 | KeyInput::CtrlDown
126 | KeyInput::CtrlLeft
127 | KeyInput::CtrlRight
128 | KeyInput::CtrlHome
129 | KeyInput::CtrlEnd
130 )
131 }
132
133 /// Check if this key has Alt modifier
134 pub fn has_alt(&self) -> bool {
135 matches!(
136 self,
137 KeyInput::Alt(_) | KeyInput::AltUp | KeyInput::AltDown | KeyInput::AltEnter
138 )
139 }
140
141 /// Check if this key has Shift modifier
142 pub fn has_shift(&self) -> bool {
143 matches!(
144 self,
145 KeyInput::Shift(_) | KeyInput::ShiftUp | KeyInput::ShiftDown
146 )
147 }
148}
149
150// <FILE>crates/fast-fs/src/nav/key_input.rs</FILE>
151// <VERS>END OF VERSION: 0.1.0</VERS>