events.rs 26 KB

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