CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUKeyboard.h
1 //
2 // CUKeyboard.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class provides basic keyboard support. It is intended for low-level,
6 // WASD-like control. It is not to be used for gather text. That is the
7 // purpose of the TextInput device.
8 //
9 // This class is a singleton and should never be allocated directly. It
10 // should only be accessed via the Input dispatcher.
11 //
12 // CUGL MIT License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 7/9/16
33 
34 #ifndef __CU_KEYBOARD_H__
35 #define __CU_KEYBOARD_H__
36 
37 #include <cugl/input/CUInput.h>
38 #include <cugl/util/CUTimestamp.h>
39 #include <functional>
40 
41 namespace cugl {
42 
43 #pragma mark KeyCode
44 
55 enum class KeyCode : int {
57  NUM_0 = SDLK_0,
59  NUM_1 = SDLK_1,
61  NUM_2 = SDLK_2,
63  NUM_3 = SDLK_3,
65  NUM_4 = SDLK_4,
67  NUM_5 = SDLK_5,
69  NUM_6 = SDLK_6,
71  NUM_7 = SDLK_7,
73  NUM_8 = SDLK_8,
75  NUM_9 = SDLK_9,
76 
78  A = SDLK_a,
80  B = SDLK_b,
82  C = SDLK_c,
84  D = SDLK_d,
86  E = SDLK_e,
88  F = SDLK_f,
90  G = SDLK_g,
92  H = SDLK_h,
94  I = SDLK_i,
96  J = SDLK_j,
98  K = SDLK_k,
100  L = SDLK_l,
102  M = SDLK_m,
104  N = SDLK_n,
106  O = SDLK_o,
108  P = SDLK_p,
110  Q = SDLK_q,
112  R = SDLK_r,
114  S = SDLK_s,
116  T = SDLK_t,
118  U = SDLK_u,
120  V = SDLK_v,
122  W = SDLK_w,
124  X = SDLK_x,
126  Y = SDLK_y,
128  Z = SDLK_z,
129 
131  ARROW_DOWN = SDLK_DOWN,
133  ARROW_LEFT = SDLK_LEFT,
135  ARROW_RIGHT = SDLK_RIGHT,
137  ARROW_UP = SDLK_UP,
138 
140  QUOTE = SDLK_QUOTE,
142  BACKSLASH = SDLK_BACKSLASH,
144  COMMA = SDLK_COMMA,
146  EQUALS = SDLK_EQUALS,
148  BACKQUOTE = SDLK_BACKQUOTE,
150  LEFT_BRACKET = SDLK_LEFTBRACKET,
152  MINUS = SDLK_MINUS,
154  PERIOD = SDLK_PERIOD,
156  RIGHT_BRACKET = SDLK_RIGHTBRACKET,
158  SEMICOLON = SDLK_SEMICOLON,
160  SLASH = SDLK_SLASH,
162  BACKSPACE = SDLK_BACKSPACE,
164  SPACE = SDLK_SPACE,
166  TAB = SDLK_TAB,
167 
169  DEL = SDLK_DELETE,
171  END = SDLK_END,
173  ESCAPE = SDLK_ESCAPE,
175  HOME = SDLK_HOME,
177  HELP = SDLK_HELP,
179  PAGE_DOWN = SDLK_PAGEDOWN,
181  PAGE_UP = SDLK_PAGEUP,
183  PAUSE = SDLK_PAUSE,
185  RETURN = SDLK_RETURN,
187  ENTER = SDLK_RETURN2,
188 
190  CAPS_LOCK = SDLK_CAPSLOCK,
192  LEFT_ALT = SDLK_LALT,
194  LEFT_CTRL = SDLK_LCTRL,
196  LEFT_SHIFT = SDLK_LSHIFT,
198  LEFT_META = SDLK_LGUI,
200  RIGHT_ALT = SDLK_RALT,
202  RIGHT_CTRL = SDLK_RCTRL,
204  RIGHT_SHIFT = SDLK_RSHIFT,
206  RIGHT_META = SDLK_RGUI,
208  NUMLOCK = SDLK_NUMLOCKCLEAR,
209 
211  KEYPAD_0 = SDLK_KP_0,
213  KEYPAD_1 = SDLK_KP_1,
215  KEYPAD_2 = SDLK_KP_2,
217  KEYPAD_3 = SDLK_KP_3,
219  KEYPAD_4 = SDLK_KP_4,
221  KEYPAD_5 = SDLK_KP_5,
223  KEYPAD_6 = SDLK_KP_6,
225  KEYPAD_7 = SDLK_KP_7,
227  KEYPAD_8 = SDLK_KP_8,
229  KEYPAD_9 = SDLK_KP_9,
231  KEYPAD_CLEAR = SDLK_KP_CLEAR,
233  KEYPAD_EQUALS = SDLK_KP_EQUALS,
235  KEYPAD_DIVIDE = SDLK_KP_DIVIDE,
237  KEYPAD_MULTIPLY = SDLK_KP_MULTIPLY,
239  KEYPAD_MINUS = SDLK_KP_MINUS,
241  KEYPAD_PLUS = SDLK_KP_PLUS,
243  KEYPAD_ENTER = SDLK_KP_ENTER,
244 
246  UNKNOWN = SDLK_POWER
247 };
248 
253  std::size_t operator()(const KeyCode& k) const {
254  return (std::hash<int>()(static_cast<int>(k)));
255  }
256 };
257 
258 
259 #pragma mark -
260 
265 enum class KeyCategory {
267  NUMBER,
269  LETTER,
271  ARROW,
273  PUNCTUATION,
275  SPECIAL,
277  MODIFIER,
279  KEYPAD,
281  UNKNOWN
282 };
283 
284 #pragma mark -
285 
289 class KeyEvent {
290 public:
294  KeyCode keycode;
295 
299  KeyEvent() : keycode(KeyCode::UNKNOWN) {}
300 
307  KeyEvent(KeyCode code, const Timestamp& stamp) {
308  keycode = code; timestamp = stamp;
309  }
310 
318  KeyCategory keyCategory();
319 };
320 
321 #pragma mark -
322 
341 class Keyboard : public InputDevice {
342 public:
343 #pragma mark Listener
344 
369  typedef std::function<void(const KeyEvent& event, bool focus)> Listener;
370 
371 #pragma mark Values
372 protected:
374  std::unordered_set<KeyCode,KeyCodeHasher> _previous;
376  std::unordered_set<KeyCode,KeyCodeHasher> _current;
377 
379  std::unordered_map<Uint32, Listener> _downListeners;
381  std::unordered_map<Uint32, Listener> _upListeners;
382 
383 #pragma mark Constructor
384 
391 
395  virtual ~Keyboard() {}
396 
402  virtual void dispose() override;
403 
404 public:
405 #pragma mark Data Polling
406 
413  bool keyDown(KeyCode code) const {
414  return _current.find(code) != _current.end();
415  }
416 
427  bool keyPressed(KeyCode code) const {
428  return _current.find(code) != _current.end() && _previous.find(code) == _previous.end();
429  }
430 
441  bool keyReleased(KeyCode code) {
442  return _current.find(code) == _current.end() && _previous.find(code) != _previous.end();
443  }
444 
450  unsigned int keyCount() const { return (unsigned int)_current.size(); }
451 
460  const std::vector<KeyCode> keySet() const;
461 
471  static KeyCategory keyCategory(KeyCode code);
472 
473 #pragma mark Listeners
474 
484  virtual bool requestFocus(Uint32 key) override;
485 
496  bool isListener(Uint32 key) const;
497 
509  const Listener getKeyDownListener(Uint32 key) const;
510 
522  const Listener getKeyUpListener(Uint32 key) const;
523 
538  bool addKeyDownListener(Uint32 key, Listener listener);
539 
554  bool addKeyUpListener(Uint32 key, Listener listener);
555 
568  bool removeKeyDownListener(Uint32 key);
569 
582  bool removeKeyUpListener(Uint32 key);
583 
584 protected:
585 #pragma mark Input Device
586 
592  virtual void clearState() override;
593 
605  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
606 
617  virtual void queryEvents(std::vector<Uint32>& eventset) override;
618 
619  // Apparently friends are not inherited
620  friend class Input;
621 };
622 
623 }
624 
625 #endif /* __CU_KEYBOARD_H__ */
bool removeKeyDownListener(Uint32 key)
bool addKeyDownListener(Uint32 key, Listener listener)
Keyboard()
Definition: CUKeyboard.h:390
Definition: CUInput.h:77
bool keyDown(KeyCode code) const
Definition: CUKeyboard.h:413
static KeyCategory keyCategory(KeyCode code)
Definition: CUTimestamp.h:61
std::function< void(const KeyEvent &event, bool focus)> Listener
Definition: CUKeyboard.h:369
bool addKeyUpListener(Uint32 key, Listener listener)
bool isListener(Uint32 key) const
unsigned int keyCount() const
Definition: CUKeyboard.h:450
const Listener getKeyDownListener(Uint32 key) const
Definition: CUKeyboard.h:252
bool keyPressed(KeyCode code) const
Definition: CUKeyboard.h:427
KeyEvent()
Definition: CUKeyboard.h:299
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
Timestamp timestamp
Definition: CUKeyboard.h:292
std::unordered_map< Uint32, Listener > _downListeners
Definition: CUKeyboard.h:379
std::unordered_set< KeyCode, KeyCodeHasher > _current
Definition: CUKeyboard.h:376
const Listener getKeyUpListener(Uint32 key) const
KeyCode keycode
Definition: CUKeyboard.h:294
std::unordered_set< KeyCode, KeyCodeHasher > _previous
Definition: CUKeyboard.h:374
std::unordered_map< Uint32, Listener > _upListeners
Definition: CUKeyboard.h:381
Definition: CUKeyboard.h:289
bool removeKeyUpListener(Uint32 key)
KeyCategory keyCategory()
virtual void queryEvents(std::vector< Uint32 > &eventset) override
const std::vector< KeyCode > keySet() const
virtual ~Keyboard()
Definition: CUKeyboard.h:395
Definition: CUAction.h:51
Definition: CUKeyboard.h:341
virtual bool requestFocus(Uint32 key) override
KeyEvent(KeyCode code, const Timestamp &stamp)
Definition: CUKeyboard.h:307
virtual void dispose() override
Definition: CUInput.h:298
bool keyReleased(KeyCode code)
Definition: CUKeyboard.h:441
virtual void clearState() override