Keyboard.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /***************************************************************************
  2. Keyboard.cpp - description
  3. -------------------
  4. begin : Thu Feb 1 2001
  5. copyright : (C) 2001 by Henrik Enqvist
  6. email : henqvist@excite.com
  7. ***************************************************************************/
  8. #include <cstring>
  9. #include "Private.h"
  10. #include "Keyboard.h"
  11. #if EM_USE_SDL
  12. bool Keyboard::m_abKey[KEY_MAX];
  13. #endif
  14. Keyboard::Keyboard(){
  15. this->clear();
  16. }
  17. Keyboard::~Keyboard(){
  18. }
  19. void Keyboard::poll() {
  20. #if EM_USE_SDL
  21. SDL_Event event;
  22. while(SDL_PollEvent(&event)) {
  23. if (event.type == SDL_KEYDOWN) {
  24. m_abKey[event.key.keysym.sym] = true;
  25. }
  26. if (event.type == SDL_KEYUP) {
  27. m_abKey[event.key.keysym.sym] = false;
  28. }
  29. }
  30. #endif
  31. #if EM_USE_ALLEGRO
  32. poll_keyboard();
  33. #endif
  34. }
  35. void Keyboard::clear() {
  36. #if EM_USE_SDL
  37. memset(m_abKey, false, KEY_MAX*sizeof(bool));
  38. #endif
  39. #if EM_USE_ALLEGRO
  40. clear_keybuf();
  41. #endif
  42. }
  43. EMKey Keyboard::waitForKey() {
  44. #if EM_USE_SDL
  45. while(true) {
  46. SDL_Event event;
  47. SDL_WaitEvent(&event);
  48. if (event.type == SDL_KEYDOWN && event.key.state == SDL_PRESSED) {
  49. break;
  50. }
  51. }
  52. while (true) {
  53. SDL_Event event;
  54. SDL_WaitEvent(&event);
  55. if (event.type == SDL_KEYUP && event.key.state == SDL_RELEASED) {
  56. return event.key.keysym.sym;
  57. }
  58. }
  59. #endif
  60. #if EM_USE_ALLEGRO
  61. // TODO
  62. return (readkey() >> 8);
  63. #endif
  64. }
  65. bool Keyboard::isKeyDown(int piKey) {
  66. if (piKey < 0 || piKey >= KEY_MAX) return false;
  67. #if EM_USE_SDL
  68. return m_abKey[piKey];
  69. #endif
  70. #if EM_USE_ALLEGRO
  71. return key[piKey];
  72. #endif
  73. }