keymap.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #
  2. # https://thanhle.me/make-raspberry-pi3-as-an-emulator-bluetooth-keyboard/
  3. #
  4. #
  5. #
  6. # Convert value returned from Linux event device ("evdev") to a HID code. This
  7. # is reverse of what's actually hardcoded in the kernel.
  8. #
  9. # Thanh Le <quangthanh010290@gmail.com>
  10. # License: GPL
  11. #
  12. # Ported to a Python module by Thanh Le
  13. #
  14. keytable = {
  15. "KEY_RESERVED" : 0,
  16. "KEY_ESC" : 41,
  17. "KEY_1" : 30,
  18. "KEY_2" : 31,
  19. "KEY_3" : 32,
  20. "KEY_4" : 33,
  21. "KEY_5" : 34,
  22. "KEY_6" : 35,
  23. "KEY_7" : 36,
  24. "KEY_8" : 37,
  25. "KEY_9" : 38,
  26. "KEY_0" : 39,
  27. "KEY_MINUS" : 45,
  28. "KEY_EQUAL" : 46,
  29. "KEY_BACKSPACE" : 42,
  30. "KEY_TAB" : 43,
  31. "KEY_Q" : 20,
  32. "KEY_W" : 26,
  33. "KEY_E" : 8,
  34. "KEY_R" : 21,
  35. "KEY_T" : 23,
  36. "KEY_Y" : 28,
  37. "KEY_U" : 24,
  38. "KEY_I" : 12,
  39. "KEY_O" : 18,
  40. "KEY_P" : 19,
  41. "KEY_LEFTBRACE" : 47,
  42. "KEY_RIGHTBRACE" : 48,
  43. "KEY_ENTER" : 40,
  44. "KEY_LEFTCTRL" : 224,
  45. "KEY_A" : 4,
  46. "KEY_S" : 22,
  47. "KEY_D" : 7,
  48. "KEY_F" : 9,
  49. "KEY_G" : 10,
  50. "KEY_H" : 11,
  51. "KEY_J" : 13,
  52. "KEY_K" : 14,
  53. "KEY_L" : 15,
  54. "KEY_SEMICOLON" : 51,
  55. "KEY_APOSTROPHE" : 52,
  56. "KEY_GRAVE" : 53,
  57. "KEY_LEFTSHIFT" : 225,
  58. "KEY_BACKSLASH" : 50,
  59. "KEY_Z" : 29,
  60. "KEY_X" : 27,
  61. "KEY_C" : 6,
  62. "KEY_V" : 25,
  63. "KEY_B" : 5,
  64. "KEY_N" : 17,
  65. "KEY_M" : 16,
  66. "KEY_COMMA" : 54,
  67. "KEY_DOT" : 55,
  68. "KEY_SLASH" : 56,
  69. "KEY_RIGHTSHIFT" : 229,
  70. "KEY_KPASTERISK" : 85,
  71. "KEY_LEFTALT" : 226,
  72. "KEY_SPACE" : 44,
  73. "KEY_CAPSLOCK" : 57,
  74. "KEY_F1" : 58,
  75. "KEY_F2" : 59,
  76. "KEY_F3" : 60,
  77. "KEY_F4" : 61,
  78. "KEY_F5" : 62,
  79. "KEY_F6" : 63,
  80. "KEY_F7" : 64,
  81. "KEY_F8" : 65,
  82. "KEY_F9" : 66,
  83. "KEY_F10" : 67,
  84. "KEY_NUMLOCK" : 83,
  85. "KEY_SCROLLLOCK" : 71,
  86. "KEY_KP7" : 95,
  87. "KEY_KP8" : 96,
  88. "KEY_KP9" : 97,
  89. "KEY_KPMINUS" : 86,
  90. "KEY_KP4" : 92,
  91. "KEY_KP5" : 93,
  92. "KEY_KP6" : 94,
  93. "KEY_KPPLUS" : 87,
  94. "KEY_KP1" : 89,
  95. "KEY_KP2" : 90,
  96. "KEY_KP3" : 91,
  97. "KEY_KP0" : 98,
  98. "KEY_KPDOT" : 99,
  99. "KEY_ZENKAKUHANKAKU" : 148,
  100. "KEY_102ND" : 100,
  101. "KEY_F11" : 68,
  102. "KEY_F12" : 69,
  103. "KEY_RO" : 135,
  104. "KEY_KATAKANA" : 146,
  105. "KEY_HIRAGANA" : 147,
  106. "KEY_HENKAN" : 138,
  107. "KEY_KATAKANAHIRAGANA" : 136,
  108. "KEY_MUHENKAN" : 139,
  109. "KEY_KPJPCOMMA" : 140,
  110. "KEY_KPENTER" : 88,
  111. "KEY_RIGHTCTRL" : 228,
  112. "KEY_KPSLASH" : 84,
  113. "KEY_SYSRQ" : 70,
  114. "KEY_RIGHTALT" : 230,
  115. "KEY_HOME" : 74,
  116. "KEY_UP" : 82,
  117. "KEY_PAGEUP" : 75,
  118. "KEY_LEFT" : 80,
  119. "KEY_RIGHT" : 79,
  120. "KEY_END" : 77,
  121. "KEY_DOWN" : 81,
  122. "KEY_PAGEDOWN" : 78,
  123. "KEY_INSERT" : 73,
  124. "KEY_DELETE" : 76,
  125. "KEY_MUTE" : 239,
  126. "KEY_VOLUMEDOWN" : 238,
  127. "KEY_VOLUMEUP" : 237,
  128. "KEY_POWER" : 102,
  129. "KEY_KPEQUAL" : 103,
  130. "KEY_PAUSE" : 72,
  131. "KEY_KPCOMMA" : 133,
  132. "KEY_HANGEUL" : 144,
  133. "KEY_HANJA" : 145,
  134. "KEY_YEN" : 137,
  135. "KEY_LEFTMETA" : 227,
  136. "KEY_RIGHTMETA" : 231,
  137. "KEY_COMPOSE" : 101,
  138. "KEY_STOP" : 243,
  139. "KEY_AGAIN" : 121,
  140. "KEY_PROPS" : 118,
  141. "KEY_UNDO" : 122,
  142. "KEY_FRONT" : 119,
  143. "KEY_COPY" : 124,
  144. "KEY_OPEN" : 116,
  145. "KEY_PASTE" : 125,
  146. "KEY_FIND" : 244,
  147. "KEY_CUT" : 123,
  148. "KEY_HELP" : 117,
  149. "KEY_CALC" : 251,
  150. "KEY_SLEEP" : 248,
  151. "KEY_WWW" : 240,
  152. "KEY_COFFEE" : 249,
  153. "KEY_BACK" : 241,
  154. "KEY_FORWARD" : 242,
  155. "KEY_EJECTCD" : 236,
  156. "KEY_NEXTSONG" : 235,
  157. "KEY_PLAYPAUSE" : 232,
  158. "KEY_PREVIOUSSONG" : 234,
  159. "KEY_STOPCD" : 233,
  160. "KEY_REFRESH" : 250,
  161. "KEY_EDIT" : 247,
  162. "KEY_SCROLLUP" : 245,
  163. "KEY_SCROLLDOWN" : 246,
  164. "KEY_F13" : 104,
  165. "KEY_F14" : 105,
  166. "KEY_F15" : 106,
  167. "KEY_F16" : 107,
  168. "KEY_F17" : 108,
  169. "KEY_F18" : 109,
  170. "KEY_F19" : 110,
  171. "KEY_F20" : 111,
  172. "KEY_F21" : 112,
  173. "KEY_F22" : 113,
  174. "KEY_F23" : 114,
  175. "KEY_F24" : 115
  176. }
  177. # Map modifier keys to array element in the bit array
  178. modkeys = {
  179. "KEY_RIGHTMETA" : 0,
  180. "KEY_RIGHTALT" : 1,
  181. "KEY_RIGHTSHIFT" : 2,
  182. "KEY_RIGHTCTRL" : 3,
  183. "KEY_LEFTMETA" : 4,
  184. "KEY_LEFTALT": 5,
  185. "KEY_LEFTSHIFT": 6,
  186. "KEY_LEFTCTRL": 7
  187. }
  188. def convert(evdev_keycode):
  189. if not isinstance(evdev_keycode, list):
  190. evdev_keycode = [evdev_keycode]
  191. for keycode in evdev_keycode:
  192. if keycode in keytable:
  193. return keytable[keycode]
  194. return -1
  195. def modkey(evdev_keycode):
  196. if not isinstance(evdev_keycode, list):
  197. evdev_keycode = [evdev_keycode]
  198. for keycode in evdev_keycode:
  199. if keycode in modkeys:
  200. return modkeys[keycode]
  201. return -1 # Return an invalid array element