keyboard.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file sdl_kb.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "keyboard.h"
  9. #if USE_KEYBOARD
  10. #include "lvgl/lv_core/lv_group.h"
  11. /*********************
  12. * DEFINES
  13. *********************/
  14. /**********************
  15. * TYPEDEFS
  16. **********************/
  17. /**********************
  18. * STATIC PROTOTYPES
  19. **********************/
  20. static uint32_t keycode_to_ascii(uint32_t sdl_key);
  21. /**********************
  22. * STATIC VARIABLES
  23. **********************/
  24. static uint32_t last_key;
  25. static lv_indev_state_t state;
  26. /**********************
  27. * MACROS
  28. **********************/
  29. /**********************
  30. * GLOBAL FUNCTIONS
  31. **********************/
  32. /**
  33. * Initialize the keyboard
  34. */
  35. void keyboard_init(void)
  36. {
  37. /*Nothing to init*/
  38. }
  39. /**
  40. * Get the last pressed or released character from the PC's keyboard
  41. * @param data store the read data here
  42. * @return false: because the points are not buffered, so no more data to be read
  43. */
  44. bool keyboard_read(lv_indev_data_t * data)
  45. {
  46. data->state = state;
  47. if(state == LV_INDEV_STATE_REL) {
  48. data->key = 0;
  49. } else {
  50. data->key = keycode_to_ascii(last_key);
  51. }
  52. return false;
  53. }
  54. void keyboard_handler(SDL_Event *event)
  55. {
  56. /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
  57. switch( event->type ){
  58. case SDL_KEYDOWN:
  59. last_key = event->key.keysym.sym;
  60. state = LV_INDEV_STATE_PR;
  61. break;
  62. case SDL_KEYUP:
  63. state = LV_INDEV_STATE_REL;
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. /**********************
  70. * STATIC FUNCTIONS
  71. **********************/
  72. static uint32_t keycode_to_ascii(uint32_t sdl_key)
  73. {
  74. /*Remap some key to LV_GROUP_KEY_... to manage groups*/
  75. switch(sdl_key) {
  76. case SDLK_RIGHT:
  77. case SDLK_KP_PLUS:
  78. return LV_GROUP_KEY_RIGHT;
  79. case SDLK_LEFT:
  80. case SDLK_KP_MINUS:
  81. return LV_GROUP_KEY_LEFT;
  82. case SDLK_UP:
  83. return LV_GROUP_KEY_UP;
  84. case SDLK_DOWN:
  85. return LV_GROUP_KEY_DOWN;
  86. case SDLK_ESCAPE:
  87. return LV_GROUP_KEY_ESC;
  88. case SDLK_KP_ENTER:
  89. return LV_GROUP_KEY_ENTER;
  90. case '\r':
  91. return LV_GROUP_KEY_ENTER;
  92. default: return sdl_key;
  93. }
  94. }
  95. #endif