process_chording.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "process_chording.h"
  2. bool keys_chord(uint8_t keys[]) {
  3. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  4. bool pass = true;
  5. uint8_t in = 0;
  6. for (uint8_t i = 0; i < chord_key_count; i++) {
  7. bool found = false;
  8. for (uint8_t j = 0; j < keys_size; j++) {
  9. if (chord_keys[i] == (keys[j] & 0xFF)) {
  10. in++; // detects key in chord
  11. found = true;
  12. break;
  13. }
  14. }
  15. if (found)
  16. continue;
  17. if (chord_keys[i] != 0) {
  18. pass = false; // makes sure rest are blank
  19. }
  20. }
  21. return (pass && (in == keys_size));
  22. }
  23. bool process_chording(uint16_t keycode, keyrecord_t *record) {
  24. if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
  25. if (record->event.pressed) {
  26. if (!chording) {
  27. chording = true;
  28. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  29. chord_keys[i] = 0;
  30. chord_key_count = 0;
  31. chord_key_down = 0;
  32. }
  33. chord_keys[chord_key_count] = (keycode & 0xFF);
  34. chord_key_count++;
  35. chord_key_down++;
  36. return false;
  37. } else {
  38. if (chording) {
  39. chord_key_down--;
  40. if (chord_key_down == 0) {
  41. chording = false;
  42. // Chord Dictionary
  43. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  44. register_code(KC_A);
  45. unregister_code(KC_A);
  46. return false;
  47. }
  48. for (uint8_t i = 0; i < chord_key_count; i++) {
  49. register_code(chord_keys[i]);
  50. unregister_code(chord_keys[i]);
  51. return false;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. return true;
  58. }