events.rs 28 KB

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