events.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. use bumpalo::boxed::Box as BumpBox;
  2. use dioxus_core::exports::bumpalo;
  3. use dioxus_core::*;
  4. use std::any::Any;
  5. pub mod on {
  6. use super::*;
  7. use std::sync::Arc;
  8. macro_rules! event_directory {
  9. ( $(
  10. $( #[$attr:meta] )*
  11. $wrapper:ident: [
  12. // $eventdata:ident($wrapper:ident): [
  13. $(
  14. $( #[$method_attr:meta] )*
  15. $name:ident
  16. )*
  17. ];
  18. )* ) => {
  19. $(
  20. $(
  21. $(#[$method_attr])*
  22. pub fn $name<'a, F>(
  23. c: NodeFactory<'a>,
  24. mut callback: F,
  25. ) -> Listener<'a>
  26. where F: FnMut(Arc<$wrapper>) + 'a
  27. {
  28. let bump = &c.bump();
  29. // we can't allocate unsized in bumpalo's box, so we need to craft the box manually
  30. // safety: this is essentially the same as calling Box::new() but manually
  31. // The box is attached to the lifetime of the bumpalo allocator
  32. let cb: &mut dyn FnMut(Arc<dyn Any + Send + Sync>) = bump.alloc(move |evt: Arc<dyn Any + Send + Sync>| {
  33. let event = evt.downcast::<$wrapper>().unwrap();
  34. callback(event)
  35. });
  36. let callback: BumpBox<dyn FnMut(Arc<dyn Any + Send + Sync>) + 'a> = unsafe { BumpBox::from_raw(cb) };
  37. // ie oncopy
  38. let event_name = stringify!($name);
  39. // ie copy
  40. let shortname: &'static str = &event_name[2..];
  41. let handler = EventHandler {
  42. callback: bump.alloc(std::cell::RefCell::new(Some(callback))),
  43. };
  44. c.listener(shortname, handler)
  45. }
  46. )*
  47. )*
  48. };
  49. }
  50. // The Dioxus Synthetic event system
  51. // todo: move these into the html event system. dioxus accepts *any* event, so having these here doesn't make sense.
  52. event_directory! {
  53. ClipboardEvent: [
  54. /// Called when "copy"
  55. oncopy
  56. /// oncut
  57. oncut
  58. /// onpaste
  59. onpaste
  60. ];
  61. CompositionEvent: [
  62. /// oncompositionend
  63. oncompositionend
  64. /// oncompositionstart
  65. oncompositionstart
  66. /// oncompositionupdate
  67. oncompositionupdate
  68. ];
  69. KeyboardEvent: [
  70. /// onkeydown
  71. onkeydown
  72. /// onkeypress
  73. onkeypress
  74. /// onkeyup
  75. onkeyup
  76. ];
  77. FocusEvent: [
  78. /// onfocus
  79. onfocus
  80. /// onblur
  81. onblur
  82. ];
  83. FormEvent: [
  84. /// onchange
  85. onchange
  86. /// oninput handler
  87. oninput
  88. /// oninvalid
  89. oninvalid
  90. /// onreset
  91. onreset
  92. /// onsubmit
  93. onsubmit
  94. ];
  95. /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
  96. ///
  97. ///
  98. /// The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse).
  99. ///
  100. /// ## Trait implementation:
  101. /// ```rust
  102. /// fn alt_key(&self) -> bool;
  103. /// fn button(&self) -> i16;
  104. /// fn buttons(&self) -> u16;
  105. /// fn client_x(&self) -> i32;
  106. /// fn client_y(&self) -> i32;
  107. /// fn ctrl_key(&self) -> bool;
  108. /// fn meta_key(&self) -> bool;
  109. /// fn page_x(&self) -> i32;
  110. /// fn page_y(&self) -> i32;
  111. /// fn screen_x(&self) -> i32;
  112. /// fn screen_y(&self) -> i32;
  113. /// fn shift_key(&self) -> bool;
  114. /// fn get_modifier_state(&self, key_code: &str) -> bool;
  115. /// ```
  116. ///
  117. /// ## Event Handlers
  118. /// - [`onclick`]
  119. /// - [`oncontextmenu`]
  120. /// - [`ondoubleclick`]
  121. /// - [`ondrag`]
  122. /// - [`ondragend`]
  123. /// - [`ondragenter`]
  124. /// - [`ondragexit`]
  125. /// - [`ondragleave`]
  126. /// - [`ondragover`]
  127. /// - [`ondragstart`]
  128. /// - [`ondrop`]
  129. /// - [`onmousedown`]
  130. /// - [`onmouseenter`]
  131. /// - [`onmouseleave`]
  132. /// - [`onmousemove`]
  133. /// - [`onmouseout`]
  134. /// - [`onmouseover`]
  135. /// - [`onmouseup`]
  136. MouseEvent: [
  137. /// Execute a callback when a button is clicked.
  138. ///
  139. /// ## Description
  140. ///
  141. /// An element receives a click event when a pointing device button (such as a mouse's primary mouse button)
  142. /// is both pressed and released while the pointer is located inside the element.
  143. ///
  144. /// - Bubbles: Yes
  145. /// - Cancelable: Yes
  146. /// - Interface: [`MouseEvent`]
  147. ///
  148. /// If the button is pressed on one element and the pointer is moved outside the element before the button
  149. /// is released, the event is fired on the most specific ancestor element that contained both elements.
  150. /// `click` fires after both the `mousedown` and `mouseup` events have fired, in that order.
  151. ///
  152. /// ## Example
  153. /// ```
  154. /// rsx!( button { "click me", onclick: move |_| log::info!("Clicked!`") } )
  155. /// ```
  156. ///
  157. /// ## Reference
  158. /// - https://www.w3schools.com/tags/ev_onclick.asp
  159. /// - https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event
  160. onclick
  161. /// oncontextmenu
  162. oncontextmenu
  163. /// ondoubleclick
  164. ondoubleclick
  165. /// ondrag
  166. ondrag
  167. /// ondragend
  168. ondragend
  169. /// ondragenter
  170. ondragenter
  171. /// ondragexit
  172. ondragexit
  173. /// ondragleave
  174. ondragleave
  175. /// ondragover
  176. ondragover
  177. /// ondragstart
  178. ondragstart
  179. /// ondrop
  180. ondrop
  181. /// onmousedown
  182. onmousedown
  183. /// onmouseenter
  184. onmouseenter
  185. /// onmouseleave
  186. onmouseleave
  187. /// onmousemove
  188. onmousemove
  189. /// onmouseout
  190. onmouseout
  191. ///
  192. onscroll
  193. /// onmouseover
  194. ///
  195. /// Triggered when the users's mouse hovers over an element.
  196. onmouseover
  197. /// onmouseup
  198. onmouseup
  199. ];
  200. PointerEvent: [
  201. /// pointerdown
  202. onpointerdown
  203. /// pointermove
  204. onpointermove
  205. /// pointerup
  206. onpointerup
  207. /// pointercancel
  208. onpointercancel
  209. /// gotpointercapture
  210. ongotpointercapture
  211. /// lostpointercapture
  212. onlostpointercapture
  213. /// pointerenter
  214. onpointerenter
  215. /// pointerleave
  216. onpointerleave
  217. /// pointerover
  218. onpointerover
  219. /// pointerout
  220. onpointerout
  221. ];
  222. SelectionEvent: [
  223. /// onselect
  224. onselect
  225. ];
  226. TouchEvent: [
  227. /// ontouchcancel
  228. ontouchcancel
  229. /// ontouchend
  230. ontouchend
  231. /// ontouchmove
  232. ontouchmove
  233. /// ontouchstart
  234. ontouchstart
  235. ];
  236. WheelEvent: [
  237. ///
  238. onwheel
  239. ];
  240. MediaEvent: [
  241. ///abort
  242. onabort
  243. ///canplay
  244. oncanplay
  245. ///canplaythrough
  246. oncanplaythrough
  247. ///durationchange
  248. ondurationchange
  249. ///emptied
  250. onemptied
  251. ///encrypted
  252. onencrypted
  253. ///ended
  254. onended
  255. ///error
  256. onerror
  257. ///loadeddata
  258. onloadeddata
  259. ///loadedmetadata
  260. onloadedmetadata
  261. ///loadstart
  262. onloadstart
  263. ///pause
  264. onpause
  265. ///play
  266. onplay
  267. ///playing
  268. onplaying
  269. ///progress
  270. onprogress
  271. ///ratechange
  272. onratechange
  273. ///seeked
  274. onseeked
  275. ///seeking
  276. onseeking
  277. ///stalled
  278. onstalled
  279. ///suspend
  280. onsuspend
  281. ///timeupdate
  282. ontimeupdate
  283. ///volumechange
  284. onvolumechange
  285. ///waiting
  286. onwaiting
  287. ];
  288. AnimationEvent: [
  289. /// onanimationstart
  290. onanimationstart
  291. /// onanimationend
  292. onanimationend
  293. /// onanimationiteration
  294. onanimationiteration
  295. ];
  296. TransitionEvent: [
  297. ///
  298. ontransitionend
  299. ];
  300. ToggleEvent: [
  301. ///
  302. ontoggle
  303. ];
  304. }
  305. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  306. #[derive(Debug)]
  307. pub struct ClipboardEvent(
  308. // DOMDataTransfer clipboardData
  309. );
  310. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  311. #[derive(Debug)]
  312. pub struct CompositionEvent {
  313. pub data: String,
  314. }
  315. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  316. #[derive(Debug)]
  317. pub struct KeyboardEvent {
  318. pub char_code: u32,
  319. /// Identify which "key" was entered.
  320. ///
  321. /// This is the best method to use for all languages. They key gets mapped to a String sequence which you can match on.
  322. /// The key isn't an enum because there are just so many context-dependent keys.
  323. ///
  324. /// A full list on which keys to use is available at:
  325. /// <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
  326. ///
  327. /// # Example
  328. ///
  329. /// ```rust
  330. /// match event.key().as_str() {
  331. /// "Esc" | "Escape" => {}
  332. /// "ArrowDown" => {}
  333. /// "ArrowLeft" => {}
  334. /// _ => {}
  335. /// }
  336. /// ```
  337. ///
  338. pub key: String,
  339. /// Get the key code as an enum Variant.
  340. ///
  341. /// This is intended for things like arrow keys, escape keys, function keys, and other non-international keys.
  342. /// To match on unicode sequences, use the [`key`] method - this will return a string identifier instead of a limited enum.
  343. ///
  344. ///
  345. /// ## Example
  346. ///
  347. /// ```rust
  348. /// use dioxus::KeyCode;
  349. /// match event.key_code() {
  350. /// KeyCode::Escape => {}
  351. /// KeyCode::LeftArrow => {}
  352. /// KeyCode::RightArrow => {}
  353. /// _ => {}
  354. /// }
  355. /// ```
  356. ///
  357. pub key_code: KeyCode,
  358. /// Indicate if the `alt` modifier key was pressed during this keyboard event
  359. pub alt_key: bool,
  360. /// Indicate if the `ctrl` modifier key was pressed during this keyboard event
  361. pub ctrl_key: bool,
  362. /// Indicate if the `meta` modifier key was pressed during this keyboard event
  363. pub meta_key: bool,
  364. /// Indicate if the `shift` modifier key was pressed during this keyboard event
  365. pub shift_key: bool,
  366. pub locale: String,
  367. pub location: usize,
  368. pub repeat: bool,
  369. pub which: usize,
  370. // get_modifier_state: bool,
  371. }
  372. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  373. #[derive(Debug)]
  374. pub struct FocusEvent {/* DOMEventInner: Send + SyncTarget relatedTarget */}
  375. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  376. #[derive(Debug)]
  377. pub struct FormEvent {
  378. pub value: String,
  379. /* DOMEvent: Send + SyncTarget relatedTarget */
  380. }
  381. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  382. #[derive(Debug)]
  383. pub struct MouseEvent {
  384. pub alt_key: bool,
  385. pub button: i16,
  386. pub buttons: u16,
  387. pub client_x: i32,
  388. pub client_y: i32,
  389. pub ctrl_key: bool,
  390. pub meta_key: bool,
  391. pub page_x: i32,
  392. pub page_y: i32,
  393. pub screen_x: i32,
  394. pub screen_y: i32,
  395. pub shift_key: bool,
  396. // fn get_modifier_state(&self, key_code: &str) -> bool;
  397. }
  398. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  399. #[derive(Debug)]
  400. pub struct PointerEvent {
  401. // Mouse only
  402. pub alt_key: bool,
  403. pub button: i16,
  404. pub buttons: u16,
  405. pub client_x: i32,
  406. pub client_y: i32,
  407. pub ctrl_key: bool,
  408. pub meta_key: bool,
  409. pub page_x: i32,
  410. pub page_y: i32,
  411. pub screen_x: i32,
  412. pub screen_y: i32,
  413. pub shift_key: bool,
  414. pub pointer_id: i32,
  415. pub width: i32,
  416. pub height: i32,
  417. pub pressure: f32,
  418. pub tangential_pressure: f32,
  419. pub tilt_x: i32,
  420. pub tilt_y: i32,
  421. pub twist: i32,
  422. pub pointer_type: String,
  423. pub is_primary: bool,
  424. // pub get_modifier_state: bool,
  425. }
  426. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  427. #[derive(Debug)]
  428. pub struct SelectionEvent {}
  429. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  430. #[derive(Debug)]
  431. pub struct TouchEvent {
  432. pub alt_key: bool,
  433. pub ctrl_key: bool,
  434. pub meta_key: bool,
  435. pub shift_key: bool,
  436. // get_modifier_state: bool,
  437. // changedTouches: DOMTouchList,
  438. // targetTouches: DOMTouchList,
  439. // touches: DOMTouchList,
  440. }
  441. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  442. #[derive(Debug)]
  443. pub struct WheelEvent {
  444. pub delta_mode: u32,
  445. pub delta_x: f64,
  446. pub delta_y: f64,
  447. pub delta_z: f64,
  448. }
  449. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  450. #[derive(Debug)]
  451. pub struct MediaEvent {}
  452. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  453. #[derive(Debug)]
  454. pub struct ImageEvent {
  455. pub load_error: bool,
  456. }
  457. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  458. #[derive(Debug)]
  459. pub struct AnimationEvent {
  460. pub animation_name: String,
  461. pub pseudo_element: String,
  462. pub elapsed_time: f32,
  463. }
  464. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  465. #[derive(Debug)]
  466. pub struct TransitionEvent {
  467. pub property_name: String,
  468. pub pseudo_element: String,
  469. pub elapsed_time: f32,
  470. }
  471. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  472. #[derive(Debug)]
  473. pub struct ToggleEvent {}
  474. }
  475. #[cfg_attr(
  476. feature = "serialize",
  477. derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr)
  478. )]
  479. #[derive(Clone, Copy, Debug)]
  480. #[repr(u8)]
  481. pub enum KeyCode {
  482. // That key has no keycode, = 0
  483. // break, = 3
  484. // backspace / delete, = 8
  485. // tab, = 9
  486. // clear, = 12
  487. // enter, = 13
  488. // shift, = 16
  489. // ctrl, = 17
  490. // alt, = 18
  491. // pause/break, = 19
  492. // caps lock, = 20
  493. // hangul, = 21
  494. // hanja, = 25
  495. // escape, = 27
  496. // conversion, = 28
  497. // non-conversion, = 29
  498. // spacebar, = 32
  499. // page up, = 33
  500. // page down, = 34
  501. // end, = 35
  502. // home, = 36
  503. // left arrow, = 37
  504. // up arrow, = 38
  505. // right arrow, = 39
  506. // down arrow, = 40
  507. // select, = 41
  508. // print, = 42
  509. // execute, = 43
  510. // Print Screen, = 44
  511. // insert, = 45
  512. // delete, = 46
  513. // help, = 47
  514. // 0, = 48
  515. // 1, = 49
  516. // 2, = 50
  517. // 3, = 51
  518. // 4, = 52
  519. // 5, = 53
  520. // 6, = 54
  521. // 7, = 55
  522. // 8, = 56
  523. // 9, = 57
  524. // :, = 58
  525. // semicolon (firefox), equals, = 59
  526. // <, = 60
  527. // equals (firefox), = 61
  528. // ß, = 63
  529. // @ (firefox), = 64
  530. // a, = 65
  531. // b, = 66
  532. // c, = 67
  533. // d, = 68
  534. // e, = 69
  535. // f, = 70
  536. // g, = 71
  537. // h, = 72
  538. // i, = 73
  539. // j, = 74
  540. // k, = 75
  541. // l, = 76
  542. // m, = 77
  543. // n, = 78
  544. // o, = 79
  545. // p, = 80
  546. // q, = 81
  547. // r, = 82
  548. // s, = 83
  549. // t, = 84
  550. // u, = 85
  551. // v, = 86
  552. // w, = 87
  553. // x, = 88
  554. // y, = 89
  555. // z, = 90
  556. // Windows Key / Left ⌘ / Chromebook Search key, = 91
  557. // right window key, = 92
  558. // Windows Menu / Right ⌘, = 93
  559. // sleep, = 95
  560. // numpad 0, = 96
  561. // numpad 1, = 97
  562. // numpad 2, = 98
  563. // numpad 3, = 99
  564. // numpad 4, = 100
  565. // numpad 5, = 101
  566. // numpad 6, = 102
  567. // numpad 7, = 103
  568. // numpad 8, = 104
  569. // numpad 9, = 105
  570. // multiply, = 106
  571. // add, = 107
  572. // numpad period (firefox), = 108
  573. // subtract, = 109
  574. // decimal point, = 110
  575. // divide, = 111
  576. // f1, = 112
  577. // f2, = 113
  578. // f3, = 114
  579. // f4, = 115
  580. // f5, = 116
  581. // f6, = 117
  582. // f7, = 118
  583. // f8, = 119
  584. // f9, = 120
  585. // f10, = 121
  586. // f11, = 122
  587. // f12, = 123
  588. // f13, = 124
  589. // f14, = 125
  590. // f15, = 126
  591. // f16, = 127
  592. // f17, = 128
  593. // f18, = 129
  594. // f19, = 130
  595. // f20, = 131
  596. // f21, = 132
  597. // f22, = 133
  598. // f23, = 134
  599. // f24, = 135
  600. // f25, = 136
  601. // f26, = 137
  602. // f27, = 138
  603. // f28, = 139
  604. // f29, = 140
  605. // f30, = 141
  606. // f31, = 142
  607. // f32, = 143
  608. // num lock, = 144
  609. // scroll lock, = 145
  610. // airplane mode, = 151
  611. // ^, = 160
  612. // !, = 161
  613. // ؛ (arabic semicolon), = 162
  614. // #, = 163
  615. // $, = 164
  616. // ù, = 165
  617. // page backward, = 166
  618. // page forward, = 167
  619. // refresh, = 168
  620. // closing paren (AZERTY), = 169
  621. // *, = 170
  622. // ~ + * key, = 171
  623. // home key, = 172
  624. // minus (firefox), mute/unmute, = 173
  625. // decrease volume level, = 174
  626. // increase volume level, = 175
  627. // next, = 176
  628. // previous, = 177
  629. // stop, = 178
  630. // play/pause, = 179
  631. // e-mail, = 180
  632. // mute/unmute (firefox), = 181
  633. // decrease volume level (firefox), = 182
  634. // increase volume level (firefox), = 183
  635. // semi-colon / ñ, = 186
  636. // equal sign, = 187
  637. // comma, = 188
  638. // dash, = 189
  639. // period, = 190
  640. // forward slash / ç, = 191
  641. // grave accent / ñ / æ / ö, = 192
  642. // ?, / or °, = 193
  643. // numpad period (chrome), = 194
  644. // open bracket, = 219
  645. // back slash, = 220
  646. // close bracket / å, = 221
  647. // single quote / ø / ä, = 222
  648. // `, = 223
  649. // left or right ⌘ key (firefox), = 224
  650. // altgr, = 225
  651. // < /git >, left back slash, = 226
  652. // GNOME Compose Key, = 230
  653. // ç, = 231
  654. // XF86Forward, = 233
  655. // XF86Back, = 234
  656. // non-conversion, = 235
  657. // alphanumeric, = 240
  658. // hiragana/katakana, = 242
  659. // half-width/full-width, = 243
  660. // kanji, = 244
  661. // unlock trackpad (Chrome/Edge), = 251
  662. // toggle touchpad, = 255
  663. NA = 0,
  664. Break = 3,
  665. Backspace = 8,
  666. Tab = 9,
  667. Clear = 12,
  668. Enter = 13,
  669. Shift = 16,
  670. Ctrl = 17,
  671. Alt = 18,
  672. Pause = 19,
  673. CapsLock = 20,
  674. // hangul, = 21
  675. // hanja, = 25
  676. Escape = 27,
  677. // conversion, = 28
  678. // non-conversion, = 29
  679. Space = 32,
  680. PageUp = 33,
  681. PageDown = 34,
  682. End = 35,
  683. Home = 36,
  684. LeftArrow = 37,
  685. UpArrow = 38,
  686. RightArrow = 39,
  687. DownArrow = 40,
  688. // select, = 41
  689. // print, = 42
  690. // execute, = 43
  691. // Print Screen, = 44
  692. Insert = 45,
  693. Delete = 46,
  694. // help, = 47
  695. Num0 = 48,
  696. Num1 = 49,
  697. Num2 = 50,
  698. Num3 = 51,
  699. Num4 = 52,
  700. Num5 = 53,
  701. Num6 = 54,
  702. Num7 = 55,
  703. Num8 = 56,
  704. Num9 = 57,
  705. // :, = 58
  706. // semicolon (firefox), equals, = 59
  707. // <, = 60
  708. // equals (firefox), = 61
  709. // ß, = 63
  710. // @ (firefox), = 64
  711. A = 65,
  712. B = 66,
  713. C = 67,
  714. D = 68,
  715. E = 69,
  716. F = 70,
  717. G = 71,
  718. H = 72,
  719. I = 73,
  720. J = 74,
  721. K = 75,
  722. L = 76,
  723. M = 77,
  724. N = 78,
  725. O = 79,
  726. P = 80,
  727. Q = 81,
  728. R = 82,
  729. S = 83,
  730. T = 84,
  731. U = 85,
  732. V = 86,
  733. W = 87,
  734. X = 88,
  735. Y = 89,
  736. Z = 90,
  737. LeftWindow = 91,
  738. RightWindow = 92,
  739. SelectKey = 93,
  740. Numpad0 = 96,
  741. Numpad1 = 97,
  742. Numpad2 = 98,
  743. Numpad3 = 99,
  744. Numpad4 = 100,
  745. Numpad5 = 101,
  746. Numpad6 = 102,
  747. Numpad7 = 103,
  748. Numpad8 = 104,
  749. Numpad9 = 105,
  750. Multiply = 106,
  751. Add = 107,
  752. Subtract = 109,
  753. DecimalPoint = 110,
  754. Divide = 111,
  755. F1 = 112,
  756. F2 = 113,
  757. F3 = 114,
  758. F4 = 115,
  759. F5 = 116,
  760. F6 = 117,
  761. F7 = 118,
  762. F8 = 119,
  763. F9 = 120,
  764. F10 = 121,
  765. F11 = 122,
  766. F12 = 123,
  767. // f13, = 124
  768. // f14, = 125
  769. // f15, = 126
  770. // f16, = 127
  771. // f17, = 128
  772. // f18, = 129
  773. // f19, = 130
  774. // f20, = 131
  775. // f21, = 132
  776. // f22, = 133
  777. // f23, = 134
  778. // f24, = 135
  779. // f25, = 136
  780. // f26, = 137
  781. // f27, = 138
  782. // f28, = 139
  783. // f29, = 140
  784. // f30, = 141
  785. // f31, = 142
  786. // f32, = 143
  787. NumLock = 144,
  788. ScrollLock = 145,
  789. // airplane mode, = 151
  790. // ^, = 160
  791. // !, = 161
  792. // ؛ (arabic semicolon), = 162
  793. // #, = 163
  794. // $, = 164
  795. // ù, = 165
  796. // page backward, = 166
  797. // page forward, = 167
  798. // refresh, = 168
  799. // closing paren (AZERTY), = 169
  800. // *, = 170
  801. // ~ + * key, = 171
  802. // home key, = 172
  803. // minus (firefox), mute/unmute, = 173
  804. // decrease volume level, = 174
  805. // increase volume level, = 175
  806. // next, = 176
  807. // previous, = 177
  808. // stop, = 178
  809. // play/pause, = 179
  810. // e-mail, = 180
  811. // mute/unmute (firefox), = 181
  812. // decrease volume level (firefox), = 182
  813. // increase volume level (firefox), = 183
  814. Semicolon = 186,
  815. EqualSign = 187,
  816. Comma = 188,
  817. Dash = 189,
  818. Period = 190,
  819. ForwardSlash = 191,
  820. GraveAccent = 192,
  821. // ?, / or °, = 193
  822. // numpad period (chrome), = 194
  823. OpenBracket = 219,
  824. BackSlash = 220,
  825. CloseBraket = 221,
  826. SingleQuote = 222,
  827. // `, = 223
  828. // left or right ⌘ key (firefox), = 224
  829. // altgr, = 225
  830. // < /git >, left back slash, = 226
  831. // GNOME Compose Key, = 230
  832. // ç, = 231
  833. // XF86Forward, = 233
  834. // XF86Back, = 234
  835. // non-conversion, = 235
  836. // alphanumeric, = 240
  837. // hiragana/katakana, = 242
  838. // half-width/full-width, = 243
  839. // kanji, = 244
  840. // unlock trackpad (Chrome/Edge), = 251
  841. // toggle touchpad, = 255
  842. #[cfg_attr(feature = "serialize", serde(other))]
  843. Unknown,
  844. }
  845. impl KeyCode {
  846. pub fn from_raw_code(i: u8) -> Self {
  847. use KeyCode::*;
  848. match i {
  849. 8 => Backspace,
  850. 9 => Tab,
  851. 13 => Enter,
  852. 16 => Shift,
  853. 17 => Ctrl,
  854. 18 => Alt,
  855. 19 => Pause,
  856. 20 => CapsLock,
  857. 27 => Escape,
  858. 33 => PageUp,
  859. 34 => PageDown,
  860. 35 => End,
  861. 36 => Home,
  862. 37 => LeftArrow,
  863. 38 => UpArrow,
  864. 39 => RightArrow,
  865. 40 => DownArrow,
  866. 45 => Insert,
  867. 46 => Delete,
  868. 48 => Num0,
  869. 49 => Num1,
  870. 50 => Num2,
  871. 51 => Num3,
  872. 52 => Num4,
  873. 53 => Num5,
  874. 54 => Num6,
  875. 55 => Num7,
  876. 56 => Num8,
  877. 57 => Num9,
  878. 65 => A,
  879. 66 => B,
  880. 67 => C,
  881. 68 => D,
  882. 69 => E,
  883. 70 => F,
  884. 71 => G,
  885. 72 => H,
  886. 73 => I,
  887. 74 => J,
  888. 75 => K,
  889. 76 => L,
  890. 77 => M,
  891. 78 => N,
  892. 79 => O,
  893. 80 => P,
  894. 81 => Q,
  895. 82 => R,
  896. 83 => S,
  897. 84 => T,
  898. 85 => U,
  899. 86 => V,
  900. 87 => W,
  901. 88 => X,
  902. 89 => Y,
  903. 90 => Z,
  904. 91 => LeftWindow,
  905. 92 => RightWindow,
  906. 93 => SelectKey,
  907. 96 => Numpad0,
  908. 97 => Numpad1,
  909. 98 => Numpad2,
  910. 99 => Numpad3,
  911. 100 => Numpad4,
  912. 101 => Numpad5,
  913. 102 => Numpad6,
  914. 103 => Numpad7,
  915. 104 => Numpad8,
  916. 105 => Numpad9,
  917. 106 => Multiply,
  918. 107 => Add,
  919. 109 => Subtract,
  920. 110 => DecimalPoint,
  921. 111 => Divide,
  922. 112 => F1,
  923. 113 => F2,
  924. 114 => F3,
  925. 115 => F4,
  926. 116 => F5,
  927. 117 => F6,
  928. 118 => F7,
  929. 119 => F8,
  930. 120 => F9,
  931. 121 => F10,
  932. 122 => F11,
  933. 123 => F12,
  934. 144 => NumLock,
  935. 145 => ScrollLock,
  936. 186 => Semicolon,
  937. 187 => EqualSign,
  938. 188 => Comma,
  939. 189 => Dash,
  940. 190 => Period,
  941. 191 => ForwardSlash,
  942. 192 => GraveAccent,
  943. 219 => OpenBracket,
  944. 220 => BackSlash,
  945. 221 => CloseBraket,
  946. 222 => SingleQuote,
  947. _ => Unknown,
  948. }
  949. }
  950. // get the raw code
  951. pub fn raw_code(&self) -> u32 {
  952. *self as u32
  953. }
  954. }
  955. pub(crate) fn _event_meta(event: &UserEvent) -> (bool, EventPriority) {
  956. use EventPriority::*;
  957. match event.name {
  958. // clipboard
  959. "copy" | "cut" | "paste" => (true, Medium),
  960. // Composition
  961. "compositionend" | "compositionstart" | "compositionupdate" => (true, Low),
  962. // Keyboard
  963. "keydown" | "keypress" | "keyup" => (true, High),
  964. // Focus
  965. "focus" | "blur" => (true, Low),
  966. // Form
  967. "change" | "input" | "invalid" | "reset" | "submit" => (true, Medium),
  968. // Mouse
  969. "click" | "contextmenu" | "doubleclick" | "drag" | "dragend" | "dragenter" | "dragexit"
  970. | "dragleave" | "dragover" | "dragstart" | "drop" | "mousedown" | "mouseenter"
  971. | "mouseleave" | "mouseout" | "mouseover" | "mouseup" => (true, High),
  972. "mousemove" => (false, Medium),
  973. // Pointer
  974. "pointerdown" | "pointermove" | "pointerup" | "pointercancel" | "gotpointercapture"
  975. | "lostpointercapture" | "pointerenter" | "pointerleave" | "pointerover" | "pointerout" => {
  976. (true, Medium)
  977. }
  978. // Selection
  979. "select" | "touchcancel" | "touchend" => (true, Medium),
  980. // Touch
  981. "touchmove" | "touchstart" => (true, Medium),
  982. // Wheel
  983. "scroll" | "wheel" => (false, Medium),
  984. // Media
  985. "abort" | "canplay" | "canplaythrough" | "durationchange" | "emptied" | "encrypted"
  986. | "ended" | "error" | "loadeddata" | "loadedmetadata" | "loadstart" | "pause" | "play"
  987. | "playing" | "progress" | "ratechange" | "seeked" | "seeking" | "stalled" | "suspend"
  988. | "timeupdate" | "volumechange" | "waiting" => (true, Medium),
  989. // Animation
  990. "animationstart" | "animationend" | "animationiteration" => (true, Medium),
  991. // Transition
  992. "transitionend" => (true, Medium),
  993. // Toggle
  994. "toggle" => (true, Medium),
  995. _ => (true, Low),
  996. }
  997. }