events.rs 26 KB

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