keymap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2022 Paul Ewing
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include QMK_KEYBOARD_H
  18. #include "key_repeater.h"
  19. #include <stdlib.h>
  20. enum {
  21. LAYER_DEFAULT,
  22. LAYER_FN,
  23. LAYER_MACRO,
  24. __LAYER_COUNT,
  25. };
  26. #define TO_MACRO TO(LAYER_MACRO)
  27. #define TO_DFLT TO(LAYER_DEFAULT)
  28. #define MO_FN MO(LAYER_FN)
  29. #define RGB_N RGB_MOD // Rotate to next RGB mode
  30. #define RGB_P RGB_RMOD // Rotate to next RGB mode
  31. #define KC_YANK LCTL(KC_INS) // Copy shortcut in most terminal emulators
  32. #define KC_PUT LSFT(KC_INS) // Paste shortcut in most terminal emulators
  33. // Custom keycodes
  34. enum {
  35. SH_TOG = SAFE_RANGE, // Toggle shift
  36. SH_BTN1, // Shift left click
  37. RP_BTN1, // Click repeatedly while key is held
  38. };
  39. const uint16_t PROGMEM keymaps[__LAYER_COUNT][MATRIX_ROWS][MATRIX_COLS] = {
  40. [LAYER_DEFAULT] = LAYOUT(
  41. KC_EQL, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS,
  42. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_UP, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSLS,
  43. KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_LEFT, KC_RGHT, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,
  44. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_DOWN, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT,
  45. KC_LCTL, KC_LGUI, KC_LALT, KC_GRV, MO_FN, KC_BSPC, KC_DEL, KC_ENT, KC_SPC, KC_LBRC, KC_RBRC, KC_RALT, KC_RGUI, KC_RCTL
  46. ),
  47. [LAYER_FN] = LAYOUT(
  48. RGB_HUI, RGB_HUD, RGB_SAI, RGB_SAD, RGB_VAI, RGB_VAD, _______, _______, _______, _______, _______, RESET,
  49. _______, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, RGB_TOG, KC_YANK, KC_GRV, KC_LBRC, KC_RBRC, KC_PUT, _______,
  50. KC_CAPS, KC_F5, KC_F6, KC_F7, KC_F8, KC_INS, RGB_N, RGB_P, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT, _______, _______,
  51. _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_PAUS, RGB_M_P, KC_HOME, KC_PGDN, KC_PGUP, KC_END, _______, _______,
  52. _______, _______, _______, _______, _______, _______, _______, TO_MACRO, _______, _______, _______, _______, _______, _______
  53. ),
  54. [LAYER_MACRO] = LAYOUT(
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, RP_BTN1, SH_TOG, _______, _______, _______, _______, _______, _______, _______,
  57. TO_DFLT, _______, _______, KC_BTN1, SH_BTN1, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  58. _______, _______, _______, KC_1, KC_6, _______, _______, _______, _______, _______, _______, _______, _______,
  59. _______, _______, _______, _______, _______, KC_BTN1, _______, _______, _______, _______, _______, _______, _______, _______
  60. ),
  61. };
  62. static bool shift_enabled = false;
  63. static struct key_repeater_t* click_repeater = NULL;
  64. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  65. switch (keycode) {
  66. case SH_TOG:
  67. if (record->event.pressed) {
  68. if (shift_enabled) {
  69. unregister_code(KC_LSFT);
  70. } else {
  71. register_code(KC_LSFT);
  72. }
  73. shift_enabled = !shift_enabled;
  74. }
  75. return false; // Skip all further processing of this key
  76. case SH_BTN1:
  77. if (record->event.pressed) {
  78. register_code(KC_LSFT);
  79. register_code(KC_BTN1);
  80. } else {
  81. unregister_code(KC_BTN1);
  82. unregister_code(KC_LSFT);
  83. }
  84. return false;
  85. case RP_BTN1:
  86. if (record->event.pressed) {
  87. kr_enable(click_repeater);
  88. } else {
  89. kr_disable(click_repeater);
  90. }
  91. return false;
  92. default:
  93. return true; // Process all other keycodes normally
  94. }
  95. }
  96. void keyboard_post_init_user(void) {
  97. // Seed the random number generator which is used by the key repeater
  98. srand(timer_read32());
  99. // Configure and instantiate a key repeater for mouse button 1 "rapid fire"
  100. struct key_repeater_config_t cfg = {
  101. .key = KC_BTN1,
  102. .key_duration_min = 20,
  103. .key_duration_max = 50,
  104. .wait_duration_min = 90,
  105. .wait_duration_max = 140,
  106. };
  107. click_repeater = kr_new(&cfg);
  108. }
  109. void matrix_scan_user(void) {
  110. kr_poll(click_repeater);
  111. }