Keyboard.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <cstring>
  25. #include "Types.h"
  26. #include "KeyCode.h"
  27. #include "Assert.h"
  28. namespace crown
  29. {
  30. /// Enumerates modifier keys.
  31. struct ModifierButton
  32. {
  33. enum Enum
  34. {
  35. SHIFT = 1,
  36. CTRL = 2,
  37. ALT = 4
  38. };
  39. };
  40. /// Interface for accessing keyboard input device.
  41. struct Keyboard
  42. {
  43. Keyboard()
  44. : m_modifier(0), m_last_button(KeyboardButton::NONE)
  45. {
  46. memset(m_last_state, 0, KeyboardButton::COUNT);
  47. memset(m_current_state, 0, KeyboardButton::COUNT);
  48. }
  49. /// Returns whether the specified @a modifier is pressed.
  50. /// @note
  51. /// A modifier is a special key that modifies the normal action
  52. /// of another key when the two are pressed in combination. (Thanks wikipedia.)
  53. /// @note
  54. /// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
  55. bool modifier_pressed(ModifierButton::Enum modifier) const
  56. {
  57. return (m_modifier & (uint8_t) modifier) == modifier;
  58. }
  59. /// Returns whether the specified @a b button is pressed in the current frame.
  60. bool button_pressed(KeyboardButton::Enum b) const
  61. {
  62. return bool(~m_last_state[b] & m_current_state[b]);
  63. }
  64. /// Returns whether the specified @a b button is released in the current frame.
  65. bool button_released(KeyboardButton::Enum b) const
  66. {
  67. return bool(m_last_state[b] & ~m_current_state[b]);
  68. }
  69. /// Returns wheter any button is pressed in the current frame.
  70. bool any_pressed()
  71. {
  72. return button_pressed(m_last_button);
  73. }
  74. /// Returns whether any button is released in the current frame.
  75. bool any_released()
  76. {
  77. return button_released(m_last_button);
  78. }
  79. //-------------------------------------------------------------------------
  80. void set_button_state(KeyboardButton::Enum b, bool state)
  81. {
  82. m_last_button = b;
  83. m_current_state[b] = state;
  84. }
  85. //-------------------------------------------------------------------------
  86. void update()
  87. {
  88. memcpy(m_last_state, m_current_state, KeyboardButton::COUNT);
  89. }
  90. public:
  91. uint8_t m_modifier;
  92. KeyboardButton::Enum m_last_button;
  93. uint8_t m_last_state[KeyboardButton::COUNT];
  94. uint8_t m_current_state[KeyboardButton::COUNT];
  95. };
  96. } // namespace crown