keymap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright 2022 elliotpatros
  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 QMK_KEYBOARD_H
  17. // Layer definitions
  18. enum custom_layers {
  19. _COLEMAK,
  20. _FUNCTION,
  21. };
  22. // Custom keycodes, implemented later, in function: process_record_user()
  23. enum custom_keycodes {
  24. CTL_ESC = SAFE_RANGE, // mod tap: left control / esc
  25. SFT_ENT, // mod tap: left shift / enter
  26. LST_PRN, // mod tap: left shift / left parenthesis
  27. RST_PRN, // mod tap: right shift / right parenthesis
  28. };
  29. // Shorthand keycode definitions
  30. #define OSM_CAG OSM(MOD_LCTL | MOD_LALT | MOD_LGUI)
  31. #define LT_BSFN LT(_FUNCTION, KC_BSLS)
  32. // The layout
  33. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  34. // Default layer
  35. [_COLEMAK] = LAYOUT (
  36. // ┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐
  37. KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS,
  38. // ├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤
  39. KC_TAB, KC_Q, KC_W, KC_F, KC_P, KC_G, KC_J, KC_L, KC_U, KC_Y, KC_SCLN, KC_EQL,
  40. // ├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤
  41. CTL_ESC, KC_A, KC_R, KC_S, KC_T, KC_D, KC_H, KC_N, KC_E, KC_I, KC_O, KC_QUOT,
  42. // ├────────┼────────┼────────┼────────┼────────┼────────┼────────┐┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤
  43. LST_PRN, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_SPC, KC_ENT, KC_K, KC_M, KC_COMM, KC_DOT, KC_SLSH, RST_PRN,
  44. // └────────┴────────┴────────┴────────┼────────┼────────┼────────┘└────────┼────────┴────────┴────────┴────────┴────────┴────────┘
  45. KC_LALT, KC_LGUI, SFT_ENT, KC_SPC, LT_BSFN, KC_BSPC
  46. // └────────┴────────┴────────┘└────────┴────────┴────────┘
  47. ),
  48. // Function (and I guess also media & navigation) layer
  49. [_FUNCTION] = LAYOUT (
  50. // ┌────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐
  51. _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, _______,
  52. // ├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤
  53. _______, KC_GRV, KC_LPRN, KC_RPRN, KC_F11, KC_F12, KC_INS, KC_HOME, KC_UP, KC_PGUP, _______, _______,
  54. // ├────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┤
  55. _______, KC_LCBR, KC_LBRC, KC_RBRC, KC_RCBR, OSM_CAG, KC_VOLU, KC_LEFT, KC_DOWN, KC_RGHT, KC_BSPC, _______,
  56. // ├────────┼────────┼────────┼────────┼────────┼────────┼────────┐┌────────┼────────┼────────┼────────┼────────┼────────┼────────┤
  57. _______, _______, _______, _______, _______, KC_MUTE, _______, _______, KC_VOLD, KC_DEL, KC_END, KC_PGDN, _______, _______,
  58. // └────────┴────────┴────────┴────────┼────────┼────────┼────────┘└────────┼────────┴────────┴────────┴────────┴────────┴────────┘
  59. _______, _______, _______, _______, _______, _______
  60. // └────────┴────────┴────────┘└────────┴────────┴────────┘
  61. )
  62. };
  63. static inline bool dualfunckey_was_tapped(const uint16_t time_when_pressed) {
  64. return timer_elapsed(time_when_pressed) < TAPPING_TERM;
  65. }
  66. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  67. // Static variables
  68. static uint16_t timer_control_escape = 0;
  69. static uint16_t timer_shift_enter = 0;
  70. static uint16_t timer_shift_lparen = 0;
  71. static uint16_t timer_shift_rparen = 0;
  72. static bool dualfunckey_pending = false;
  73. static bool shift_enter_is_pressed = false;
  74. static bool shift_lparen_is_pressed = false;
  75. // Was this function called to handle a keydown? If not, it was called by a
  76. // key up.
  77. const bool pressed = record->event.pressed;
  78. // Any keydown (at all) spends a pending dual function key check. Why? If,
  79. // for example, you use shift to type a capital letter, and shift keys down
  80. // and up faster than the tapping term, did you mean to call shift's tapping
  81. // key? No. You just meant to call shift.
  82. if (pressed) {
  83. dualfunckey_pending = false;
  84. }
  85. // Only keycodes >= SAFE_RANGE are handled in this function
  86. if (keycode < SAFE_RANGE) {
  87. // This keypress was not handled
  88. return true;
  89. }
  90. // Handle custom keypresses here
  91. switch (keycode) {
  92. // -------------------------------------------------------------------------
  93. // Dual function key (hold: left shift; tap: enter)
  94. // -------------------------------------------------------------------------
  95. case SFT_ENT:
  96. // There are multiple keys that are mapped to left shift. Keep track of
  97. // which are pressed
  98. shift_enter_is_pressed = pressed;
  99. // Handle keypress routine
  100. if (pressed) {
  101. // The [left shift/enter] key was pressed
  102. register_code(KC_LSFT);
  103. // Save keydown time for checking hold/tap on keyup
  104. timer_shift_enter = timer_read();
  105. // Pressing a dual function key requires a hold/tap check on key up
  106. dualfunckey_pending = true;
  107. } else {
  108. // The [left shift/enter] key was unpressed
  109. // Note: unregister shift before pressing ENT so that ENT registers
  110. // correctly
  111. // If no other left shift keys are currently pressed, unregister it
  112. if (! shift_lparen_is_pressed) {
  113. unregister_code(KC_LSFT);
  114. }
  115. // If a pending dual function key hasn't been used yet, use it here
  116. if (dualfunckey_pending && dualfunckey_was_tapped(timer_shift_enter)) {
  117. tap_code(KC_ENT);
  118. dualfunckey_pending = false;
  119. }
  120. }
  121. // This keypress was handled
  122. return false;
  123. // -------------------------------------------------------------------------
  124. // Dual function key (hold: left control; tap: escape)
  125. // -------------------------------------------------------------------------
  126. case CTL_ESC:
  127. // Handle keypress routine
  128. if (pressed) {
  129. // Control/escape key was pressed
  130. register_code(KC_LCTL);
  131. // Save keydown time for checking hold/tap on keyup
  132. timer_control_escape = timer_read();
  133. // Pressing a dual function key requires a hold/tap check on key up
  134. dualfunckey_pending = true;
  135. } else {
  136. // Control/escape key was unpressed
  137. unregister_code(KC_LCTL);
  138. // If a pending dual function key hasn't been used yet, use it here
  139. if (dualfunckey_pending && dualfunckey_was_tapped(timer_control_escape)) {
  140. tap_code(KC_ESC);
  141. dualfunckey_pending = false;
  142. }
  143. }
  144. // This keypress was handled
  145. return false;
  146. // -------------------------------------------------------------------------
  147. // Dual function key (hold: left shift; tap: left parenthesis)
  148. // -------------------------------------------------------------------------
  149. case LST_PRN:
  150. // There are multiple keys that are mapped to left shift. Keep track of
  151. // which are pressed
  152. shift_lparen_is_pressed = pressed;
  153. // Handle keypress routine
  154. if (pressed) {
  155. // The [left shift/left parenthesis] key was pressed
  156. register_code(KC_LSFT);
  157. // Save keydown time for checking hold/tap on keyup
  158. timer_shift_lparen = timer_read();
  159. // Pressing a dual function key requires a hold/tap check on key up
  160. dualfunckey_pending = true;
  161. } else {
  162. // The [left shift/left parenthesis] key was unpressed
  163. // Note: unregister shift after tapping 9 so that left parenthesis
  164. // registers correctly
  165. // If a pending dual function key hasn't been used yet, use it here
  166. if (dualfunckey_pending && dualfunckey_was_tapped(timer_shift_lparen)) {
  167. tap_code(KC_9);
  168. dualfunckey_pending = false;
  169. }
  170. // If no other left shift keys are currently pressed, unregister it
  171. if (! shift_enter_is_pressed) {
  172. unregister_code(KC_LSFT);
  173. }
  174. }
  175. // This keypress was handled
  176. return false;
  177. // -------------------------------------------------------------------------
  178. // Dual function key (hold: right shift; tap: right parenthesis)
  179. // -------------------------------------------------------------------------
  180. case RST_PRN:
  181. // Handle keypress routine
  182. if (pressed) {
  183. // The [right shift/right parenthesis] key was pressed
  184. register_code(KC_RSFT);
  185. // Save keydown time for checking hold/tap on keyup
  186. timer_shift_rparen = timer_read();
  187. // Pressing a dual function key requires a hold/tap check on key up
  188. dualfunckey_pending = true;
  189. } else {
  190. // The [right shift/right parenthesis] key was unpressed
  191. // Note: unregister shift after tapping 0 so that right parenthesis
  192. // registers correctly
  193. // If a pending dual function key hasn't been used yet, use it here
  194. if (dualfunckey_pending && dualfunckey_was_tapped(timer_shift_rparen)) {
  195. // Space cadet shift: If the [left shift/left parenthesis] key
  196. // is currently pressed, then tap the sequence ()
  197. if (shift_lparen_is_pressed) {
  198. tap_code(KC_9);
  199. }
  200. tap_code(KC_0);
  201. dualfunckey_pending = false;
  202. }
  203. unregister_code(KC_RSFT);
  204. }
  205. // This keypress was handled
  206. return false;
  207. // -------------------------------------------------------------------------
  208. // Some other key was pressed (this shouldn't happen)
  209. // -------------------------------------------------------------------------
  210. default:
  211. // This keypress was not handled
  212. return true;
  213. }
  214. }