Keyboard.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "Assert.h"
  28. #undef MK_SHIFT
  29. #undef MK_ALT
  30. namespace crown
  31. {
  32. /// Enumerates modifier keys.
  33. enum ModifierKey
  34. {
  35. MK_SHIFT = 1,
  36. MK_CTRL = 2,
  37. MK_ALT = 4
  38. };
  39. /// Interface for accessing keyboard input device.
  40. class Keyboard
  41. {
  42. public:
  43. Keyboard()
  44. : m_modifier(0), m_current_frame(0), m_last_key(KC_NOKEY)
  45. {
  46. for (uint32_t i = 0; i < MAX_KEYCODES; i++)
  47. {
  48. m_keys[i] = ~0;
  49. m_state[i] = false;
  50. }
  51. }
  52. /// Returns whether the specified @a modifier is pressed.
  53. /// @note
  54. /// A modifier is a special key that modifies the normal action
  55. /// of another key when the two are pressed in combination. (Thanks wikipedia.)
  56. /// @note
  57. /// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
  58. bool modifier_pressed(ModifierKey modifier) const
  59. {
  60. return (m_modifier & modifier) == modifier;
  61. }
  62. /// Returns whether the specified @a key is pressed in the current frame.
  63. bool key_pressed(KeyCode key) const
  64. {
  65. CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
  66. return (m_state[key] == true) && (m_keys[key] == m_current_frame);
  67. }
  68. /// Returns whether the specified @a key is released in the current frame.
  69. bool key_released(KeyCode key) const
  70. {
  71. CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
  72. return (m_state[key] == false) && (m_keys[key] == m_current_frame);
  73. }
  74. /// Returns wheter any key is pressed in the current frame.
  75. bool any_pressed() const
  76. {
  77. return key_pressed(m_last_key);
  78. }
  79. /// Returns whether any key is released in the current frame.
  80. bool any_released() const
  81. {
  82. return key_released(m_last_key);
  83. }
  84. private:
  85. void update(uint64_t frame, KeyCode k, bool state)
  86. {
  87. CE_ASSERT(k >= 0 && k < MAX_KEYCODES, "KeyCode out of range: %d", k);
  88. m_last_key = k;
  89. m_keys[k] = frame;
  90. m_state[k] = state;
  91. }
  92. private:
  93. uint8_t m_modifier;
  94. // The current frame number
  95. uint64_t m_current_frame;
  96. // Last key updated
  97. KeyCode m_last_key;
  98. uint64_t m_keys[MAX_KEYCODES];
  99. bool m_state[MAX_KEYCODES];
  100. friend class Device;
  101. };
  102. } // namespace crown