1 /**
2  * Keyboard
3  */
4 module d2d.sdl2.Keyboard;
5 
6 import std.algorithm;
7 import std.array;
8 import std.datetime;
9 import d2d.sdl2;
10 
11 ///A list of all of the key codes
12 immutable allKeyCodes = [
13     SDLK_UNKNOWN, SDLK_BACKSPACE, SDLK_TAB, SDLK_RETURN, SDLK_ESCAPE,
14     SDLK_SPACE, SDLK_EXCLAIM, SDLK_QUOTEDBL, SDLK_HASH,
15     SDLK_DOLLAR, SDLK_PERCENT, SDLK_AMPERSAND, SDLK_QUOTE, SDLK_LEFTPAREN,
16     SDLK_RIGHTPAREN, SDLK_ASTERISK, SDLK_PLUS, SDLK_COMMA,
17     SDLK_MINUS, SDLK_PERIOD, SDLK_SLASH, SDLK_0, SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6,
18     SDLK_7, SDLK_8, SDLK_9, SDLK_COLON, SDLK_SEMICOLON, SDLK_LESS, SDLK_EQUALS,
19     SDLK_GREATER, SDLK_QUESTION, SDLK_AT, SDLK_LEFTBRACKET,
20     SDLK_BACKSLASH, SDLK_RIGHTBRACKET, SDLK_CARET, SDLK_UNDERSCORE, SDLK_BACKQUOTE, SDLK_a, SDLK_b,
21     SDLK_c, SDLK_d, SDLK_e, SDLK_f, SDLK_g, SDLK_h, SDLK_i, SDLK_j, SDLK_k,
22     SDLK_l, SDLK_m, SDLK_n, SDLK_o, SDLK_p, SDLK_q, SDLK_r, SDLK_s, SDLK_t,
23     SDLK_u, SDLK_v, SDLK_w, SDLK_x, SDLK_y, SDLK_z, SDLK_DELETE, SDLK_CAPSLOCK,
24     SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11,
25     SDLK_F12, SDLK_PRINTSCREEN, SDLK_SCROLLLOCK, SDLK_PAUSE, SDLK_INSERT,
26     SDLK_HOME, SDLK_PAGEUP, SDLK_END, SDLK_PAGEDOWN, SDLK_RIGHT,
27     SDLK_LEFT, SDLK_DOWN, SDLK_UP, SDLK_NUMLOCKCLEAR, SDLK_KP_DIVIDE,
28     SDLK_KP_MULTIPLY, SDLK_KP_MINUS, SDLK_KP_PLUS, SDLK_KP_ENTER,
29     SDLK_KP_1, SDLK_KP_2, SDLK_KP_3, SDLK_KP_4, SDLK_KP_5, SDLK_KP_6,
30     SDLK_KP_7, SDLK_KP_8, SDLK_KP_9, SDLK_KP_0, SDLK_KP_PERIOD,
31     SDLK_APPLICATION, SDLK_POWER, SDLK_KP_EQUALS, SDLK_F13, SDLK_F14, SDLK_F15, SDLK_F16, SDLK_F17, SDLK_F18, SDLK_F19,
32     SDLK_F20, SDLK_F21, SDLK_F22, SDLK_F23, SDLK_F24, SDLK_EXECUTE, SDLK_HELP, SDLK_MENU, SDLK_SELECT, SDLK_STOP,
33     SDLK_AGAIN, SDLK_UNDO, SDLK_CUT, SDLK_COPY, SDLK_PASTE, SDLK_FIND,
34     SDLK_MUTE, SDLK_VOLUMEUP, SDLK_VOLUMEDOWN, SDLK_KP_COMMA,
35     SDLK_KP_EQUALSAS400, SDLK_ALTERASE, SDLK_SYSREQ, SDLK_CANCEL, SDLK_CLEAR,
36     SDLK_PRIOR, SDLK_RETURN2, SDLK_SEPARATOR, SDLK_OUT, SDLK_OPER,
37     SDLK_CLEARAGAIN, SDLK_CRSEL, SDLK_EXSEL, SDLK_KP_00, SDLK_KP_000, SDLK_THOUSANDSSEPARATOR,
38     SDLK_DECIMALSEPARATOR, SDLK_CURRENCYUNIT, SDLK_CURRENCYSUBUNIT,
39     SDLK_KP_LEFTPAREN, SDLK_KP_RIGHTPAREN, SDLK_KP_LEFTBRACE,
40     SDLK_KP_RIGHTBRACE, SDLK_KP_TAB, SDLK_KP_BACKSPACE, SDLK_KP_A, SDLK_KP_B,
41     SDLK_KP_C, SDLK_KP_D, SDLK_KP_E, SDLK_KP_F, SDLK_KP_XOR, SDLK_KP_POWER,
42     SDLK_KP_PERCENT, SDLK_KP_LESS, SDLK_KP_GREATER, SDLK_KP_AMPERSAND, SDLK_KP_DBLAMPERSAND,
43     SDLK_KP_VERTICALBAR, SDLK_KP_DBLVERTICALBAR, SDLK_KP_COLON, SDLK_KP_HASH,
44     SDLK_KP_SPACE, SDLK_KP_AT, SDLK_KP_EXCLAM, SDLK_KP_MEMSTORE,
45     SDLK_KP_MEMRECALL, SDLK_KP_MEMCLEAR, SDLK_KP_MEMADD, SDLK_KP_MEMSUBTRACT,
46     SDLK_KP_MEMMULTIPLY, SDLK_KP_MEMDIVIDE, SDLK_KP_PLUSMINUS, SDLK_KP_CLEAR,
47     SDLK_KP_CLEARENTRY, SDLK_KP_BINARY, SDLK_KP_OCTAL, SDLK_KP_DECIMAL,
48     SDLK_KP_HEXADECIMAL, SDLK_LCTRL, SDLK_LSHIFT, SDLK_LALT, SDLK_LGUI,
49     SDLK_RCTRL, SDLK_RSHIFT, SDLK_RALT, SDLK_RGUI, SDLK_MODE,
50     SDLK_AUDIONEXT, SDLK_AUDIOPREV, SDLK_AUDIOSTOP, SDLK_AUDIOPLAY,
51     SDLK_AUDIOMUTE, SDLK_MEDIASELECT, SDLK_WWW, SDLK_MAIL, SDLK_CALCULATOR,
52     SDLK_COMPUTER, SDLK_AC_SEARCH, SDLK_AC_HOME, SDLK_AC_BACK,
53     SDLK_AC_FORWARD, SDLK_AC_STOP, SDLK_AC_REFRESH, SDLK_AC_BOOKMARKS,
54     SDLK_BRIGHTNESSDOWN, SDLK_BRIGHTNESSUP, SDLK_DISPLAYSWITCH,
55     SDLK_KBDILLUMTOGGLE, SDLK_KBDILLUMDOWN, SDLK_KBDILLUMUP, SDLK_EJECT, SDLK_SLEEP
56 ];
57 
58 /**
59  * The keyboard input source which accumulates keyboard information
60  */
61 class Keyboard : InputSource!SDL_Keycode, EventHandler {
62 
63     private Pressable!SDL_Keycode[SDL_Keycode] _allKeys; ///All of the pressable structs visible
64 
65     alias allKeys = allPressables; ///Allows allPressables to be called as allKeys
66 
67     /**
68      * Returns all of the keys on the keyboard
69      */
70     override @property Pressable!SDL_Keycode[SDL_Keycode] allPressables() {
71         return this._allKeys.dup;
72     }
73 
74     /**
75      * Initializes all keys of a keyboard
76      */
77     this() {
78         allKeyCodes.each!(keycode => this._allKeys[keycode] = new Pressable!SDL_Keycode(keycode));
79     }
80 
81     /**
82      * Accumulates all of the keyboard events and updates stored pressables accordingly
83      */
84     override void handleEvent(SDL_Event event) {
85         if (event.type == SDL_KEYDOWN) {
86             this._allKeys[event.key.keysym.sym].lastPressed = Clock.currTime();
87         }
88         else if (event.type == SDL_KEYUP) {
89             this._allKeys[event.key.keysym.sym].lastReleased = Clock.currTime();
90         }
91     }
92 
93 }