events.rs 28 KB

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