1
2use wasm_bindgen::prelude::*;
3use crate::io::Key;
4
5#[wasm_bindgen]
6extern {
7 fn copperCreateKeyboard(element_id: &str) -> usize;
8
9 fn copperKeyboardPressing(pointer: usize, key: usize) -> bool;
10
11 fn copperDropKeyboard(pointer: usize);
12}
13
14fn get_key_js(key: Key) -> usize {
15 match key {
16 Key::Ctrl => 0,
17 Key::Shift => 1,
18 Key::Space => 2,
19 Key::Backspace => 3,
20 Key::Enter => 4,
21 Key::Alt => 5,
22 Key::A => 6, Key::B => 7, Key::C => 8, Key::D => 9, Key::E => 10,
23 Key::F => 11, Key::G => 12, Key::H => 13, Key::I => 14, Key::J => 15,
24 Key::K => 16, Key::L => 17, Key::M => 18, Key::N => 19, Key::O => 20,
25 Key::P => 21, Key::Q => 22, Key::R => 23, Key::S => 24, Key::T => 25,
26 Key::U => 26, Key::V => 27, Key::W => 28, Key::X => 29, Key::Y => 30,
27 Key::Z => 31,
28 Key::ArrowUp => 32, Key::ArrowDown => 33, Key::ArrowLef => 34, Key::ArrowRight => 35,
29 Key::D0 => 36, Key::D1 => 37, Key::D2 => 38, Key::D3 => 39, Key::D4 => 40,
30 Key::D5 => 41, Key::D6 => 42, Key::D7 => 43, Key::D8 => 44, Key::D9 => 45,
31 Key::F1 => 46, Key::F2 => 47, Key::F3 => 48, Key::F4 => 49, Key::F5 => 50,
32 Key::F6 => 51, Key::F7 => 52, Key::F8 => 53, Key::F9 => 54, Key::F10 => 55,
33 Key::F11 => 56, Key::F12 => 57,
34 Key::Escape => 58,
35 }
36}
37
38pub struct Keyboard {
39 pointer: usize
40}
41
42impl Keyboard {
43 pub fn from_document() -> Keyboard { Keyboard { pointer: copperCreateKeyboard("") } }
44 pub fn from_element(element_id: &str) -> Keyboard { Keyboard { pointer: copperCreateKeyboard(element_id) } }
45
46 pub fn pressing(&self, key: Key) -> bool { copperKeyboardPressing(self.pointer, get_key_js(key)) }
47}
48
49impl Drop for Keyboard {
50 fn drop(&mut self) {
51 copperDropKeyboard(self.pointer);
52 }
53}