Keyboard.h 3.0 KB

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