events.rs 29 KB

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