process_chording.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_chording.h"
  17. bool keys_chord(uint8_t keys[]) {
  18. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  19. bool pass = true;
  20. uint8_t in = 0;
  21. for (uint8_t i = 0; i < chord_key_count; i++) {
  22. bool found = false;
  23. for (uint8_t j = 0; j < keys_size; j++) {
  24. if (chord_keys[i] == (keys[j] & 0xFF)) {
  25. in++; // detects key in chord
  26. found = true;
  27. break;
  28. }
  29. }
  30. if (found)
  31. continue;
  32. if (chord_keys[i] != 0) {
  33. pass = false; // makes sure rest are blank
  34. }
  35. }
  36. return (pass && (in == keys_size));
  37. }
  38. bool process_chording(uint16_t keycode, keyrecord_t *record) {
  39. if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
  40. if (record->event.pressed) {
  41. if (!chording) {
  42. chording = true;
  43. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  44. chord_keys[i] = 0;
  45. chord_key_count = 0;
  46. chord_key_down = 0;
  47. }
  48. chord_keys[chord_key_count] = (keycode & 0xFF);
  49. chord_key_count++;
  50. chord_key_down++;
  51. return false;
  52. } else {
  53. if (chording) {
  54. chord_key_down--;
  55. if (chord_key_down == 0) {
  56. chording = false;
  57. // Chord Dictionary
  58. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  59. register_code(KC_A);
  60. unregister_code(KC_A);
  61. return false;
  62. }
  63. for (uint8_t i = 0; i < chord_key_count; i++) {
  64. register_code(chord_keys[i]);
  65. unregister_code(chord_keys[i]);
  66. return false;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return true;
  73. }