keysym.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. use miniquad::{
  2. KeyCode, MouseButton
  3. };
  4. pub trait KeyCodeAsStr {
  5. fn to_str(&self) -> &str;
  6. }
  7. impl KeyCodeAsStr for KeyCode {
  8. fn to_str(&self) -> &str {
  9. match self {
  10. Self::Space => " ",
  11. Self::Apostrophe => "'",
  12. Self::Comma => ",",
  13. Self::Minus => "-",
  14. Self::Period => ".",
  15. Self::Slash => "/",
  16. Self::Key0 => "0",
  17. Self::Key1 => "1",
  18. Self::Key2 => "2",
  19. Self::Key3 => "3",
  20. Self::Key4 => "4",
  21. Self::Key5 => "5",
  22. Self::Key6 => "6",
  23. Self::Key7 => "7",
  24. Self::Key8 => "8",
  25. Self::Key9 => "9",
  26. Self::Semicolon => ";",
  27. Self::Equal => "=",
  28. Self::A => "A",
  29. Self::B => "B",
  30. Self::C => "C",
  31. Self::D => "D",
  32. Self::E => "E",
  33. Self::F => "F",
  34. Self::G => "G",
  35. Self::H => "H",
  36. Self::I => "I",
  37. Self::J => "J",
  38. Self::K => "K",
  39. Self::L => "L",
  40. Self::M => "M",
  41. Self::N => "N",
  42. Self::O => "O",
  43. Self::P => "P",
  44. Self::Q => "Q",
  45. Self::R => "R",
  46. Self::S => "S",
  47. Self::T => "T",
  48. Self::U => "U",
  49. Self::V => "V",
  50. Self::W => "W",
  51. Self::X => "X",
  52. Self::Y => "Y",
  53. Self::Z => "Z",
  54. Self::LeftBracket => "(",
  55. Self::Backslash => "\\",
  56. Self::RightBracket => ")",
  57. Self::GraveAccent => "`",
  58. Self::World1 => "World1",
  59. Self::World2 => "World2",
  60. Self::Escape => "Escape",
  61. Self::Enter => "\n",
  62. Self::Tab => "Tab",
  63. Self::Backspace => "Backspace",
  64. Self::Insert => "Insert",
  65. Self::Delete => "Delete",
  66. Self::Right => "Right",
  67. Self::Left => "Left",
  68. Self::Down => "Down",
  69. Self::Up => "Up",
  70. Self::PageUp => "PageUp",
  71. Self::PageDown => "PageDown",
  72. Self::Home => "Home",
  73. Self::End => "End",
  74. Self::CapsLock => "CapsLock",
  75. Self::ScrollLock => "ScrollLock",
  76. Self::NumLock => "NumLock",
  77. Self::PrintScreen => "PrintScreen",
  78. Self::Pause => "Pause",
  79. Self::F1 => "F1",
  80. Self::F2 => "F2",
  81. Self::F3 => "F3",
  82. Self::F4 => "F4",
  83. Self::F5 => "F5",
  84. Self::F6 => "F6",
  85. Self::F7 => "F7",
  86. Self::F8 => "F8",
  87. Self::F9 => "F9",
  88. Self::F10 => "F10",
  89. Self::F11 => "F11",
  90. Self::F12 => "F12",
  91. Self::F13 => "F13",
  92. Self::F14 => "F14",
  93. Self::F15 => "F15",
  94. Self::F16 => "F16",
  95. Self::F17 => "F17",
  96. Self::F18 => "F18",
  97. Self::F19 => "F19",
  98. Self::F20 => "F20",
  99. Self::F21 => "F21",
  100. Self::F22 => "F22",
  101. Self::F23 => "F23",
  102. Self::F24 => "F24",
  103. Self::F25 => "F25",
  104. Self::Kp0 => "Kp0",
  105. Self::Kp1 => "Kp1",
  106. Self::Kp2 => "Kp2",
  107. Self::Kp3 => "Kp3",
  108. Self::Kp4 => "Kp4",
  109. Self::Kp5 => "Kp5",
  110. Self::Kp6 => "Kp6",
  111. Self::Kp7 => "Kp7",
  112. Self::Kp8 => "Kp8",
  113. Self::Kp9 => "Kp9",
  114. Self::KpDecimal => "KpDecimal",
  115. Self::KpDivide => "KpDivide",
  116. Self::KpMultiply => "KpMultiply",
  117. Self::KpSubtract => "KpSubtract",
  118. Self::KpAdd => "KpAdd",
  119. Self::KpEnter => "KpEnter",
  120. Self::KpEqual => "KpEqual",
  121. Self::LeftShift => "LeftShift",
  122. Self::LeftControl => "LeftControl",
  123. Self::LeftAlt => "LeftAlt",
  124. Self::LeftSuper => "LeftSuper",
  125. Self::RightShift => "RightShift",
  126. Self::RightControl => "RightControl",
  127. Self::RightAlt => "RightAlt",
  128. Self::RightSuper => "RightSuper",
  129. Self::Menu => "Menu",
  130. Self::Unknown => "Unknown",
  131. }
  132. }
  133. }
  134. pub trait KeyCodeAsU16 {
  135. fn to_u16(&self) -> u16;
  136. fn from_u16(keysym: u16) -> Self;
  137. }
  138. impl KeyCodeAsU16 for KeyCode {
  139. fn to_u16(&self) -> u16 {
  140. match self {
  141. Self::Space => 0x0020,
  142. Self::Apostrophe => 0x0027,
  143. Self::Comma => 0x002c,
  144. Self::Minus => 0x002d,
  145. Self::Period => 0x002e,
  146. Self::Slash => 0x002f,
  147. Self::Key0 => 0x0030,
  148. Self::Key1 => 0x0031,
  149. Self::Key2 => 0x0032,
  150. Self::Key3 => 0x0033,
  151. Self::Key4 => 0x0034,
  152. Self::Key5 => 0x0035,
  153. Self::Key6 => 0x0036,
  154. Self::Key7 => 0x0037,
  155. Self::Key8 => 0x0038,
  156. Self::Key9 => 0x0039,
  157. Self::Semicolon => 0x003b,
  158. Self::Equal => 0x003d,
  159. Self::A => 0x0041,
  160. Self::B => 0x0042,
  161. Self::C => 0x0043,
  162. Self::D => 0x0044,
  163. Self::E => 0x0045,
  164. Self::F => 0x0046,
  165. Self::G => 0x0047,
  166. Self::H => 0x0048,
  167. Self::I => 0x0049,
  168. Self::J => 0x004a,
  169. Self::K => 0x004b,
  170. Self::L => 0x004c,
  171. Self::M => 0x004d,
  172. Self::N => 0x004e,
  173. Self::O => 0x004f,
  174. Self::P => 0x0050,
  175. Self::Q => 0x0051,
  176. Self::R => 0x0052,
  177. Self::S => 0x0053,
  178. Self::T => 0x0054,
  179. Self::U => 0x0055,
  180. Self::V => 0x0056,
  181. Self::W => 0x0057,
  182. Self::X => 0x0058,
  183. Self::Y => 0x0059,
  184. Self::Z => 0x005a,
  185. Self::LeftBracket => 0x005b,
  186. Self::Backslash => 0x005c,
  187. Self::RightBracket => 0x005d,
  188. Self::GraveAccent => 0x0060,
  189. Self::World1 => 0x0100,
  190. Self::World2 => 0x0101,
  191. Self::Escape => 0xff1b,
  192. Self::Enter => 0xff0d,
  193. Self::Tab => 0xff09,
  194. Self::Backspace => 0xff08,
  195. Self::Insert => 0xff63,
  196. Self::Delete => 0xffff,
  197. Self::Right => 0xff53,
  198. Self::Left => 0xff51,
  199. Self::Down => 0xff54,
  200. Self::Up => 0xff52,
  201. Self::PageUp => 0xff55,
  202. Self::PageDown => 0xff56,
  203. Self::Home => 0xff50,
  204. Self::End => 0xff57,
  205. Self::CapsLock => 0xffe5,
  206. Self::ScrollLock => 0xff14,
  207. Self::NumLock => 0xff7f,
  208. Self::PrintScreen => 0xfd1d,
  209. Self::Pause => 0xff13,
  210. Self::F1 => 0xffbe,
  211. Self::F2 => 0xffbf,
  212. Self::F3 => 0xffc0,
  213. Self::F4 => 0xffc1,
  214. Self::F5 => 0xffc2,
  215. Self::F6 => 0xffc3,
  216. Self::F7 => 0xffc4,
  217. Self::F8 => 0xffc5,
  218. Self::F9 => 0xffc6,
  219. Self::F10 => 0xffc7,
  220. Self::F11 => 0xffc8,
  221. Self::F12 => 0xffc9,
  222. Self::F13 => 0xffca,
  223. Self::F14 => 0xffcb,
  224. Self::F15 => 0xffcc,
  225. Self::F16 => 0xffcd,
  226. Self::F17 => 0xffce,
  227. Self::F18 => 0xffcf,
  228. Self::F19 => 0xffd0,
  229. Self::F20 => 0xffd1,
  230. Self::F21 => 0xffd2,
  231. Self::F22 => 0xffd3,
  232. Self::F23 => 0xffd4,
  233. Self::F24 => 0xffd5,
  234. Self::F25 => 0xffd6,
  235. Self::Kp0 => 0xffb0,
  236. Self::Kp1 => 0xffb1,
  237. Self::Kp2 => 0xffb2,
  238. Self::Kp3 => 0xffb3,
  239. Self::Kp4 => 0xffb4,
  240. Self::Kp5 => 0xffb5,
  241. Self::Kp6 => 0xffb6,
  242. Self::Kp7 => 0xffb7,
  243. Self::Kp8 => 0xffb8,
  244. Self::Kp9 => 0xffb9,
  245. Self::KpDecimal => 0xffae,
  246. Self::KpDivide => 0xffaf,
  247. Self::KpMultiply => 0xffaa,
  248. Self::KpSubtract => 0xffad,
  249. Self::KpAdd => 0xffab,
  250. Self::KpEnter => 0xff8d,
  251. Self::KpEqual => 0xffbd,
  252. Self::LeftShift => 0xffe1,
  253. Self::LeftControl => 0xffe3,
  254. Self::LeftAlt => 0xffe9,
  255. Self::LeftSuper => 0xffeb,
  256. Self::RightShift => 0xffe2,
  257. Self::RightControl => 0xffe4,
  258. Self::RightAlt => 0xffea,
  259. Self::RightSuper => 0xffec,
  260. Self::Menu => 0xff67,
  261. Self::Unknown => 0x01ff,
  262. }
  263. }
  264. fn from_u16(keysym: u16) -> Self {
  265. match keysym {
  266. 0x0020 => Self::Space,
  267. 0x0027 => Self::Apostrophe,
  268. 0x002c => Self::Comma,
  269. 0x002d => Self::Minus,
  270. 0x002e => Self::Period,
  271. 0x002f => Self::Slash,
  272. 0x0030 => Self::Key0,
  273. 0x0031 => Self::Key1,
  274. 0x0032 => Self::Key2,
  275. 0x0033 => Self::Key3,
  276. 0x0034 => Self::Key4,
  277. 0x0035 => Self::Key5,
  278. 0x0036 => Self::Key6,
  279. 0x0037 => Self::Key7,
  280. 0x0038 => Self::Key8,
  281. 0x0039 => Self::Key9,
  282. 0x003b => Self::Semicolon,
  283. 0x003d => Self::Equal,
  284. 0x0041 => Self::A,
  285. 0x0042 => Self::B,
  286. 0x0043 => Self::C,
  287. 0x0044 => Self::D,
  288. 0x0045 => Self::E,
  289. 0x0046 => Self::F,
  290. 0x0047 => Self::G,
  291. 0x0048 => Self::H,
  292. 0x0049 => Self::I,
  293. 0x004a => Self::J,
  294. 0x004b => Self::K,
  295. 0x004c => Self::L,
  296. 0x004d => Self::M,
  297. 0x004e => Self::N,
  298. 0x004f => Self::O,
  299. 0x0050 => Self::P,
  300. 0x0051 => Self::Q,
  301. 0x0052 => Self::R,
  302. 0x0053 => Self::S,
  303. 0x0054 => Self::T,
  304. 0x0055 => Self::U,
  305. 0x0056 => Self::V,
  306. 0x0057 => Self::W,
  307. 0x0058 => Self::X,
  308. 0x0059 => Self::Y,
  309. 0x005a => Self::Z,
  310. 0x005b => Self::LeftBracket,
  311. 0x005c => Self::Backslash,
  312. 0x005d => Self::RightBracket,
  313. 0x0060 => Self::GraveAccent,
  314. 0x0100 => Self::World1,
  315. 0x0101 => Self::World2,
  316. 0xff1b => Self::Escape,
  317. 0xff0d => Self::Enter,
  318. 0xff09 => Self::Tab,
  319. 0xff08 => Self::Backspace,
  320. 0xff63 => Self::Insert,
  321. 0xffff => Self::Delete,
  322. 0xff53 => Self::Right,
  323. 0xff51 => Self::Left,
  324. 0xff54 => Self::Down,
  325. 0xff52 => Self::Up,
  326. 0xff55 => Self::PageUp,
  327. 0xff56 => Self::PageDown,
  328. 0xff50 => Self::Home,
  329. 0xff57 => Self::End,
  330. 0xffe5 => Self::CapsLock,
  331. 0xff14 => Self::ScrollLock,
  332. 0xff7f => Self::NumLock,
  333. 0xfd1d => Self::PrintScreen,
  334. 0xff13 => Self::Pause,
  335. 0xffbe => Self::F1,
  336. 0xffbf => Self::F2,
  337. 0xffc0 => Self::F3,
  338. 0xffc1 => Self::F4,
  339. 0xffc2 => Self::F5,
  340. 0xffc3 => Self::F6,
  341. 0xffc4 => Self::F7,
  342. 0xffc5 => Self::F8,
  343. 0xffc6 => Self::F9,
  344. 0xffc7 => Self::F10,
  345. 0xffc8 => Self::F11,
  346. 0xffc9 => Self::F12,
  347. 0xffca => Self::F13,
  348. 0xffcb => Self::F14,
  349. 0xffcc => Self::F15,
  350. 0xffcd => Self::F16,
  351. 0xffce => Self::F17,
  352. 0xffcf => Self::F18,
  353. 0xffd0 => Self::F19,
  354. 0xffd1 => Self::F20,
  355. 0xffd2 => Self::F21,
  356. 0xffd3 => Self::F22,
  357. 0xffd4 => Self::F23,
  358. 0xffd5 => Self::F24,
  359. 0xffd6 => Self::F25,
  360. 0xffb0 => Self::Kp0,
  361. 0xffb1 => Self::Kp1,
  362. 0xffb2 => Self::Kp2,
  363. 0xffb3 => Self::Kp3,
  364. 0xffb4 => Self::Kp4,
  365. 0xffb5 => Self::Kp5,
  366. 0xffb6 => Self::Kp6,
  367. 0xffb7 => Self::Kp7,
  368. 0xffb8 => Self::Kp8,
  369. 0xffb9 => Self::Kp9,
  370. 0xffae => Self::KpDecimal,
  371. 0xffaf => Self::KpDivide,
  372. 0xffaa => Self::KpMultiply,
  373. 0xffad => Self::KpSubtract,
  374. 0xffab => Self::KpAdd,
  375. 0xff8d => Self::KpEnter,
  376. 0xffbd => Self::KpEqual,
  377. 0xffe1 => Self::LeftShift,
  378. 0xffe3 => Self::LeftControl,
  379. 0xffe9 => Self::LeftAlt,
  380. 0xffeb => Self::LeftSuper,
  381. 0xffe2 => Self::RightShift,
  382. 0xffe4 => Self::RightControl,
  383. 0xffea => Self::RightAlt,
  384. 0xffec => Self::RightSuper,
  385. 0xff67 => Self::Menu,
  386. _ => Self::Unknown,
  387. }
  388. }
  389. }
  390. pub trait MouseButtonAsU8 {
  391. fn to_u8(&self) -> u8;
  392. fn from_u8(btn: u8) -> Self;
  393. }
  394. impl MouseButtonAsU8 for MouseButton {
  395. fn to_u8(&self) -> u8 {
  396. match self {
  397. Self::Left => 0,
  398. Self::Middle => 1,
  399. Self::Right => 2,
  400. Self::Unknown => 3,
  401. }
  402. }
  403. fn from_u8(btn: u8) -> Self {
  404. match btn {
  405. 0 => Self::Left,
  406. 1 => Self::Middle,
  407. 2 => Self::Right,
  408. _ => Self::Unknown,
  409. }
  410. }
  411. }