You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB

  1. #include QMK_KEYBOARD_H
  2. #include "tap_tog.h"
  3. bool tap_tog_layer_other_key_pressed = false;
  4. bool tap_tog_layer_toggled_on = false;
  5. uint8_t tap_tog_count = 0;
  6. void process_tap_tog(uint8_t layer, keyrecord_t *record) {
  7. tap_tog_count++;
  8. // press
  9. if (record->event.pressed) {
  10. // TTL has already been pressed and we are toggled into that layer
  11. // so now we need to leave
  12. if(tap_tog_layer_toggled_on) {
  13. layer_clear();
  14. tap_tog_layer_toggled_on = false;
  15. }
  16. // this means we're in our default layer
  17. // so switch the layer immediately
  18. // whether we'll switch back when it's released depends on if a button gets pressed while this is held down
  19. else {
  20. // switch layer
  21. layer_on(layer);
  22. tap_tog_layer_other_key_pressed = false; // if this becomes true before it gets released, it will act as a held modifier
  23. }
  24. }
  25. // release
  26. else {
  27. // if it was used as a held modifier (like traditional shift)
  28. if(tap_tog_layer_other_key_pressed) {
  29. // switch layer back
  30. layer_clear();
  31. }
  32. // if it was used as a toggle button
  33. else {
  34. // next time, it will turn layer off
  35. tap_tog_layer_toggled_on = true;
  36. // If it's been tapped twice, reset the toggle flag.
  37. // Otherwise, we get stuck oscillating between this code block and the
  38. // pressed && TTL_toggled_on block.
  39. if (tap_tog_count >= 4 ) {
  40. tap_tog_count = 0;
  41. tap_tog_layer_toggled_on = false;
  42. }
  43. }
  44. }
  45. }