keylogger.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include "crkbd.h"
  3. char keylog_str[24] = {};
  4. char keylogs_str[21] = {};
  5. int keylogs_str_idx = 0;
  6. const char code_to_name[60] = {
  7. ' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f',
  8. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  9. 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  10. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  11. 'R', 'E', 'B', 'T', ' ', ' ', ' ', ' ', ' ', ' ',
  12. ' ', ';', '\'', ' ', ',', '.', '/', ' ', ' ', ' '};
  13. void set_keylog(uint16_t keycode, keyrecord_t *record) {
  14. char name = ' ';
  15. if (keycode < 60) {
  16. name = code_to_name[keycode];
  17. }
  18. // update keylog
  19. snprintf(keylog_str, sizeof(keylog_str), "%dx%d, k%2d : %c",
  20. record->event.key.row, record->event.key.col,
  21. keycode, name);
  22. // update keylogs
  23. if (keylogs_str_idx == sizeof(keylogs_str) - 1) {
  24. keylogs_str_idx = 0;
  25. for (int i = 0; i < sizeof(keylogs_str) - 1; i++) {
  26. keylogs_str[i] = ' ';
  27. }
  28. }
  29. keylogs_str[keylogs_str_idx] = name;
  30. keylogs_str_idx++;
  31. }
  32. const char *read_keylog(void) {
  33. return keylog_str;
  34. }
  35. const char *read_keylogs(void) {
  36. return keylogs_str;
  37. }