CUGL 1.3
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 #ifndef __CU_KEYBOARD_H__
34 #define __CU_KEYBOARD_H__
35 
36 #include <cugl/input/CUInput.h>
37 #include <cugl/util/CUTimestamp.h>
38 #include <functional>
39 
40 namespace cugl {
41 
42 #pragma mark KeyCode
43 
54 enum class KeyCode : int {
56  NUM_0 = SDLK_0,
58  NUM_1 = SDLK_1,
60  NUM_2 = SDLK_2,
62  NUM_3 = SDLK_3,
64  NUM_4 = SDLK_4,
66  NUM_5 = SDLK_5,
68  NUM_6 = SDLK_6,
70  NUM_7 = SDLK_7,
72  NUM_8 = SDLK_8,
74  NUM_9 = SDLK_9,
75 
77  A = SDLK_a,
79  B = SDLK_b,
81  C = SDLK_c,
83  D = SDLK_d,
85  E = SDLK_e,
87  F = SDLK_f,
89  G = SDLK_g,
91  H = SDLK_h,
93  I = SDLK_i,
95  J = SDLK_j,
97  K = SDLK_k,
99  L = SDLK_l,
101  M = SDLK_m,
103  N = SDLK_n,
105  O = SDLK_o,
107  P = SDLK_p,
109  Q = SDLK_q,
111  R = SDLK_r,
113  S = SDLK_s,
115  T = SDLK_t,
117  U = SDLK_u,
119  V = SDLK_v,
121  W = SDLK_w,
123  X = SDLK_x,
125  Y = SDLK_y,
127  Z = SDLK_z,
128 
130  ARROW_DOWN = SDLK_DOWN,
132  ARROW_LEFT = SDLK_LEFT,
134  ARROW_RIGHT = SDLK_RIGHT,
136  ARROW_UP = SDLK_UP,
137 
139  QUOTE = SDLK_QUOTE,
141  BACKSLASH = SDLK_BACKSLASH,
143  COMMA = SDLK_COMMA,
145  EQUALS = SDLK_EQUALS,
147  BACKQUOTE = SDLK_BACKQUOTE,
149  LEFT_BRACKET = SDLK_LEFTBRACKET,
151  MINUS = SDLK_MINUS,
153  PERIOD = SDLK_PERIOD,
155  RIGHT_BRACKET = SDLK_RIGHTBRACKET,
157  SEMICOLON = SDLK_SEMICOLON,
159  SLASH = SDLK_SLASH,
161  BACKSPACE = SDLK_BACKSPACE,
163  SPACE = SDLK_SPACE,
165  TAB = SDLK_TAB,
166 
168  DEL = SDLK_DELETE,
170  END = SDLK_END,
172  ESCAPE = SDLK_ESCAPE,
174  HOME = SDLK_HOME,
176  HELP = SDLK_HELP,
178  PAGE_DOWN = SDLK_PAGEDOWN,
180  PAGE_UP = SDLK_PAGEUP,
182  PAUSE = SDLK_PAUSE,
184  RETURN = SDLK_RETURN,
186  ENTER = SDLK_RETURN2,
187 
189  CAPS_LOCK = SDLK_CAPSLOCK,
191  LEFT_ALT = SDLK_LALT,
193  LEFT_CTRL = SDLK_LCTRL,
195  LEFT_SHIFT = SDLK_LSHIFT,
197  LEFT_META = SDLK_LGUI,
199  RIGHT_ALT = SDLK_RALT,
201  RIGHT_CTRL = SDLK_RCTRL,
203  RIGHT_SHIFT = SDLK_RSHIFT,
205  RIGHT_META = SDLK_RGUI,
207  NUMLOCK = SDLK_NUMLOCKCLEAR,
208 
210  KEYPAD_0 = SDLK_KP_0,
212  KEYPAD_1 = SDLK_KP_1,
214  KEYPAD_2 = SDLK_KP_2,
216  KEYPAD_3 = SDLK_KP_3,
218  KEYPAD_4 = SDLK_KP_4,
220  KEYPAD_5 = SDLK_KP_5,
222  KEYPAD_6 = SDLK_KP_6,
224  KEYPAD_7 = SDLK_KP_7,
226  KEYPAD_8 = SDLK_KP_8,
228  KEYPAD_9 = SDLK_KP_9,
230  KEYPAD_CLEAR = SDLK_KP_CLEAR,
232  KEYPAD_EQUALS = SDLK_KP_EQUALS,
234  KEYPAD_DIVIDE = SDLK_KP_DIVIDE,
236  KEYPAD_MULTIPLY = SDLK_KP_MULTIPLY,
238  KEYPAD_MINUS = SDLK_KP_MINUS,
240  KEYPAD_PLUS = SDLK_KP_PLUS,
242  KEYPAD_ENTER = SDLK_KP_ENTER,
243 
245  UNKNOWN = SDLK_POWER
246 };
247 
257  std::size_t operator()(const KeyCode& k) const {
258  return (std::hash<int>()(static_cast<int>(k)));
259  }
260 };
261 
262 
263 #pragma mark -
264 
269 enum class KeyCategory {
271  NUMBER,
273  LETTER,
275  ARROW,
277  PUNCTUATION,
279  SPECIAL,
281  MODIFIER,
283  KEYPAD,
285  UNKNOWN
286 };
287 
288 #pragma mark -
289 
293 class KeyEvent {
294 public:
298  KeyCode keycode;
299 
303  KeyEvent() : keycode(KeyCode::UNKNOWN) {}
304 
311  KeyEvent(KeyCode code, const Timestamp& stamp) {
312  keycode = code; timestamp = stamp;
313  }
314 
322  KeyCategory keyCategory();
323 };
324 
325 #pragma mark -
326 
345 class Keyboard : public InputDevice {
346 public:
347 #pragma mark Listener
348 
373  typedef std::function<void(const KeyEvent& event, bool focus)> Listener;
374 
375 #pragma mark Values
376 protected:
378  std::unordered_set<KeyCode,KeyCodeHasher> _previous;
380  std::unordered_set<KeyCode,KeyCodeHasher> _current;
381 
383  std::unordered_map<Uint32, Listener> _downListeners;
385  std::unordered_map<Uint32, Listener> _upListeners;
386 
387 #pragma mark Constructor
388 
395 
399  virtual ~Keyboard() {}
400 
406  virtual void dispose() override;
407 
408 public:
409 #pragma mark Data Polling
410 
417  bool keyDown(KeyCode code) const {
418  return _current.find(code) != _current.end();
419  }
420 
431  bool keyPressed(KeyCode code) const {
432  return _current.find(code) != _current.end() && _previous.find(code) == _previous.end();
433  }
434 
445  bool keyReleased(KeyCode code) {
446  return _current.find(code) == _current.end() && _previous.find(code) != _previous.end();
447  }
448 
454  unsigned int keyCount() const { return (unsigned int)_current.size(); }
455 
464  const std::vector<KeyCode> keySet() const;
465 
475  static KeyCategory keyCategory(KeyCode code);
476 
477 #pragma mark Listeners
478 
488  virtual bool requestFocus(Uint32 key) override;
489 
500  bool isListener(Uint32 key) const;
501 
513  const Listener getKeyDownListener(Uint32 key) const;
514 
526  const Listener getKeyUpListener(Uint32 key) const;
527 
542  bool addKeyDownListener(Uint32 key, Listener listener);
543 
558  bool addKeyUpListener(Uint32 key, Listener listener);
559 
572  bool removeKeyDownListener(Uint32 key);
573 
586  bool removeKeyUpListener(Uint32 key);
587 
588 protected:
589 #pragma mark Input Device
590 
596  virtual void clearState() override;
597 
609  virtual bool updateState(const SDL_Event& event, const Timestamp& stamp) override;
610 
621  virtual void queryEvents(std::vector<Uint32>& eventset) override;
622 
623  // Apparently friends are not inherited
624  friend class Input;
625 };
626 
627 }
628 
629 #endif /* __CU_KEYBOARD_H__ */
cugl::KeyEvent::keycode
KeyCode keycode
Definition: CUKeyboard.h:298
cugl::Keyboard::keyCategory
static KeyCategory keyCategory(KeyCode code)
cugl::Keyboard::removeKeyDownListener
bool removeKeyDownListener(Uint32 key)
cugl::KeyEvent::KeyEvent
KeyEvent()
Definition: CUKeyboard.h:303
cugl::Keyboard::keyDown
bool keyDown(KeyCode code) const
Definition: CUKeyboard.h:417
cugl::Keyboard::keyCount
unsigned int keyCount() const
Definition: CUKeyboard.h:454
cugl::Keyboard::clearState
virtual void clearState() override
cugl::Keyboard::_previous
std::unordered_set< KeyCode, KeyCodeHasher > _previous
Definition: CUKeyboard.h:378
cugl::Keyboard::keyPressed
bool keyPressed(KeyCode code) const
Definition: CUKeyboard.h:431
cugl::Keyboard::Keyboard
Keyboard()
Definition: CUKeyboard.h:394
cugl::Keyboard::isListener
bool isListener(Uint32 key) const
cugl::Timestamp
Definition: CUTimestamp.h:61
cugl::Keyboard::addKeyUpListener
bool addKeyUpListener(Uint32 key, Listener listener)
cugl::Keyboard::keyReleased
bool keyReleased(KeyCode code)
Definition: CUKeyboard.h:445
cugl::Keyboard::addKeyDownListener
bool addKeyDownListener(Uint32 key, Listener listener)
cugl::Keyboard::dispose
virtual void dispose() override
cugl::Keyboard::queryEvents
virtual void queryEvents(std::vector< Uint32 > &eventset) override
cugl::KeyEvent
Definition: CUKeyboard.h:293
cugl::Keyboard::getKeyDownListener
const Listener getKeyDownListener(Uint32 key) const
cugl::Keyboard::_downListeners
std::unordered_map< Uint32, Listener > _downListeners
Definition: CUKeyboard.h:383
cugl::KeyEvent::KeyEvent
KeyEvent(KeyCode code, const Timestamp &stamp)
Definition: CUKeyboard.h:311
cugl::Keyboard::keySet
const std::vector< KeyCode > keySet() const
cugl::KeyCodeHasher
Definition: CUKeyboard.h:251
cugl::KeyCodeHasher::operator()
std::size_t operator()(const KeyCode &k) const
Definition: CUKeyboard.h:257
cugl::Keyboard::~Keyboard
virtual ~Keyboard()
Definition: CUKeyboard.h:399
cugl::Keyboard::requestFocus
virtual bool requestFocus(Uint32 key) override
cugl::Keyboard::_upListeners
std::unordered_map< Uint32, Listener > _upListeners
Definition: CUKeyboard.h:385
cugl::Keyboard::updateState
virtual bool updateState(const SDL_Event &event, const Timestamp &stamp) override
cugl::KeyEvent::timestamp
Timestamp timestamp
Definition: CUKeyboard.h:296
cugl::Input
Definition: CUInput.h:77
cugl::Keyboard::Listener
std::function< void(const KeyEvent &event, bool focus)> Listener
Definition: CUKeyboard.h:373
cugl::Keyboard
Definition: CUKeyboard.h:345
cugl::InputDevice
Definition: CUInput.h:298
cugl::Keyboard::getKeyUpListener
const Listener getKeyUpListener(Uint32 key) const
cugl::KeyEvent::keyCategory
KeyCategory keyCategory()
cugl::Keyboard::removeKeyUpListener
bool removeKeyUpListener(Uint32 key)
cugl::Keyboard::_current
std::unordered_set< KeyCode, KeyCodeHasher > _current
Definition: CUKeyboard.h:380