events.rs 26 KB

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