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 fn is_key_down(&mut self, key_code: KeyCode)->bool{
88        if let Some(_) = self.keys_down.iter().position( | k | k.key_code == key_code) {
89            return true;
90        }
91        return false
92    }
93
94    #[allow(dead_code)]
95    pub (crate) fn process_key_down(&mut self, key_event: KeyEvent) {
96        if let Some(_) = self.keys_down.iter().position( | k | k.key_code == key_event.key_code) {
97            return;
98        }
99        self.keys_down.push(key_event);
100    }
101
102    #[allow(dead_code)]
103    pub (crate) fn process_key_up(&mut self, key_event: KeyEvent) {
104        if let Some(pos) = self.keys_down.iter().position( | k | k.key_code == key_event.key_code) {
105            self.keys_down.remove(pos);
106        }
107    }
108}
109
110#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
111pub struct KeyEvent {
112    pub key_code: KeyCode,
113    pub is_repeat: bool,
114    pub modifiers: KeyModifiers,
115    pub time: f64
116}
117
118#[derive(Clone, Debug)]
119pub struct KeyFocusEvent {
120    pub prev: Area,
121    pub focus: Area,
122}
123
124#[derive(Clone, Debug, SerBin, DeBin, SerJson, DeJson, PartialEq)]
125pub struct TextInputEvent {
126    pub input: String,
127    pub replace_last: bool,
128    pub was_paste: bool
129}
130
131#[derive(Clone, Debug)]
132pub struct TextClipboardEvent {
133    pub response: Rc<RefCell<Option<String>>>
134}
135
136impl Default for KeyCode {
137    fn default() -> Self {KeyCode::Unknown}
138}
139
140
141// lowest common denominator keymap between desktop and web
142#[derive(Live, LiveHook, Clone, Copy, Debug, SerBin, DeBin, SerJson, DeJson, Eq, PartialEq)]
143pub enum KeyCode {
144    #[pick] Escape,
145
146    Back,
147
148    Backtick,
149    Key0,
150    Key1,
151    Key2,
152    Key3,
153    Key4,
154    Key5,
155    Key6,
156    Key7,
157    Key8,
158    Key9,
159    Minus,
160    Equals,
161
162    Backspace,
163    Tab,
164
165    KeyQ,
166    KeyW,
167    KeyE,
168    KeyR,
169    KeyT,
170    KeyY,
171    KeyU,
172    KeyI,
173    KeyO,
174    KeyP,
175    LBracket,
176    RBracket,
177    ReturnKey,
178
179    KeyA,
180    KeyS,
181    KeyD,
182    KeyF,
183    KeyG,
184    KeyH,
185    KeyJ,
186    KeyK,
187    KeyL,
188    Semicolon,
189    Quote,
190    Backslash,
191
192    KeyZ,
193    KeyX,
194    KeyC,
195    KeyV,
196    KeyB,
197    KeyN,
198    KeyM,
199    Comma,
200    Period,
201    Slash,
202
203    Control,
204    Alt,
205    Shift,
206    Logo,
207
208    Space,
209    Capslock,
210    F1,
211    F2,
212    F3,
213    F4,
214    F5,
215    F6,
216    F7,
217    F8,
218    F9,
219    F10,
220    F11,
221    F12,
222
223    PrintScreen,
224    ScrollLock,
225    Pause,
226
227    Insert,
228    Delete,
229    Home,
230    End,
231    PageUp,
232    PageDown,
233
234    Numpad0,
235    Numpad1,
236    Numpad2,
237    Numpad3,
238    Numpad4,
239    Numpad5,
240    Numpad6,
241    Numpad7,
242    Numpad8,
243    Numpad9,
244
245    NumpadEquals,
246    NumpadSubtract,
247    NumpadAdd,
248    NumpadDecimal,
249    NumpadMultiply,
250    NumpadDivide,
251    Numlock,
252    NumpadEnter,
253
254    ArrowUp,
255    ArrowDown,
256    ArrowLeft,
257    ArrowRight,
258
259    Unknown,
260}
261
262impl KeyCode{
263    pub fn is_unknown(&self)->bool{
264        match self{
265            Self::Unknown=>true,
266            _=>false
267        }
268    }
269    pub fn to_char(&self, uc:bool)->Option<char>{
270        match self {
271            KeyCode::KeyA => if uc {Some('A')}else {Some('a')},
272            KeyCode::KeyB => if uc {Some('B')}else {Some('b')},
273            KeyCode::KeyC => if uc {Some('C')}else {Some('c')},
274            KeyCode::KeyD => if uc {Some('D')}else {Some('d')},
275            KeyCode::KeyE => if uc {Some('E')}else {Some('e')},
276            KeyCode::KeyF => if uc {Some('F')}else {Some('f')},
277            KeyCode::KeyG => if uc {Some('G')}else {Some('g')},
278            KeyCode::KeyH => if uc {Some('H')}else {Some('h')},
279            KeyCode::KeyI => if uc {Some('I')}else {Some('i')},
280            KeyCode::KeyJ => if uc {Some('J')}else {Some('j')},
281            KeyCode::KeyK => if uc {Some('K')}else {Some('k')},
282            KeyCode::KeyL => if uc {Some('L')}else {Some('l')},
283            KeyCode::KeyM => if uc {Some('M')}else {Some('m')},
284            KeyCode::KeyN => if uc {Some('N')}else {Some('n')},
285            KeyCode::KeyO => if uc {Some('O')}else {Some('o')},
286            KeyCode::KeyP => if uc {Some('P')}else {Some('p')},
287            KeyCode::KeyQ => if uc {Some('Q')}else {Some('q')},
288            KeyCode::KeyR => if uc {Some('R')}else {Some('r')},
289            KeyCode::KeyS => if uc {Some('S')}else {Some('s')},
290            KeyCode::KeyT => if uc {Some('T')}else {Some('t')},
291            KeyCode::KeyU => if uc {Some('U')}else {Some('u')},
292            KeyCode::KeyV => if uc {Some('V')}else {Some('v')},
293            KeyCode::KeyW => if uc {Some('W')}else {Some('w')},
294            KeyCode::KeyX => if uc {Some('X')}else {Some('x')},
295            KeyCode::KeyY => if uc {Some('Y')}else {Some('y')},
296            KeyCode::KeyZ => if uc {Some('Z')}else {Some('z')},
297            KeyCode::Key0 => if uc {Some(')')}else {Some('0')},
298            KeyCode::Key1 => if uc {Some('!')}else {Some('1')},
299            KeyCode::Key2 => if uc {Some('@')}else {Some('2')},
300            KeyCode::Key3 => if uc {Some('#')}else {Some('3')},
301            KeyCode::Key4 => if uc {Some('$')}else {Some('4')},
302            KeyCode::Key5 => if uc {Some('%')}else {Some('5')},
303            KeyCode::Key6 => if uc {Some('^')}else {Some('6')},
304            KeyCode::Key7 => if uc {Some('&')}else {Some('7')},
305            KeyCode::Key8 => if uc {Some('*')}else {Some('8')},
306            KeyCode::Key9 => if uc {Some('(')}else {Some('9')},
307            KeyCode::Equals => if uc {Some('+')}else {Some('=')},
308            KeyCode::Minus => if uc {Some('_')}else {Some('-')},
309            KeyCode::RBracket => if uc {Some('{')}else {Some('[')},
310            KeyCode::LBracket => if uc {Some('}')}else {Some(']')},
311            KeyCode::ReturnKey => Some('\n'),
312            KeyCode::Backtick => if uc {Some('~')}else {Some('`')},
313            KeyCode::Semicolon => if uc {Some(':')}else {Some(';')},
314            KeyCode::Backslash => if uc {Some('|')}else {Some('\\')},
315            KeyCode::Comma => if uc {Some('<')}else {Some(',')},
316            KeyCode::Slash => if uc {Some('?')}else {Some('/')},
317            KeyCode::Period => if uc {Some('>')}else {Some('.')},
318            KeyCode::Tab => Some('\t'),
319            KeyCode::Space => Some(' '),
320            KeyCode::NumpadDecimal => Some('.'),
321            KeyCode::NumpadMultiply => Some('*'),
322            KeyCode::NumpadAdd => Some('+'),
323            KeyCode::NumpadDivide => Some('/'),
324            KeyCode::NumpadEnter => Some('\n'),
325            KeyCode::NumpadSubtract => Some('-'),
326            KeyCode::Numpad0 => Some('0'),
327            KeyCode::Numpad1 => Some('1'),
328            KeyCode::Numpad2 => Some('2'),
329            KeyCode::Numpad3 => Some('3'),
330            KeyCode::Numpad4 => Some('4'),
331            KeyCode::Numpad5 => Some('5'),
332            KeyCode::Numpad6 => Some('6'),
333            KeyCode::Numpad7 => Some('7'),
334            KeyCode::Numpad8 => Some('8'),
335            KeyCode::Numpad9 => Some('9'),
336            _ => None
337        }
338    }
339}