makepad_platform/event/
keyboard.rs

1use {
2    std::rc::Rc,
3    std::cell::RefCell,
4    crate::{
5        makepad_live_compiler::*,
6        live_traits::*,
7        makepad_derive_live::*,
8        makepad_micro_serde::*,
9        cx::Cx, 
10        event::{
11            finger::KeyModifiers,
12        },
13        area::Area,
14    },
15};
16
17
18#[derive(Default)]
19pub struct CxKeyboard {
20    pub (crate) prev_key_focus: Area,
21    pub (crate) next_key_focus: Area,
22    pub (crate) key_focus: Area,
23    #[allow(dead_code)]
24    pub (crate) keys_down: Vec<KeyEvent>,
25    pub (crate) text_ime_dismissed: bool
26}
27
28impl CxKeyboard {
29    pub fn modifiers(&self)->KeyModifiers{
30        if let Some(key) = self.keys_down.first(){
31            key.modifiers
32        }
33        else{
34            Default::default()
35        }
36    }
37    
38    pub fn set_key_focus(&mut self, focus_area: Area) {
39        self.text_ime_dismissed = false;
40        self.next_key_focus = focus_area;
41    }
42    
43    pub fn revert_key_focus(&mut self) {
44        self.next_key_focus = self.prev_key_focus;
45    }
46    
47    pub fn has_key_focus(&self, focus_area: Area) -> bool {
48        self.key_focus == focus_area
49    }
50
51    pub fn set_text_ime_dismissed(&mut self) {
52        self.text_ime_dismissed = true;
53    }
54
55    pub fn reset_text_ime_dismissed(&mut self) {
56        self.text_ime_dismissed = false;
57    }
58    
59    pub (crate) fn update_area(&mut self, old_area: Area, new_area: Area) {
60        if self.key_focus == old_area {
61            self.key_focus = new_area
62        }
63        if self.prev_key_focus == old_area {
64            self.prev_key_focus = new_area
65        }
66        if self.next_key_focus == old_area {
67            self.next_key_focus = new_area
68        }
69    }
70    /*
71    pub (crate) fn all_keys_up(&mut self) -> Vec<KeyEvent> {
72        let mut keys_down = Vec::new();
73        std::mem::swap(&mut keys_down, &mut self.keys_down);
74        keys_down
75    }*/
76    
77    pub (crate) fn cycle_key_focus_changed(&mut self) -> Option<(Area, Area)> {
78        if self.next_key_focus != self.key_focus {
79            self.prev_key_focus = self.key_focus;
80            self.key_focus = self.next_key_focus;
81            return Some((self.prev_key_focus, self.key_focus))
82        }
83        None
84    }
85    
86    #[allow(dead_code)]
87    pub (crate) fn process_key_down(&mut self, key_event: KeyEvent) {
88        if let Some(_) = self.keys_down.iter().position( | k | k.key_code == key_event.key_code) {
89            return;
90        }
91        self.keys_down.push(key_event);
92    }
93    
94    #[allow(dead_code)]
95    pub (crate) fn process_key_up(&mut self, key_event: KeyEvent) {
96        if let Some(pos) = self.keys_down.iter().position( | k | k.key_code == key_event.key_code) {
97            self.keys_down.remove(pos);
98        }
99    }
100}
101
102#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
103pub struct KeyEvent {
104    pub key_code: KeyCode,
105    pub is_repeat: bool,
106    pub modifiers: KeyModifiers,
107    pub time: f64
108}
109
110#[derive(Clone, Debug)]
111pub struct KeyFocusEvent {
112    pub prev: Area,
113    pub focus: Area,
114}
115
116#[derive(Clone, Debug)]
117pub struct TextInputEvent {
118    pub input: String,
119    pub replace_last: bool,
120    pub was_paste: bool
121}
122
123#[derive(Clone, Debug)]
124pub struct TextClipboardEvent {
125    pub response: Rc<RefCell<Option<String>>>
126}
127
128impl Default for KeyCode {
129    fn default() -> Self {KeyCode::Unknown}
130}
131
132
133// lowest common denominator keymap between desktop and web
134#[derive(Live, LiveHook, Clone, Copy, Debug, SerBin, DeBin, SerJson, DeJson, PartialEq)]
135pub enum KeyCode {
136    #[pick] Escape,
137    
138    Backtick,
139    Key0,
140    Key1,
141    Key2,
142    Key3,
143    Key4,
144    Key5,
145    Key6,
146    Key7,
147    Key8,
148    Key9,
149    Minus,
150    Equals,
151    
152    Backspace,
153    Tab,
154    
155    KeyQ,
156    KeyW,
157    KeyE,
158    KeyR,
159    KeyT,
160    KeyY,
161    KeyU,
162    KeyI,
163    KeyO,
164    KeyP,
165    LBracket,
166    RBracket,
167    ReturnKey,
168    
169    KeyA,
170    KeyS,
171    KeyD,
172    KeyF,
173    KeyG,
174    KeyH,
175    KeyJ,
176    KeyK,
177    KeyL,
178    Semicolon,
179    Quote,
180    Backslash,
181    
182    KeyZ,
183    KeyX,
184    KeyC,
185    KeyV,
186    KeyB,
187    KeyN,
188    KeyM,
189    Comma,
190    Period,
191    Slash,
192    
193    Control,
194    Alt,
195    Shift,
196    Logo,
197    
198    Space,
199    Capslock,
200    F1,
201    F2,
202    F3,
203    F4,
204    F5,
205    F6,
206    F7,
207    F8,
208    F9,
209    F10,
210    F11,
211    F12,
212    
213    PrintScreen,
214    ScrollLock,
215    Pause,
216    
217    Insert,
218    Delete,
219    Home,
220    End,
221    PageUp,
222    PageDown,
223    
224    Numpad0,
225    Numpad1,
226    Numpad2,
227    Numpad3,
228    Numpad4,
229    Numpad5,
230    Numpad6,
231    Numpad7,
232    Numpad8,
233    Numpad9,
234    
235    NumpadEquals,
236    NumpadSubtract,
237    NumpadAdd,
238    NumpadDecimal,
239    NumpadMultiply,
240    NumpadDivide,
241    Numlock,
242    NumpadEnter,
243    
244    ArrowUp,
245    ArrowDown,
246    ArrowLeft,
247    ArrowRight,
248    
249    Unknown,
250}
251
252impl KeyCode{
253    pub fn is_unknown(&self)->bool{
254        match self{
255            Self::Unknown=>true,
256            _=>false
257        }
258    }
259    pub fn to_char(&self, uc:bool)->Option<char>{
260        match self {
261            KeyCode::KeyA => if uc {Some('A')}else {Some('a')},
262            KeyCode::KeyB => if uc {Some('B')}else {Some('b')},
263            KeyCode::KeyC => if uc {Some('C')}else {Some('c')},
264            KeyCode::KeyD => if uc {Some('D')}else {Some('d')},
265            KeyCode::KeyE => if uc {Some('E')}else {Some('e')},
266            KeyCode::KeyF => if uc {Some('F')}else {Some('f')},
267            KeyCode::KeyG => if uc {Some('G')}else {Some('g')},
268            KeyCode::KeyH => if uc {Some('H')}else {Some('h')},
269            KeyCode::KeyI => if uc {Some('I')}else {Some('i')},
270            KeyCode::KeyJ => if uc {Some('J')}else {Some('j')},
271            KeyCode::KeyK => if uc {Some('K')}else {Some('k')},
272            KeyCode::KeyL => if uc {Some('L')}else {Some('l')},
273            KeyCode::KeyM => if uc {Some('M')}else {Some('m')},
274            KeyCode::KeyN => if uc {Some('N')}else {Some('n')},
275            KeyCode::KeyO => if uc {Some('O')}else {Some('o')},
276            KeyCode::KeyP => if uc {Some('P')}else {Some('p')},
277            KeyCode::KeyQ => if uc {Some('Q')}else {Some('q')},
278            KeyCode::KeyR => if uc {Some('R')}else {Some('r')},
279            KeyCode::KeyS => if uc {Some('S')}else {Some('s')},
280            KeyCode::KeyT => if uc {Some('T')}else {Some('t')},
281            KeyCode::KeyU => if uc {Some('U')}else {Some('u')},
282            KeyCode::KeyV => if uc {Some('V')}else {Some('v')},
283            KeyCode::KeyW => if uc {Some('W')}else {Some('w')},
284            KeyCode::KeyX => if uc {Some('X')}else {Some('x')},
285            KeyCode::KeyY => if uc {Some('Y')}else {Some('y')},
286            KeyCode::KeyZ => if uc {Some('Z')}else {Some('z')},
287            KeyCode::Key0 => if uc {Some(')')}else {Some('0')},
288            KeyCode::Key1 => if uc {Some('!')}else {Some('1')},
289            KeyCode::Key2 => if uc {Some('@')}else {Some('2')},
290            KeyCode::Key3 => if uc {Some('#')}else {Some('3')},
291            KeyCode::Key4 => if uc {Some('$')}else {Some('4')},
292            KeyCode::Key5 => if uc {Some('%')}else {Some('5')},
293            KeyCode::Key6 => if uc {Some('^')}else {Some('6')},
294            KeyCode::Key7 => if uc {Some('&')}else {Some('7')},
295            KeyCode::Key8 => if uc {Some('*')}else {Some('8')},
296            KeyCode::Key9 => if uc {Some('(')}else {Some('9')},
297            KeyCode::Equals => if uc {Some('+')}else {Some('=')},
298            KeyCode::Minus => if uc {Some('_')}else {Some('-')},
299            KeyCode::RBracket => if uc {Some('{')}else {Some('[')},
300            KeyCode::LBracket => if uc {Some('}')}else {Some(']')},
301            KeyCode::ReturnKey => Some('\n'),
302            KeyCode::Backtick => if uc {Some('~')}else {Some('`')},
303            KeyCode::Semicolon => if uc {Some(':')}else {Some(';')},
304            KeyCode::Backslash => if uc {Some('|')}else {Some('\\')},
305            KeyCode::Comma => if uc {Some('<')}else {Some(',')},
306            KeyCode::Slash => if uc {Some('?')}else {Some('/')},
307            KeyCode::Period => if uc {Some('>')}else {Some('.')},
308            KeyCode::Tab => Some('\t'),
309            KeyCode::Space => Some(' '),
310            KeyCode::NumpadDecimal => Some('.'),
311            KeyCode::NumpadMultiply => Some('*'),
312            KeyCode::NumpadAdd => Some('+'),
313            KeyCode::NumpadDivide => Some('/'),
314            KeyCode::NumpadEnter => Some('\n'),
315            KeyCode::NumpadSubtract => Some('-'),
316            KeyCode::Numpad0 => Some('0'),
317            KeyCode::Numpad1 => Some('1'),
318            KeyCode::Numpad2 => Some('2'),
319            KeyCode::Numpad3 => Some('3'),
320            KeyCode::Numpad4 => Some('4'),
321            KeyCode::Numpad5 => Some('5'),
322            KeyCode::Numpad6 => Some('6'),
323            KeyCode::Numpad7 => Some('7'),
324            KeyCode::Numpad8 => Some('8'),
325            KeyCode::Numpad9 => Some('9'),
326            _ => None
327        }
328    }
329}