CUGL
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 zlib 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:
295 
299  KeyEvent() : keycode(KeyCode::UNKNOWN) {}
300 
307  KeyEvent(KeyCode code, const Timestamp& stamp) {
308  keycode = code; timestamp = stamp;
309  }
310 
319 };
320 
321 #pragma mark -
322 #pragma mark KeyListener
323 
349 typedef std::function<void(const KeyEvent& event, bool focus)> KeyListener;
350 
351 #pragma mark -
352 
371 class Keyboard : public InputDevice {
372 #pragma mark Values
373 protected:
375  std::unordered_set<KeyCode,KeyCodeHasher> _previous;
377  std::unordered_set<KeyCode,KeyCodeHasher> _current;
378 
380  std::unordered_map<Uint32, KeyListener> _downListeners;
382  std::unordered_map<Uint32, KeyListener> _upListeners;
383 
384 #pragma mark Constructor
385 
392 
396  virtual ~Keyboard() {}
397 
403  virtual void dispose() override;
404 
405 public:
406 #pragma mark Data Polling
407 
414  bool keyDown(KeyCode code) const {
415  return _current.find(code) != _current.end();
416  }
417 
428  bool keyPressed(KeyCode code) const {
429  return _current.find(code) != _current.end() && _previous.find(code) == _previous.end();
430  }
431 
442  bool keyReleased(KeyCode code) {
443  return _current.find(code) == _current.end() && _previous.find(code) != _previous.end();
444  }
445 
451  unsigned int keyCount() const { return (unsigned int)_current.size(); }
452 
461  const std::vector<KeyCode> keySet() const;
462 
472  static KeyCategory keyCategory(KeyCode code);
473 
474 #pragma mark Listeners
475 
485  virtual bool requestFocus(Uint32 key) override;
486 
497  bool isListener(Uint32 key) const;
498 
510  const KeyListener getKeyDownListener(Uint32 key) const;
511 
523  const KeyListener getKeyUpListener(Uint32 key) const;
524 
539  bool addKeyDownListener(Uint32 key, KeyListener listener);
540 
555  bool addKeyUpListener(Uint32 key, KeyListener listener);
556 
569  bool removeKeyDownListener(Uint32 key);
570 
583  bool removeKeyUpListener(Uint32 key);
584 
585 protected:
586 #pragma mark Input Device
587 
593  virtual void clearState() override;
594 
606  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
607 
618  virtual void queryEvents(std::vector<Uint32>& eventset) override;
619 
620  // Apparently friends are not inherited
621  friend class Input;
622 };
623 
624 }
625 
626 #endif /* __CU_KEYBOARD_H__ */
KeyCategory
Definition: CUKeyboard.h:265
bool removeKeyDownListener(Uint32 key)
const KeyListener getKeyUpListener(Uint32 key) const
Keyboard()
Definition: CUKeyboard.h:391
Definition: CUInput.h:77
bool keyDown(KeyCode code) const
Definition: CUKeyboard.h:414
static KeyCategory keyCategory(KeyCode code)
Definition: CUTimestamp.h:61
bool isListener(Uint32 key) const
unsigned int keyCount() const
Definition: CUKeyboard.h:451
Definition: CUKeyboard.h:252
bool keyPressed(KeyCode code) const
Definition: CUKeyboard.h:428
KeyEvent()
Definition: CUKeyboard.h:299
bool addKeyDownListener(Uint32 key, KeyListener listener)
std::function< void(const KeyEvent &event, bool focus)> KeyListener
Definition: CUKeyboard.h:349
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
Timestamp timestamp
Definition: CUKeyboard.h:292
std::unordered_set< KeyCode, KeyCodeHasher > _current
Definition: CUKeyboard.h:377
KeyCode keycode
Definition: CUKeyboard.h:294
KeyCode
Definition: CUKeyboard.h:55
std::unordered_set< KeyCode, KeyCodeHasher > _previous
Definition: CUKeyboard.h:375
const KeyListener getKeyDownListener(Uint32 key) const
Definition: CUKeyboard.h:289
std::unordered_map< Uint32, KeyListener > _downListeners
Definition: CUKeyboard.h:380
std::unordered_map< Uint32, KeyListener > _upListeners
Definition: CUKeyboard.h:382
bool removeKeyUpListener(Uint32 key)
KeyCategory keyCategory()
bool addKeyUpListener(Uint32 key, KeyListener listener)
virtual void queryEvents(std::vector< Uint32 > &eventset) override
const std::vector< KeyCode > keySet() const
virtual ~Keyboard()
Definition: CUKeyboard.h:396
Definition: CUAnimationNode.h:52
Definition: CUKeyboard.h:371
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:442
virtual void clearState() override