events.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. //! This module provides a set of common events for all Dioxus apps to target, regardless of host platform.
  2. //! -------------------------------------------------------------------------------------------------------
  3. //!
  4. //! 3rd party renderers are responsible for converting their native events into these virtual event types. Events might
  5. //! be heavy or need to interact through FFI, so the events themselves are designed to be lazy.
  6. use crate::innerlude::{ElementId, ScopeId};
  7. use bumpalo::boxed::Box as BumpBox;
  8. use std::{any::Any, cell::RefCell, fmt::Debug, ops::Deref, rc::Rc};
  9. use crate::{
  10. innerlude::NodeFactory,
  11. innerlude::{Attribute, Listener, VNode},
  12. };
  13. use std::cell::Cell;
  14. #[derive(Debug)]
  15. pub struct UserEvent {
  16. /// The originator of the event trigger
  17. pub scope: ScopeId,
  18. /// The optional real node associated with the trigger
  19. pub mounted_dom_id: Option<ElementId>,
  20. /// The event type IE "onclick" or "onmouseover"
  21. ///
  22. /// The name that the renderer will use to mount the listener.
  23. pub name: &'static str,
  24. /// The type of event
  25. pub event: SyntheticEvent,
  26. }
  27. pub enum SyntheticEvent {
  28. AnimationEvent(on::AnimationEvent),
  29. ClipboardEvent(on::ClipboardEvent),
  30. CompositionEvent(on::CompositionEvent),
  31. FocusEvent(on::FocusEvent),
  32. FormEvent(on::FormEvent),
  33. KeyboardEvent(on::KeyboardEvent),
  34. GenericEvent(on::GenericEvent),
  35. TouchEvent(on::TouchEvent),
  36. ToggleEvent(on::ToggleEvent),
  37. MediaEvent(on::MediaEvent),
  38. MouseEvent(on::MouseEvent),
  39. WheelEvent(on::WheelEvent),
  40. SelectionEvent(on::SelectionEvent),
  41. TransitionEvent(on::TransitionEvent),
  42. PointerEvent(on::PointerEvent),
  43. }
  44. // ImageEvent(event_data::ImageEvent),
  45. impl std::fmt::Debug for SyntheticEvent {
  46. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  47. let name = match self {
  48. SyntheticEvent::ClipboardEvent(_) => "ClipboardEvent",
  49. SyntheticEvent::CompositionEvent(_) => "CompositionEvent",
  50. SyntheticEvent::KeyboardEvent(_) => "KeyboardEvent",
  51. SyntheticEvent::FocusEvent(_) => "FocusEvent",
  52. SyntheticEvent::FormEvent(_) => "FormEvent",
  53. SyntheticEvent::SelectionEvent(_) => "SelectionEvent",
  54. SyntheticEvent::TouchEvent(_) => "TouchEvent",
  55. SyntheticEvent::WheelEvent(_) => "WheelEvent",
  56. SyntheticEvent::MediaEvent(_) => "MediaEvent",
  57. SyntheticEvent::AnimationEvent(_) => "AnimationEvent",
  58. SyntheticEvent::TransitionEvent(_) => "TransitionEvent",
  59. SyntheticEvent::ToggleEvent(_) => "ToggleEvent",
  60. SyntheticEvent::MouseEvent(_) => "MouseEvent",
  61. SyntheticEvent::PointerEvent(_) => "PointerEvent",
  62. SyntheticEvent::GenericEvent(_) => "GenericEvent",
  63. };
  64. f.debug_struct("VirtualEvent").field("type", &name).finish()
  65. }
  66. }
  67. pub mod on {
  68. //! This module defines the synthetic events that all Dioxus apps enable. No matter the platform, every dioxus renderer
  69. //! will implement the same events and same behavior (bubbling, cancelation, etc).
  70. //!
  71. //! Synthetic events are immutable and wrapped in Arc. It is the intention for Dioxus renderers to re-use the underyling
  72. //! Arc allocation through "get_mut"
  73. //!
  74. //! React recently dropped support for re-using event allocation and just passes the real event along.
  75. use super::*;
  76. macro_rules! event_directory {
  77. ( $(
  78. $( #[$attr:meta] )*
  79. $eventdata:ident($wrapper:ident): [
  80. $(
  81. $( #[$method_attr:meta] )*
  82. $name:ident
  83. )*
  84. ];
  85. )* ) => {
  86. $(
  87. $(#[$attr])*
  88. pub struct $wrapper(pub Rc<dyn $eventdata>);
  89. // todo: derefing to the event is fine (and easy) but breaks some IDE stuff like (go to source)
  90. // going to source in fact takes you to the source of Rc which is... less than useful
  91. // Either we ask for this to be changed in Rust-analyzer or manually impkement the trait
  92. impl Deref for $wrapper {
  93. type Target = Rc<dyn $eventdata>;
  94. fn deref(&self) -> &Self::Target {
  95. &self.0
  96. }
  97. }
  98. $(
  99. $(#[$method_attr])*
  100. pub fn $name<'a, F>(
  101. c: NodeFactory<'a>,
  102. mut callback: F,
  103. ) -> Listener<'a>
  104. where F: FnMut($wrapper) + 'a
  105. {
  106. let bump = &c.bump();
  107. let cb: &mut dyn FnMut(SyntheticEvent) = bump.alloc(move |evt: SyntheticEvent| match evt {
  108. SyntheticEvent::$wrapper(event) => callback(event),
  109. _ => unreachable!("Downcasted SyntheticEvent to wrong event type - this is an internal bug!")
  110. });
  111. let callback: BumpBox<dyn FnMut(SyntheticEvent) + 'a> = unsafe { BumpBox::from_raw(cb) };
  112. let event_name = stringify!($name);
  113. let shortname: &'static str = &event_name[2..];
  114. Listener {
  115. event: shortname,
  116. mounted_node: Cell::new(None),
  117. callback: RefCell::new(Some(callback)),
  118. }
  119. }
  120. )*
  121. )*
  122. };
  123. }
  124. // The Dioxus Synthetic event system
  125. event_directory! {
  126. ClipboardEventInner(ClipboardEvent): [
  127. /// Called when "copy"
  128. oncopy
  129. /// oncut
  130. oncut
  131. /// onpaste
  132. onpaste
  133. ];
  134. CompositionEventInner(CompositionEvent): [
  135. /// oncompositionend
  136. oncompositionend
  137. /// oncompositionstart
  138. oncompositionstart
  139. /// oncompositionupdate
  140. oncompositionupdate
  141. ];
  142. KeyboardEventInner(KeyboardEvent): [
  143. /// onkeydown
  144. onkeydown
  145. /// onkeypress
  146. onkeypress
  147. /// onkeyup
  148. onkeyup
  149. ];
  150. FocusEventInner(FocusEvent): [
  151. /// onfocus
  152. onfocus
  153. /// onblur
  154. onblur
  155. ];
  156. FormEventInner(FormEvent): [
  157. /// onchange
  158. onchange
  159. /// oninput
  160. oninput
  161. /// oninvalid
  162. oninvalid
  163. /// onreset
  164. onreset
  165. /// onsubmit
  166. onsubmit
  167. ];
  168. /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
  169. ///
  170. ///
  171. /// The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse).
  172. ///
  173. /// ## Trait implementation:
  174. /// ```rust
  175. /// fn alt_key(&self) -> bool;
  176. /// fn button(&self) -> i16;
  177. /// fn buttons(&self) -> u16;
  178. /// fn client_x(&self) -> i32;
  179. /// fn client_y(&self) -> i32;
  180. /// fn ctrl_key(&self) -> bool;
  181. /// fn meta_key(&self) -> bool;
  182. /// fn page_x(&self) -> i32;
  183. /// fn page_y(&self) -> i32;
  184. /// fn screen_x(&self) -> i32;
  185. /// fn screen_y(&self) -> i32;
  186. /// fn shift_key(&self) -> bool;
  187. /// fn get_modifier_state(&self, key_code: &str) -> bool;
  188. /// ```
  189. ///
  190. /// ## Event Handlers
  191. /// - [`onclick`]
  192. /// - [`oncontextmenu`]
  193. /// - [`ondoubleclick`]
  194. /// - [`ondrag`]
  195. /// - [`ondragend`]
  196. /// - [`ondragenter`]
  197. /// - [`ondragexit`]
  198. /// - [`ondragleave`]
  199. /// - [`ondragover`]
  200. /// - [`ondragstart`]
  201. /// - [`ondrop`]
  202. /// - [`onmousedown`]
  203. /// - [`onmouseenter`]
  204. /// - [`onmouseleave`]
  205. /// - [`onmousemove`]
  206. /// - [`onmouseout`]
  207. /// - [`onmouseover`]
  208. /// - [`onmouseup`]
  209. MouseEventInner(MouseEvent): [
  210. /// Execute a callback when a button is clicked.
  211. ///
  212. /// ## Description
  213. ///
  214. /// An element receives a click event when a pointing device button (such as a mouse's primary mouse button)
  215. /// is both pressed and released while the pointer is located inside the element.
  216. ///
  217. /// - Bubbles: Yes
  218. /// - Cancelable: Yes
  219. /// - Interface: [`MouseEvent`]
  220. ///
  221. /// If the button is pressed on one element and the pointer is moved outside the element before the button
  222. /// is released, the event is fired on the most specific ancestor element that contained both elements.
  223. /// `click` fires after both the `mousedown` and `mouseup` events have fired, in that order.
  224. ///
  225. /// ## Example
  226. /// ```
  227. /// rsx!( button { "click me", onclick: move |_| log::info!("Clicked!`") } )
  228. /// ```
  229. ///
  230. /// ## Reference
  231. /// - https://www.w3schools.com/tags/ev_onclick.asp
  232. /// - https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event
  233. ///
  234. onclick
  235. /// oncontextmenu
  236. oncontextmenu
  237. /// ondoubleclick
  238. ondoubleclick
  239. /// ondrag
  240. ondrag
  241. /// ondragend
  242. ondragend
  243. /// ondragenter
  244. ondragenter
  245. /// ondragexit
  246. ondragexit
  247. /// ondragleave
  248. ondragleave
  249. /// ondragover
  250. ondragover
  251. /// ondragstart
  252. ondragstart
  253. /// ondrop
  254. ondrop
  255. /// onmousedown
  256. onmousedown
  257. /// onmouseenter
  258. onmouseenter
  259. /// onmouseleave
  260. onmouseleave
  261. /// onmousemove
  262. onmousemove
  263. /// onmouseout
  264. onmouseout
  265. ///
  266. onscroll
  267. /// onmouseover
  268. ///
  269. /// Triggered when the users's mouse hovers over an element.
  270. onmouseover
  271. /// onmouseup
  272. onmouseup
  273. ];
  274. PointerEventInner(PointerEvent): [
  275. /// pointerdown
  276. onpointerdown
  277. /// pointermove
  278. onpointermove
  279. /// pointerup
  280. onpointerup
  281. /// pointercancel
  282. onpointercancel
  283. /// gotpointercapture
  284. ongotpointercapture
  285. /// lostpointercapture
  286. onlostpointercapture
  287. /// pointerenter
  288. onpointerenter
  289. /// pointerleave
  290. onpointerleave
  291. /// pointerover
  292. onpointerover
  293. /// pointerout
  294. onpointerout
  295. ];
  296. SelectionEventInner(SelectionEvent): [
  297. /// onselect
  298. onselect
  299. ];
  300. TouchEventInner(TouchEvent): [
  301. /// ontouchcancel
  302. ontouchcancel
  303. /// ontouchend
  304. ontouchend
  305. /// ontouchmove
  306. ontouchmove
  307. /// ontouchstart
  308. ontouchstart
  309. ];
  310. WheelEventInner(WheelEvent): [
  311. ///
  312. onwheel
  313. ];
  314. MediaEventInner(MediaEvent): [
  315. ///abort
  316. onabort
  317. ///canplay
  318. oncanplay
  319. ///canplaythrough
  320. oncanplaythrough
  321. ///durationchange
  322. ondurationchange
  323. ///emptied
  324. onemptied
  325. ///encrypted
  326. onencrypted
  327. ///ended
  328. onended
  329. ///error
  330. onerror
  331. ///loadeddata
  332. onloadeddata
  333. ///loadedmetadata
  334. onloadedmetadata
  335. ///loadstart
  336. onloadstart
  337. ///pause
  338. onpause
  339. ///play
  340. onplay
  341. ///playing
  342. onplaying
  343. ///progress
  344. onprogress
  345. ///ratechange
  346. onratechange
  347. ///seeked
  348. onseeked
  349. ///seeking
  350. onseeking
  351. ///stalled
  352. onstalled
  353. ///suspend
  354. onsuspend
  355. ///timeupdate
  356. ontimeupdate
  357. ///volumechange
  358. onvolumechange
  359. ///waiting
  360. onwaiting
  361. ];
  362. AnimationEventInner(AnimationEvent): [
  363. /// onanimationstart
  364. onanimationstart
  365. /// onanimationend
  366. onanimationend
  367. /// onanimationiteration
  368. onanimationiteration
  369. ];
  370. TransitionEventInner(TransitionEvent): [
  371. ///
  372. ontransitionend
  373. ];
  374. ToggleEventInner(ToggleEvent): [
  375. ///
  376. ontoggle
  377. ];
  378. }
  379. pub struct GenericEvent(pub Rc<dyn GenericEventInner>);
  380. pub trait GenericEventInner {
  381. /// Return a reference to the raw event. User will need to downcast the event to the right platform-specific type.
  382. fn raw_event(&self) -> &dyn Any;
  383. /// Returns whether or not a specific event is a bubbling event
  384. fn bubbles(&self) -> bool;
  385. /// Sets or returns whether the event should propagate up the hierarchy or not
  386. fn cancel_bubble(&self);
  387. /// Returns whether or not an event can have its default action prevented
  388. fn cancelable(&self) -> bool;
  389. /// Returns whether the event is composed or not
  390. fn composed(&self) -> bool;
  391. // Currently not supported because those no way we could possibly support it
  392. // just cast the event to the right platform-specific type and return it
  393. // /// Returns the event's path
  394. // fn composed_path(&self) -> String;
  395. /// Returns the element whose event listeners triggered the event
  396. fn current_target(&self);
  397. /// Returns whether or not the preventDefault method was called for the event
  398. fn default_prevented(&self) -> bool;
  399. /// Returns which phase of the event flow is currently being evaluated
  400. fn event_phase(&self) -> u16;
  401. /// Returns whether or not an event is trusted
  402. fn is_trusted(&self) -> bool;
  403. /// Cancels the event if it is cancelable, meaning that the default action that belongs to the event will
  404. fn prevent_default(&self);
  405. /// Prevents other listeners of the same event from being called
  406. fn stop_immediate_propagation(&self);
  407. /// Prevents further propagation of an event during event flow
  408. fn stop_propagation(&self);
  409. /// Returns the element that triggered the event
  410. fn target(&self);
  411. /// Returns the time (in milliseconds relative to the epoch) at which the event was created
  412. fn time_stamp(&self) -> f64;
  413. }
  414. pub trait ClipboardEventInner {
  415. // DOMDataTransfer clipboardData
  416. }
  417. pub trait CompositionEventInner {
  418. fn data(&self) -> String;
  419. }
  420. pub trait KeyboardEventInner {
  421. fn alt_key(&self) -> bool;
  422. fn char_code(&self) -> u32;
  423. /// Identify which "key" was entered.
  424. ///
  425. /// This is the best method to use for all languages. They key gets mapped to a String sequence which you can match on.
  426. /// The key isn't an enum because there are just so many context-dependent keys.
  427. ///
  428. /// A full list on which keys to use is available at:
  429. /// <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
  430. ///
  431. /// # Example
  432. ///
  433. /// ```rust
  434. /// match event.key().as_str() {
  435. /// "Esc" | "Escape" => {}
  436. /// "ArrowDown" => {}
  437. /// "ArrowLeft" => {}
  438. /// _ => {}
  439. /// }
  440. /// ```
  441. ///
  442. fn key(&self) -> String;
  443. /// Get the key code as an enum Variant.
  444. ///
  445. /// This is intended for things like arrow keys, escape keys, function keys, and other non-international keys.
  446. /// To match on unicode sequences, use the [`key`] method - this will return a string identifier instead of a limited enum.
  447. ///
  448. ///
  449. /// ## Example
  450. ///
  451. /// ```rust
  452. /// use dioxus::KeyCode;
  453. /// match event.key_code() {
  454. /// KeyCode::Escape => {}
  455. /// KeyCode::LeftArrow => {}
  456. /// KeyCode::RightArrow => {}
  457. /// _ => {}
  458. /// }
  459. /// ```
  460. ///
  461. fn key_code(&self) -> KeyCode;
  462. /// Check if the ctrl key was pressed down
  463. fn ctrl_key(&self) -> bool;
  464. fn get_modifier_state(&self, key_code: &str) -> bool;
  465. fn locale(&self) -> String;
  466. fn location(&self) -> usize;
  467. fn meta_key(&self) -> bool;
  468. fn repeat(&self) -> bool;
  469. fn shift_key(&self) -> bool;
  470. fn which(&self) -> usize;
  471. }
  472. pub trait FocusEventInner {
  473. /* DOMEventInnerTarget relatedTarget */
  474. }
  475. pub trait FormEventInner {
  476. fn value(&self) -> String;
  477. }
  478. pub trait MouseEventInner {
  479. fn alt_key(&self) -> bool;
  480. fn button(&self) -> i16;
  481. fn buttons(&self) -> u16;
  482. /// Get the X coordinate of the mouse relative to the window
  483. fn client_x(&self) -> i32;
  484. fn client_y(&self) -> i32;
  485. fn ctrl_key(&self) -> bool;
  486. fn meta_key(&self) -> bool;
  487. fn page_x(&self) -> i32;
  488. fn page_y(&self) -> i32;
  489. fn screen_x(&self) -> i32;
  490. fn screen_y(&self) -> i32;
  491. fn shift_key(&self) -> bool;
  492. fn get_modifier_state(&self, key_code: &str) -> bool;
  493. }
  494. pub trait PointerEventInner {
  495. // Mouse only
  496. fn alt_key(&self) -> bool;
  497. fn button(&self) -> i16;
  498. fn buttons(&self) -> u16;
  499. fn client_x(&self) -> i32;
  500. fn client_y(&self) -> i32;
  501. fn ctrl_key(&self) -> bool;
  502. fn meta_key(&self) -> bool;
  503. fn page_x(&self) -> i32;
  504. fn page_y(&self) -> i32;
  505. fn screen_x(&self) -> i32;
  506. fn screen_y(&self) -> i32;
  507. fn shift_key(&self) -> bool;
  508. fn get_modifier_state(&self, key_code: &str) -> bool;
  509. fn pointer_id(&self) -> i32;
  510. fn width(&self) -> i32;
  511. fn height(&self) -> i32;
  512. fn pressure(&self) -> f32;
  513. fn tangential_pressure(&self) -> f32;
  514. fn tilt_x(&self) -> i32;
  515. fn tilt_y(&self) -> i32;
  516. fn twist(&self) -> i32;
  517. fn pointer_type(&self) -> String;
  518. fn is_primary(&self) -> bool;
  519. }
  520. pub trait SelectionEventInner {}
  521. pub trait TouchEventInner {
  522. fn alt_key(&self) -> bool;
  523. fn ctrl_key(&self) -> bool;
  524. fn meta_key(&self) -> bool;
  525. fn shift_key(&self) -> bool;
  526. fn get_modifier_state(&self, key_code: &str) -> bool;
  527. // changedTouches: DOMTouchList,
  528. // targetTouches: DOMTouchList,
  529. // touches: DOMTouchList,
  530. }
  531. pub trait UIEventInner {
  532. // DOMAbstractView view
  533. fn detail(&self) -> i32;
  534. }
  535. pub trait WheelEventInner {
  536. fn delta_mode(&self) -> u32;
  537. fn delta_x(&self) -> f64;
  538. fn delta_y(&self) -> f64;
  539. fn delta_z(&self) -> f64;
  540. }
  541. pub trait MediaEventInner {}
  542. pub trait ImageEventInner {
  543. // load error
  544. }
  545. pub trait AnimationEventInner {
  546. fn animation_name(&self) -> String;
  547. fn pseudo_element(&self) -> String;
  548. fn elapsed_time(&self) -> f32;
  549. }
  550. pub trait TransitionEventInner {
  551. fn property_name(&self) -> String;
  552. fn pseudo_element(&self) -> String;
  553. fn elapsed_time(&self) -> f32;
  554. }
  555. pub trait ToggleEventInner {}
  556. pub use util::KeyCode;
  557. mod util {
  558. #[derive(Clone, Copy)]
  559. pub enum KeyCode {
  560. Backspace = 8,
  561. Tab = 9,
  562. Enter = 13,
  563. Shift = 16,
  564. Ctrl = 17,
  565. Alt = 18,
  566. Pause = 19,
  567. CapsLock = 20,
  568. Escape = 27,
  569. PageUp = 33,
  570. PageDown = 34,
  571. End = 35,
  572. Home = 36,
  573. LeftArrow = 37,
  574. UpArrow = 38,
  575. RightArrow = 39,
  576. DownArrow = 40,
  577. Insert = 45,
  578. Delete = 46,
  579. Num0 = 48,
  580. Num1 = 49,
  581. Num2 = 50,
  582. Num3 = 51,
  583. Num4 = 52,
  584. Num5 = 53,
  585. Num6 = 54,
  586. Num7 = 55,
  587. Num8 = 56,
  588. Num9 = 57,
  589. A = 65,
  590. B = 66,
  591. C = 67,
  592. D = 68,
  593. E = 69,
  594. F = 70,
  595. G = 71,
  596. H = 72,
  597. I = 73,
  598. J = 74,
  599. K = 75,
  600. L = 76,
  601. M = 77,
  602. N = 78,
  603. O = 79,
  604. P = 80,
  605. Q = 81,
  606. R = 82,
  607. S = 83,
  608. T = 84,
  609. U = 85,
  610. V = 86,
  611. W = 87,
  612. X = 88,
  613. Y = 89,
  614. Z = 90,
  615. LeftWindow = 91,
  616. RightWindow = 92,
  617. SelectKey = 93,
  618. Numpad0 = 96,
  619. Numpad1 = 97,
  620. Numpad2 = 98,
  621. Numpad3 = 99,
  622. Numpad4 = 100,
  623. Numpad5 = 101,
  624. Numpad6 = 102,
  625. Numpad7 = 103,
  626. Numpad8 = 104,
  627. Numpad9 = 105,
  628. Multiply = 106,
  629. Add = 107,
  630. Subtract = 109,
  631. DecimalPoint = 110,
  632. Divide = 111,
  633. F1 = 112,
  634. F2 = 113,
  635. F3 = 114,
  636. F4 = 115,
  637. F5 = 116,
  638. F6 = 117,
  639. F7 = 118,
  640. F8 = 119,
  641. F9 = 120,
  642. F10 = 121,
  643. F11 = 122,
  644. F12 = 123,
  645. NumLock = 144,
  646. ScrollLock = 145,
  647. Semicolon = 186,
  648. EqualSign = 187,
  649. Comma = 188,
  650. Dash = 189,
  651. Period = 190,
  652. ForwardSlash = 191,
  653. GraveAccent = 192,
  654. OpenBracket = 219,
  655. BackSlash = 220,
  656. CloseBraket = 221,
  657. SingleQuote = 222,
  658. Unknown,
  659. }
  660. impl KeyCode {
  661. pub fn from_raw_code(i: u8) -> Self {
  662. use KeyCode::*;
  663. match i {
  664. 8 => Backspace,
  665. 9 => Tab,
  666. 13 => Enter,
  667. 16 => Shift,
  668. 17 => Ctrl,
  669. 18 => Alt,
  670. 19 => Pause,
  671. 20 => CapsLock,
  672. 27 => Escape,
  673. 33 => PageUp,
  674. 34 => PageDown,
  675. 35 => End,
  676. 36 => Home,
  677. 37 => LeftArrow,
  678. 38 => UpArrow,
  679. 39 => RightArrow,
  680. 40 => DownArrow,
  681. 45 => Insert,
  682. 46 => Delete,
  683. 48 => Num0,
  684. 49 => Num1,
  685. 50 => Num2,
  686. 51 => Num3,
  687. 52 => Num4,
  688. 53 => Num5,
  689. 54 => Num6,
  690. 55 => Num7,
  691. 56 => Num8,
  692. 57 => Num9,
  693. 65 => A,
  694. 66 => B,
  695. 67 => C,
  696. 68 => D,
  697. 69 => E,
  698. 70 => F,
  699. 71 => G,
  700. 72 => H,
  701. 73 => I,
  702. 74 => J,
  703. 75 => K,
  704. 76 => L,
  705. 77 => M,
  706. 78 => N,
  707. 79 => O,
  708. 80 => P,
  709. 81 => Q,
  710. 82 => R,
  711. 83 => S,
  712. 84 => T,
  713. 85 => U,
  714. 86 => V,
  715. 87 => W,
  716. 88 => X,
  717. 89 => Y,
  718. 90 => Z,
  719. 91 => LeftWindow,
  720. 92 => RightWindow,
  721. 93 => SelectKey,
  722. 96 => Numpad0,
  723. 97 => Numpad1,
  724. 98 => Numpad2,
  725. 99 => Numpad3,
  726. 100 => Numpad4,
  727. 101 => Numpad5,
  728. 102 => Numpad6,
  729. 103 => Numpad7,
  730. 104 => Numpad8,
  731. 105 => Numpad9,
  732. 106 => Multiply,
  733. 107 => Add,
  734. 109 => Subtract,
  735. 110 => DecimalPoint,
  736. 111 => Divide,
  737. 112 => F1,
  738. 113 => F2,
  739. 114 => F3,
  740. 115 => F4,
  741. 116 => F5,
  742. 117 => F6,
  743. 118 => F7,
  744. 119 => F8,
  745. 120 => F9,
  746. 121 => F10,
  747. 122 => F11,
  748. 123 => F12,
  749. 144 => NumLock,
  750. 145 => ScrollLock,
  751. 186 => Semicolon,
  752. 187 => EqualSign,
  753. 188 => Comma,
  754. 189 => Dash,
  755. 190 => Period,
  756. 191 => ForwardSlash,
  757. 192 => GraveAccent,
  758. 219 => OpenBracket,
  759. 220 => BackSlash,
  760. 221 => CloseBraket,
  761. 222 => SingleQuote,
  762. _ => Unknown,
  763. }
  764. }
  765. // get the raw code
  766. fn raw_code(&self) -> u32 {
  767. *self as u32
  768. }
  769. }
  770. }
  771. }