From ff60dcb2365e53c6ba784bd465539fb9bddf966b Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Thu, 15 Feb 2018 21:06:04 +0100 Subject: [PATCH] Added Key and KeyMod consts. Refactored events to use Key and KeyMod where applicable. Added IsKeyDown method. --- allegro5/event.go | 10 +-- allegro5/keyboard.go | 177 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 allegro5/keyboard.go diff --git a/allegro5/event.go b/allegro5/event.go index 013c9e8..ff5ed55 100644 --- a/allegro5/event.go +++ b/allegro5/event.go @@ -72,14 +72,14 @@ type DisplayOrientationEvent struct { type KeyEvent struct { EventBase - KeyCode int + KeyCode Key Display *Display } type KeyCharEvent struct { KeyEvent UnicodeCharacter rune - Modifiers uint + Modifiers KeyMod Repeat bool } @@ -165,13 +165,13 @@ func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event { return &MouseButtonUpEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, uint(mouse.button), float32(mouse.pressure)} case C.ALLEGRO_EVENT_KEY_DOWN: key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e)) - return &KeyDownEvent{KeyEvent{eb, int(key.keycode), nil}} + return &KeyDownEvent{KeyEvent{eb, Key(key.keycode), nil}} case C.ALLEGRO_EVENT_KEY_UP: key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e)) - return &KeyUpEvent{KeyEvent{eb, int(key.keycode), nil}} + return &KeyUpEvent{KeyEvent{eb, Key(key.keycode), nil}} case C.ALLEGRO_EVENT_KEY_CHAR: key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e)) - return &KeyCharEvent{KeyEvent{eb, int(key.keycode), nil}, rune(key.unichar), uint(key.modifiers), bool(key.repeat)} + return &KeyCharEvent{KeyEvent{eb, Key(key.keycode), nil}, rune(key.unichar), KeyMod(key.modifiers), bool(key.repeat)} } return nil } diff --git a/allegro5/keyboard.go b/allegro5/keyboard.go new file mode 100644 index 0000000..9faa571 --- /dev/null +++ b/allegro5/keyboard.go @@ -0,0 +1,177 @@ +package allegro5 + +// #include +import "C" + +type Key int + +const ( + KeyA Key = 1 + KeyB = 2 + KeyC = 3 + KeyD = 4 + KeyE = 5 + KeyF = 6 + KeyG = 7 + KeyH = 8 + KeyI = 9 + KeyJ = 10 + KeyK = 11 + KeyL = 12 + KeyM = 13 + KeyN = 14 + KeyO = 15 + KeyP = 16 + KeyQ = 17 + KeyR = 18 + KeyS = 19 + KeyT = 20 + KeyU = 21 + KeyV = 22 + KeyW = 23 + KeyX = 24 + KeyY = 25 + KeyZ = 26 + Key0 = 27 + Key1 = 28 + Key2 = 29 + Key3 = 30 + Key4 = 31 + Key5 = 32 + Key6 = 33 + Key7 = 34 + Key8 = 35 + Key9 = 36 + KeyPad0 = 37 + KeyPad1 = 38 + KeyPad2 = 39 + KeyPad3 = 40 + KeyPad4 = 41 + KeyPad5 = 42 + KeyPad6 = 43 + KeyPad7 = 44 + KeyPad8 = 45 + KeyPad9 = 46 + KeyF1 = 47 + KeyF2 = 48 + KeyF3 = 49 + KeyF4 = 50 + KeyF5 = 51 + KeyF6 = 52 + KeyF7 = 53 + KeyF8 = 54 + KeyF9 = 55 + KeyF10 = 56 + KeyF11 = 57 + KeyF12 = 58 + KeyEscape = 59 + KeyTilde = 60 + KeyMinus = 61 + KeyEquals = 62 + KeyBackspace = 63 + KeyTab = 64 + KeyOpenBrace = 65 + KeyCloseBrace = 66 + KeyEnter = 67 + KeySemicolon = 68 + KeyQuote = 69 + KeyBackslash = 70 + KeyBackslash2 = 71 /* DirectInput calls this DIK_OEM_102: "< > | on UK/Germany keyboards" */ + KeyComma = 72 + KeyFullstop = 73 + KeySlash = 74 + KeySpace = 75 + KeyInsert = 76 + KeyDelete = 77 + KeyHome = 78 + KeyEnd = 79 + KeyPgup = 80 + KeyPgdn = 81 + KeyLeft = 82 + KeyRight = 83 + KeyUp = 84 + KeyDown = 85 + KeyPadSlash = 86 + KeyPadAsterisk = 87 + KeyPadMinus = 88 + KeyPadPlus = 89 + KeyPadDelete = 90 + KeyPadEnter = 91 + KeyPrintScreen = 92 + KeyPause = 93 + KeyAbntC1 = 94 + KeyYen = 95 + KeyKana = 96 + KeyConvert = 97 + KeyNoConvert = 98 + KeyAt = 99 + KeyCircumflex = 100 + KeyColon2 = 101 + KeyKanji = 102 + KeyPadEquals = 103 /* MacOS X */ + KeyBackQuote = 104 /* MacOS X */ + KeySemicolon2 = 105 /* MacOS X -- TODO: ask lillo what this should be */ + KeyCommand = 106 /* MacOS X */ + KeyBack = 107 /* Android back key */ + KeyVolumeUp = 108 + KeyVolumeDown = 109 + KeySearch = 110 + KeyDPadCenter = 111 + KeyButtonX = 112 + KeyButtonY = 113 + KeyDPadUp = 114 + KeyDPadDown = 115 + KeyDpadLeft = 116 + KeyDpadRight = 117 + KeySelect = 118 + KeyStart = 119 + KeyButtonL1 = 120 + KeyButtonR1 = 121 + KeyButtonL2 = 122 + KeyButtonR2 = 123 + KeyButtonA = 124 + KeyButtonB = 125 + KeyThumbL = 126 + KeyThumbR = 127 + KeyUnknown = 128 + KeyModifiers = 215 + KeyLShift = 215 + KeyRShift = 216 + KeyLCtrl = 217 + KeyRCtrl = 218 + KeyAlt = 219 + KeyAltGr = 220 + KeyLWin = 221 + KeyRWin = 222 + KeyMenu = 223 + KeyScrollLock = 224 + KeyNumLock = 225 + KeyCapsLock = 226 +) + +type KeyMod uint + +const ( + KeyModShift KeyMod = 0x00001 + KeyModCtrl = 0x00002 + KeyModAlt = 0x00004 + KeyModLWin = 0x00008 + KeyModRWin = 0x00010 + KeyModMenu = 0x00020 + KeyModAltGr = 0x00040 + KeyModCommand = 0x00080 + KeyModScrollLock = 0x00100 + KeyModNumlock = 0x00200 + KeyModCapsLock = 0x00400 + KeyModInaltseq = 0x00800 + KeyModAccent1 = 0x01000 + KeyModAccent2 = 0x02000 + KeyModAccent3 = 0x04000 + KeyModAccent4 = 0x08000 +) + +func IsKeyDown(k Key) bool { + var state C.ALLEGRO_KEYBOARD_STATE + C.al_get_keyboard_state(&state) + return bool(C.al_key_down(&state, C.int(k))) +}