1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  * 
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  * 
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  * 
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  * 
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.System.Keyboard;
25 
26 private:
27 
28 import derelict.sdl2.types;
29 import derelict.sdl2.functions;
30 
31 public:
32 
33 /**
34  * Represent the Keyboard
35  *
36  * Author: Randy Schuett (rswhite4@googlemail.com)
37  */
38 interface Keyboard {
39     /**
40      * Returns if the given Keyboard.Code is pressed.
41      * 
42      * Examples:
43      * ---
44      * if (Keyboard.isPressed(Keyboard.Code.Escape))
45      *     writeln("escape is pressed.");
46      * ---
47      */
48     @nogc
49     static bool isPressed(Code code) nothrow {
50         immutable int scancode = SDL_GetScancodeFromKey(code);
51         const ubyte* keys = SDL_GetKeyboardState(null);
52 
53         return keys[scancode] == 1;
54     }
55     
56     /**
57      * Returns the current Keyboard modifier.
58      *
59      * See: Mod enum
60      */
61     @nogc
62     static Mod getModifier() nothrow {
63         return cast(Mod) SDL_GetModState();
64     }
65     
66     /**
67      * Set the current Keyboard modifier.
68      *
69      * See: Mod enum
70      */
71     @nogc
72     static void setModifier(Mod mod) nothrow {
73         SDL_SetModState(mod);
74     }
75     
76     /**
77      * Returns if screen keyboard is supported.
78      */
79     @nogc
80     static bool hasScreenSupport() nothrow {
81         return SDL_HasScreenKeyboardSupport() == SDL_TRUE;
82     }
83     
84     /**
85      * All supported Keyboard modifiers.
86      */
87     enum Mod {
88         None    = KMOD_NONE,    /// 0 (no modifier is applicable)
89         LShift  = KMOD_LSHIFT,  /// the left Shift key is down
90         RShift  = KMOD_RSHIFT,  /// the right Shift key is down
91         LCtrl   = KMOD_LCTRL,   /// the left Ctrl (Control) key is down
92         RCtrl   = KMOD_RCTRL,   /// the right Ctrl (Control) key is down
93         LAlt    = KMOD_LALT,    /// the left Alt key is down
94         RAlt    = KMOD_RALT,    /// the right Alt key is down
95         LGui    = KMOD_LGUI,    /// the left GUI key (often the Windows key) is down
96         RGui    = KMOD_RGUI,    /// the right GUI key (often the Windows key) is down
97         Num     = KMOD_NUM,     /// the Num Lock key (may be located on an extended keypad) is down
98         Caps    = KMOD_CAPS,    /// the Caps Lock key is down
99         Mode    = KMOD_MODE,    /// the AltGr key is down
100         
101         Ctrl    = KMOD_CTRL,    /// (Mod.LCtrl|Mod.RCtrl)
102         Shift   = KMOD_SHIFT,   /// (Mod.LShift|Mod.RShift)
103         Alt     = KMOD_ALT,     /// (Mod.LAlt|Mod.RAlt)
104         Gui     = KMOD_GUI,     /// (Mod.LGui|Mod.RGui)
105     }
106 
107     /**
108      * An alias
109      */
110     deprecated("Use 'Key' instead")
111     alias Code = Key;
112     
113     /**
114      * Supported Keyboard Codes.
115      * These are all possible/supported keys.
116      */
117     enum Key {
118         Unknown = SDLK_UNKNOWN, /// 
119         
120         Return = SDLK_RETURN, /// 
121         Escape = SDLK_ESCAPE, /// 
122         Backspace = SDLK_BACKSPACE, /// 
123         Tab = SDLK_TAB, /// 
124         Space = SDLK_SPACE, /// 
125         Exclaim = SDLK_EXCLAIM, /// 
126         Quotedbl = SDLK_QUOTEDBL, /// 
127         Hash = SDLK_HASH, /// 
128         Percent = SDLK_PERCENT, /// 
129         Dollar = SDLK_DOLLAR, /// 
130         Ampersand = SDLK_AMPERSAND, /// 
131         Quote = SDLK_QUOTE, /// 
132         Leftparen = SDLK_LEFTPAREN, /// 
133         Rightparen = SDLK_RIGHTPAREN, /// 
134         Asterisk = SDLK_ASTERISK, /// 
135         Plus = SDLK_PLUS, /// 
136         Comma = SDLK_COMMA, /// 
137         Minus = SDLK_MINUS, /// 
138         Period = SDLK_PERIOD, /// 
139         Slash = SDLK_SLASH, /// 
140         
141         Dot = Period, /** Shortcut */
142         Esc = Escape, /** Shortcut */
143         
144         Num0 = SDLK_0, /// 
145         Num1 = SDLK_1, /// 
146         Num2 = SDLK_2, /// 
147         Num3 = SDLK_3, /// 
148         Num4 = SDLK_4, /// 
149         Num5 = SDLK_5, /// 
150         Num6 = SDLK_6, /// 
151         Num7 = SDLK_7, /// 
152         Num8 = SDLK_8, /// 
153         Num9 = SDLK_9, /// 
154         
155         Colon = SDLK_COLON, /// 
156         Semicolon = SDLK_SEMICOLON, /// 
157         Less = SDLK_LESS, /// 
158         Equals = SDLK_EQUALS, /// 
159         Greater = SDLK_GREATER, /// 
160         Question = SDLK_QUESTION, /// 
161         At = SDLK_AT, /// 
162         
163         Leftbracket = SDLK_LEFTBRACKET, /// 
164         Backslash = SDLK_BACKSLASH, /// 
165         Rightbracket = SDLK_RIGHTBRACKET, /// 
166         Caret = SDLK_CARET, /// 
167         Underscore = SDLK_UNDERSCORE, /// 
168         Backquote = SDLK_BACKQUOTE, /// 
169         
170         A = SDLK_a, /// 
171         B = SDLK_b, /// 
172         C = SDLK_c, /// 
173         D = SDLK_d, /// 
174         E = SDLK_e, /// 
175         F = SDLK_f, /// 
176         G = SDLK_g, /// 
177         H = SDLK_h, /// 
178         I = SDLK_i, /// 
179         J = SDLK_j, /// 
180         K = SDLK_k, /// 
181         L = SDLK_l, /// 
182         M = SDLK_m, /// 
183         N = SDLK_n, /// 
184         O = SDLK_o, /// 
185         P = SDLK_p, /// 
186         Q = SDLK_q, /// 
187         R = SDLK_r, /// 
188         S = SDLK_s, /// 
189         T = SDLK_t, /// 
190         U = SDLK_u, /// 
191         V = SDLK_v, /// 
192         W = SDLK_w, /// 
193         X = SDLK_x, /// 
194         Y = SDLK_y, /// 
195         Z = SDLK_z, /// 
196         
197         Capslock = SDLK_CAPSLOCK, /// 
198         
199         F1 = SDLK_F1, /// 
200         F2 = SDLK_F2, /// 
201         F3 = SDLK_F3, /// 
202         F4 = SDLK_F4, /// 
203         F5 = SDLK_F5, /// 
204         F6 = SDLK_F6, /// 
205         F7 = SDLK_F7, /// 
206         F8 = SDLK_F8, /// 
207         F9 = SDLK_F9, /// 
208         F10 = SDLK_F10, /// 
209         F11 = SDLK_F11, /// 
210         F12 = SDLK_F12, /// 
211         
212         Printscreen = SDLK_PRINTSCREEN, /// 
213         Scrolllock = SDLK_SCROLLLOCK, /// 
214         Pause = SDLK_PAUSE, /// 
215         Insert = SDLK_INSERT, /// 
216         Home = SDLK_HOME, /// 
217         PageUp = SDLK_PAGEUP, /// 
218         Delete = SDLK_DELETE, /// 
219         End = SDLK_END, /// 
220         PageDown = SDLK_PAGEDOWN, /// 
221         Right = SDLK_RIGHT, /// 
222         Left = SDLK_LEFT, /// 
223         Down = SDLK_DOWN, /// 
224         Up = SDLK_UP, /// 
225         
226         NumLockClear = SDLK_NUMLOCKCLEAR, /// 
227         KP_Divide = SDLK_KP_DIVIDE, /// 
228         KP_Multiply = SDLK_KP_MULTIPLY, /// 
229         KP_Minus = SDLK_KP_MINUS, /// 
230         KP_Plus = SDLK_KP_PLUS, /// 
231         KP_Enter = SDLK_KP_ENTER, /// 
232         KP_1 = SDLK_KP_1, /// 
233         KP_2 = SDLK_KP_2, /// 
234         KP_3 = SDLK_KP_3, /// 
235         KP_4 = SDLK_KP_4, /// 
236         KP_5 = SDLK_KP_5, /// 
237         KP_6 = SDLK_KP_6, /// 
238         KP_7 = SDLK_KP_7, /// 
239         KP_8 = SDLK_KP_8, /// 
240         KP_9 = SDLK_KP_9, /// 
241         KP_0 = SDLK_KP_0, /// 
242         
243         F13 = SDLK_F13, /// 
244         F14 = SDLK_F14, /// 
245         F15 = SDLK_F15, /// 
246         F16 = SDLK_F16, /// 
247         F17 = SDLK_F17, /// 
248         F18 = SDLK_F18, /// 
249         F19 = SDLK_F19, /// 
250         F20 = SDLK_F20, /// 
251         F21 = SDLK_F21, /// 
252         F22 = SDLK_F22, /// 
253         F23 = SDLK_F23, /// 
254         F24 = SDLK_F24, /// 
255         
256         LCtrl = SDLK_LCTRL, /// 
257         LShift = SDLK_LSHIFT, /// 
258         LAlt = SDLK_LALT, /// 
259         LGui = SDLK_LGUI, /// 
260         RCtrl = SDLK_RCTRL, /// 
261         RShift = SDLK_RSHIFT, /// 
262         RAlt = SDLK_RALT, /// 
263         RGui = SDLK_RGUI /// 
264     }
265 }