Keyboard.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "KeyCode.h"
  26. #include "OS.h"
  27. #undef MK_SHIFT
  28. #undef MK_ALT
  29. namespace crown
  30. {
  31. class InputManager;
  32. /// Enumerates modifier keys.
  33. enum ModifierKey
  34. {
  35. MK_SHIFT = 1,
  36. MK_CTRL = 2,
  37. MK_ALT = 4
  38. };
  39. struct KeyboardEvent
  40. {
  41. KeyCode key;
  42. uint8_t modifier;
  43. char text[4];
  44. };
  45. class KeyboardListener
  46. {
  47. public:
  48. virtual void key_pressed(const KeyboardEvent& event) { (void)event; }
  49. virtual void key_released(const KeyboardEvent& event) { (void)event; }
  50. virtual void text_input(const KeyboardEvent& event) { (void)event; }
  51. };
  52. /// Interface for accessing keyboard input device.
  53. class Keyboard
  54. {
  55. public:
  56. Keyboard();
  57. /// Returns whether the specified @a modifier is pressed.
  58. /// @note
  59. /// A modifier is a special key that modifies the normal action
  60. /// of another key when the two are pressed in combination. (Thanks wikipedia.)
  61. /// @note
  62. /// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
  63. bool modifier_pressed(ModifierKey modifier) const;
  64. /// Returns whether the specified @a key is pressed in the current frame.
  65. bool key_pressed(KeyCode key) const;
  66. /// Returns whether the specified @a key is released in the current frame.
  67. bool key_released(KeyCode key) const;
  68. /// Returns wheter any key is pressed in the current frame.
  69. bool any_pressed() const;
  70. /// Returns whether any key is released in the current frame.
  71. bool any_released() const;
  72. private:
  73. void update(uint64_t frame, KeyCode k, bool state);
  74. uint8_t m_modifier;
  75. // The current frame number
  76. uint64_t m_current_frame;
  77. // Last key updated
  78. KeyCode m_last_key;
  79. uint64_t m_keys[MAX_KEYCODES];
  80. bool m_state[MAX_KEYCODES];
  81. friend class InputManager;
  82. };
  83. } // namespace crown