1use crate::input::{input3_key_to_hid_usage, keyboard, DeviceId, Event, EventType, Modifiers};
6use std::collections::HashSet;
7
8#[derive(Default)]
9pub(crate) struct KeyboardInputHandler {
10 keyboard_device_id: DeviceId,
11 pressed_keys: HashSet<fidl_fuchsia_input::Key>,
12}
13
14impl KeyboardInputHandler {
15 pub fn new() -> Self {
16 Self {
17 keyboard_device_id: DeviceId("scenic-keyboard".to_string()),
18 pressed_keys: HashSet::new(),
19 }
20 }
21
22 pub fn handle_key_event(&mut self, event: &fidl_fuchsia_ui_input3::KeyEvent) -> Vec<Event> {
23 if event.type_.is_none() || event.timestamp.is_none() || event.key.is_none() {
24 println!("Malformed key event {:?}", event);
25 return vec![];
26 }
27 let event_type = event.type_.unwrap();
28 let timestamp = event.timestamp.unwrap() as u64;
29 let key = event.key.unwrap();
30 let phase: Option<keyboard::Phase> = match event_type {
31 fidl_fuchsia_ui_input3::KeyEventType::Pressed => Some(keyboard::Phase::Pressed),
32 fidl_fuchsia_ui_input3::KeyEventType::Released => Some(keyboard::Phase::Released),
33 fidl_fuchsia_ui_input3::KeyEventType::Cancel => Some(keyboard::Phase::Cancelled),
34 fidl_fuchsia_ui_input3::KeyEventType::Sync => None,
36 };
37 if phase.is_none() {
38 return vec![];
39 }
40 let phase = phase.unwrap();
41
42 match phase {
43 keyboard::Phase::Pressed => {
44 self.pressed_keys.insert(key);
45 }
46 keyboard::Phase::Released | keyboard::Phase::Cancelled => {
47 self.pressed_keys.remove(&key);
48 }
49 _ => (),
50 }
51
52 let device_id = self.keyboard_device_id.clone();
53 let hid_usage = input3_key_to_hid_usage(key);
54 let modifiers = Modifiers::from_pressed_keys_3(&self.pressed_keys);
55
56 let code_point = match event.key_meaning {
60 Some(fidl_fuchsia_ui_input3::KeyMeaning::Codepoint(cp)) => Some(cp),
61 Some(fidl_fuchsia_ui_input3::KeyMeaning::NonPrintableKey(_)) | _ => keymaps::US_QWERTY
62 .hid_usage_to_code_point_for_mods(hid_usage, modifiers.shift, modifiers.caps_lock),
63 }
64 .filter(|cp| *cp != 0);
65
66 let keyboard_event = keyboard::Event { code_point, hid_usage, modifiers, phase };
67
68 let event = Event {
69 event_time: timestamp,
70 event_type: EventType::Keyboard(keyboard_event),
71 device_id,
72 };
73
74 vec![event]
75 }
76}