keyboard.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. // Usful regexp for KATE: ALLEGRO_([A-Z0-9_]+)(.*) -> \1 = C.ALLEGRO_\1
  9. // Keyboard functionality.
  10. // Checks if the Allegro keyboard module is installed or not.
  11. func IsKeyboardInstalled() bool {
  12. return bool(C.al_is_keyboard_installed())
  13. }
  14. // Installs the Allegro keyboard module.
  15. func InstallKeyboard() bool {
  16. return bool(C.al_install_keyboard())
  17. }
  18. // Uninstalls the Allegro keyboard module.
  19. func UninstallKeyboard() {
  20. C.al_uninstall_keyboard()
  21. }
  22. // Gets the name of te given keucode as a string.
  23. func KeycodeToName(keycode int) string {
  24. return gostr(C.al_keycode_to_name(C.int(keycode)))
  25. }
  26. // KeyboardState isn't very interesting for now, so don't wrap it
  27. // and let KeyDown work as a global function
  28. // Gets the state of a given keyboard key by keycode. True is down, false is up.
  29. func KeyDown(keycode int) bool {
  30. state := &C.ALLEGRO_KEYBOARD_STATE{}
  31. C.al_get_keyboard_state(state)
  32. return bool(C.al_key_down(state, C.int(keycode)))
  33. }
  34. // Sets the state of the leds on the keyboard.
  35. func SetKeyboardLeds(leds int) bool {
  36. return bool(C.al_set_keyboard_leds(C.int(leds)))
  37. }
  38. // Returns the Keyboard event source that can be registered to an EventQueue
  39. // with RegisterEventSource.
  40. func KeyboardEventSource() *EventSource {
  41. return (*EventSource)(C.al_get_keyboard_event_source())
  42. }
  43. // Keyboard constants
  44. const (
  45. KEY_A = C.ALLEGRO_KEY_A
  46. KEY_B = C.ALLEGRO_KEY_B
  47. KEY_C = C.ALLEGRO_KEY_C
  48. KEY_D = C.ALLEGRO_KEY_D
  49. KEY_E = C.ALLEGRO_KEY_E
  50. KEY_F = C.ALLEGRO_KEY_F
  51. KEY_G = C.ALLEGRO_KEY_G
  52. KEY_H = C.ALLEGRO_KEY_H
  53. KEY_I = C.ALLEGRO_KEY_I
  54. KEY_J = C.ALLEGRO_KEY_J
  55. KEY_K = C.ALLEGRO_KEY_K
  56. KEY_L = C.ALLEGRO_KEY_L
  57. KEY_M = C.ALLEGRO_KEY_M
  58. KEY_N = C.ALLEGRO_KEY_N
  59. KEY_O = C.ALLEGRO_KEY_O
  60. KEY_P = C.ALLEGRO_KEY_P
  61. KEY_Q = C.ALLEGRO_KEY_Q
  62. KEY_R = C.ALLEGRO_KEY_R
  63. KEY_S = C.ALLEGRO_KEY_S
  64. KEY_T = C.ALLEGRO_KEY_T
  65. KEY_U = C.ALLEGRO_KEY_U
  66. KEY_V = C.ALLEGRO_KEY_V
  67. KEY_W = C.ALLEGRO_KEY_W
  68. KEY_X = C.ALLEGRO_KEY_X
  69. KEY_Y = C.ALLEGRO_KEY_Y
  70. KEY_Z = C.ALLEGRO_KEY_Z
  71. KEY_0 = C.ALLEGRO_KEY_0
  72. KEY_1 = C.ALLEGRO_KEY_1
  73. KEY_2 = C.ALLEGRO_KEY_2
  74. KEY_3 = C.ALLEGRO_KEY_3
  75. KEY_4 = C.ALLEGRO_KEY_4
  76. KEY_5 = C.ALLEGRO_KEY_5
  77. KEY_6 = C.ALLEGRO_KEY_6
  78. KEY_7 = C.ALLEGRO_KEY_7
  79. KEY_8 = C.ALLEGRO_KEY_8
  80. KEY_9 = C.ALLEGRO_KEY_9
  81. KEY_PAD_0 = C.ALLEGRO_KEY_PAD_0
  82. KEY_PAD_1 = C.ALLEGRO_KEY_PAD_1
  83. KEY_PAD_2 = C.ALLEGRO_KEY_PAD_2
  84. KEY_PAD_3 = C.ALLEGRO_KEY_PAD_3
  85. KEY_PAD_4 = C.ALLEGRO_KEY_PAD_4
  86. KEY_PAD_5 = C.ALLEGRO_KEY_PAD_5
  87. KEY_PAD_6 = C.ALLEGRO_KEY_PAD_6
  88. KEY_PAD_7 = C.ALLEGRO_KEY_PAD_7
  89. KEY_PAD_8 = C.ALLEGRO_KEY_PAD_8
  90. KEY_PAD_9 = C.ALLEGRO_KEY_PAD_9
  91. KEY_F1 = C.ALLEGRO_KEY_F1
  92. KEY_F2 = C.ALLEGRO_KEY_F2
  93. KEY_F3 = C.ALLEGRO_KEY_F3
  94. KEY_F4 = C.ALLEGRO_KEY_F4
  95. KEY_F5 = C.ALLEGRO_KEY_F5
  96. KEY_F6 = C.ALLEGRO_KEY_F6
  97. KEY_F7 = C.ALLEGRO_KEY_F7
  98. KEY_F8 = C.ALLEGRO_KEY_F8
  99. KEY_F9 = C.ALLEGRO_KEY_F9
  100. KEY_F10 = C.ALLEGRO_KEY_F10
  101. KEY_F11 = C.ALLEGRO_KEY_F11
  102. KEY_F12 = C.ALLEGRO_KEY_F12
  103. KEY_ESCAPE = C.ALLEGRO_KEY_ESCAPE
  104. KEY_TILDE = C.ALLEGRO_KEY_TILDE
  105. KEY_MINUS = C.ALLEGRO_KEY_MINUS
  106. KEY_EQUALS = C.ALLEGRO_KEY_EQUALS
  107. KEY_BACKSPACE = C.ALLEGRO_KEY_BACKSPACE
  108. KEY_TAB = C.ALLEGRO_KEY_TAB
  109. KEY_OPENBRACE = C.ALLEGRO_KEY_OPENBRACE
  110. KEY_CLOSEBRACE = C.ALLEGRO_KEY_CLOSEBRACE
  111. KEY_ENTER = C.ALLEGRO_KEY_ENTER
  112. KEY_SEMICOLON = C.ALLEGRO_KEY_SEMICOLON
  113. KEY_QUOTE = C.ALLEGRO_KEY_QUOTE
  114. KEY_BACKSLASH = C.ALLEGRO_KEY_BACKSLASH
  115. KEY_BACKSLASH2 = C.ALLEGRO_KEY_BACKSLASH2
  116. KEY_COMMA = C.ALLEGRO_KEY_COMMA
  117. KEY_FULLSTOP = C.ALLEGRO_KEY_FULLSTOP
  118. KEY_SLASH = C.ALLEGRO_KEY_SLASH
  119. KEY_SPACE = C.ALLEGRO_KEY_SPACE
  120. KEY_INSERT = C.ALLEGRO_KEY_INSERT
  121. KEY_DELETE = C.ALLEGRO_KEY_DELETE
  122. KEY_HOME = C.ALLEGRO_KEY_HOME
  123. KEY_END = C.ALLEGRO_KEY_END
  124. KEY_PGUP = C.ALLEGRO_KEY_PGUP
  125. KEY_PGDN = C.ALLEGRO_KEY_PGDN
  126. KEY_LEFT = C.ALLEGRO_KEY_LEFT
  127. KEY_RIGHT = C.ALLEGRO_KEY_RIGHT
  128. KEY_UP = C.ALLEGRO_KEY_UP
  129. KEY_DOWN = C.ALLEGRO_KEY_DOWN
  130. KEY_PAD_SLASH = C.ALLEGRO_KEY_PAD_SLASH
  131. KEY_PAD_ASTERISK = C.ALLEGRO_KEY_PAD_ASTERISK
  132. KEY_PAD_MINUS = C.ALLEGRO_KEY_PAD_MINUS
  133. KEY_PAD_PLUS = C.ALLEGRO_KEY_PAD_PLUS
  134. KEY_PAD_DELETE = C.ALLEGRO_KEY_PAD_DELETE
  135. KEY_PAD_ENTER = C.ALLEGRO_KEY_PAD_ENTER
  136. KEY_PRINTSCREEN = C.ALLEGRO_KEY_PRINTSCREEN
  137. KEY_PAUSE = C.ALLEGRO_KEY_PAUSE
  138. KEY_ABNT_C1 = C.ALLEGRO_KEY_ABNT_C1
  139. KEY_YEN = C.ALLEGRO_KEY_YEN
  140. KEY_KANA = C.ALLEGRO_KEY_KANA
  141. KEY_CONVERT = C.ALLEGRO_KEY_CONVERT
  142. KEY_NOCONVERT = C.ALLEGRO_KEY_NOCONVERT
  143. KEY_AT = C.ALLEGRO_KEY_AT
  144. KEY_CIRCUMFLEX = C.ALLEGRO_KEY_CIRCUMFLEX
  145. KEY_COLON2 = C.ALLEGRO_KEY_COLON2
  146. KEY_KANJI = C.ALLEGRO_KEY_KANJI
  147. KEY_PAD_EQUALS = C.ALLEGRO_KEY_PAD_EQUALS
  148. KEY_BACKQUOTE = C.ALLEGRO_KEY_BACKQUOTE
  149. KEY_SEMICOLON2 = C.ALLEGRO_KEY_SEMICOLON2
  150. KEY_COMMAND = C.ALLEGRO_KEY_COMMAND
  151. KEY_UNKNOWN = C.ALLEGRO_KEY_UNKNOWN
  152. /* All codes up to before KEY_MODIFIERS = C.ALLEGRO_KEY_MODIFIERS
  153. * assignedas additional unknown keys, like various multimedia
  154. * and application keys keyboards may have.
  155. */
  156. KEY_MODIFIERS = C.ALLEGRO_KEY_MODIFIERS
  157. KEY_LSHIFT = C.ALLEGRO_KEY_LSHIFT
  158. KEY_RSHIFT = C.ALLEGRO_KEY_RSHIFT
  159. KEY_LCTRL = C.ALLEGRO_KEY_LCTRL
  160. KEY_RCTRL = C.ALLEGRO_KEY_RCTRL
  161. KEY_ALT = C.ALLEGRO_KEY_ALT
  162. KEY_ALTGR = C.ALLEGRO_KEY_ALTGR
  163. KEY_LWIN = C.ALLEGRO_KEY_LWIN
  164. KEY_RWIN = C.ALLEGRO_KEY_RWIN
  165. KEY_MENU = C.ALLEGRO_KEY_MENU
  166. KEY_SCROLLLOCK = C.ALLEGRO_KEY_SCROLLLOCK
  167. KEY_NUMLOCK = C.ALLEGRO_KEY_NUMLOCK
  168. KEY_CAPSLOCK = C.ALLEGRO_KEY_CAPSLOCK
  169. KEY_MAX = C.ALLEGRO_KEY_MAX
  170. )
  171. // Keyboard modifier constants
  172. const (
  173. KEYMOD_SHIFT = C.ALLEGRO_KEYMOD_SHIFT
  174. KEYMOD_CTRL = C.ALLEGRO_KEYMOD_CTRL
  175. KEYMOD_ALT = C.ALLEGRO_KEYMOD_ALT
  176. KEYMOD_LWIN = C.ALLEGRO_KEYMOD_LWIN
  177. KEYMOD_RWIN = C.ALLEGRO_KEYMOD_RWIN
  178. KEYMOD_MENU = C.ALLEGRO_KEYMOD_MENU
  179. KEYMOD_ALTGR = C.ALLEGRO_KEYMOD_ALTGR
  180. KEYMOD_COMMAND = C.ALLEGRO_KEYMOD_COMMAND
  181. KEYMOD_SCROLLLOCK = C.ALLEGRO_KEYMOD_SCROLLLOCK
  182. KEYMOD_NUMLOCK = C.ALLEGRO_KEYMOD_NUMLOCK
  183. KEYMOD_CAPSLOCK = C.ALLEGRO_KEYMOD_CAPSLOCK
  184. KEYMOD_INALTSEQ = C.ALLEGRO_KEYMOD_INALTSEQ
  185. KEYMOD_ACCENT1 = C.ALLEGRO_KEYMOD_ACCENT1
  186. KEYMOD_ACCENT2 = C.ALLEGRO_KEYMOD_ACCENT2
  187. KEYMOD_ACCENT3 = C.ALLEGRO_KEYMOD_ACCENT3
  188. KEYMOD_ACCENT4 = C.ALLEGRO_KEYMOD_ACCENT4
  189. )
  190. type Keyboard struct {
  191. handle * C.ALLEGRO_KEYBOARD
  192. }
  193. func wrapKeyboardRaw(kb * C.ALLEGRO_KEYBOARD) * Keyboard {
  194. res := &Keyboard{kb}
  195. return res
  196. }
  197. type KeyboardState C.ALLEGRO_KEYBOARD_STATE
  198. func (ks * KeyboardState) toC() * C.ALLEGRO_KEYBOARD_STATE {
  199. return (*C.ALLEGRO_KEYBOARD_STATE)(ks)
  200. }
  201. func PollKeyboardState() * KeyboardState {
  202. ks := &KeyboardState{}
  203. C.al_get_keyboard_state(ks.toC());
  204. return ks
  205. }
  206. func (ks * KeyboardState) KeyDown(keycode int) bool {
  207. return bool(C.al_key_down(ks.toC(), C.int(keycode)))
  208. }