keymap.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include QMK_KEYBOARD_H
  2. #define SHIFT_DOWN() (get_mods() & (MOD_BIT(KC_LSFT)|MOD_BIT(KC_RSFT)))
  3. #define KC_FKEYS MO(FKEYS)
  4. // Layers
  5. enum {
  6. BASE=0,
  7. SYMNUM, // Symbols and numbers (activated by holding enter)
  8. FKEYS, // Function keys (activated by a thumb button)
  9. };
  10. // counter that increments on all key press events. This is needed for our LT
  11. // handling
  12. uint16_t press_counter = 0;
  13. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  14. // Keymap 0: Base layer
  15. [BASE] = LAYOUT_ergodox(
  16. // left hand ---------------------------------------------------------------------------------------------------
  17. KC_NO, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_NO,
  18. KC_GRV, KC_QUOT, KC_COMM, KC_DOT, KC_P, KC_Y, KC_LGUI,
  19. KC_SLSH, KC_A, KC_O, KC_E, KC_U, KC_I,
  20. KC_LSFT, KC_SCLN, KC_Q, KC_J, KC_K, KC_X, KC_HOME,
  21. KC_LCTL, KC_LALT, KC_PGUP, KC_PGDN, KC_NO,
  22. // left thumb --------------------------------------------------------------------------------------------------
  23. KC_NO, KC_NO,
  24. KC_NO,
  25. KC_SPC, KC_TAB, KC_FKEYS,
  26. // right hand --------------------------------------------------------------------------------------------------
  27. KC_NO, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_NO,
  28. KC_RGUI, KC_F, KC_G, KC_C, KC_R, KC_L, KC_BSLS,
  29. KC_D, KC_H, KC_T, KC_N, KC_S, KC_MINS,
  30. KC_END, KC_B, KC_M, KC_W, KC_V, KC_Z, KC_RSFT,
  31. KC_LEFT, KC_UP, KC_DOWN, KC_RIGHT, KC_RCTL,
  32. // right thumb -------------------------------------------------------------------------------------------------
  33. KC_NO, KC_NO,
  34. KC_NO,
  35. KC_DEL, KC_BSPC, KC_ENT
  36. ),
  37. // SYMNUM -- numpad and function keys
  38. [SYMNUM] = LAYOUT_ergodox(
  39. // left hand ---------------------------------------------------------------------------------------------------
  40. KC_TRNS, LALT(KC_1), LALT(KC_2), LALT(KC_3), LALT(KC_4), LALT(KC_5), KC_TRNS,
  41. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_AMPR, KC_TRNS, KC_TRNS,
  42. KC_TRNS, KC_LCBR, KC_LBRC, KC_LPRN, KC_0, KC_MINS,
  43. KC_TRNS, KC_6, KC_7, KC_8, KC_9, KC_SLSH, KC_TRNS,
  44. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  45. // left thumb --------------------------------------------------------------------------------------------------
  46. KC_TRNS, KC_TRNS,
  47. KC_TRNS,
  48. KC_TRNS, KC_TRNS, KC_TRNS,
  49. // right hand --------------------------------------------------------------------------------------------------
  50. KC_TRNS, LALT(KC_6), LALT(KC_7), LALT(KC_8), LALT(KC_9), LALT(KC_0), KC_TRNS,
  51. KC_TRNS, KC_PERC, KC_EQL, KC_LT, KC_GT, KC_EXLM, KC_TRNS,
  52. KC_PLUS, KC_1, KC_RPRN, KC_RBRC, KC_RCBR, KC_TRNS,
  53. KC_TRNS, KC_ASTR, KC_2, KC_3, KC_4, KC_5, KC_TRNS,
  54. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  55. // right thumb -------------------------------------------------------------------------------------------------
  56. KC_TRNS, KC_TRNS,
  57. KC_TRNS,
  58. KC_TRNS, KC_TRNS, KC_TRNS
  59. ),
  60. // FKEYS -- function keys
  61. [FKEYS] = LAYOUT_ergodox(
  62. // left hand ---------------------------------------------------------------------------------------------------
  63. KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6,
  64. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  65. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  66. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  67. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  68. // left thumb --------------------------------------------------------------------------------------------------
  69. KC_TRNS, KC_TRNS,
  70. KC_TRNS,
  71. KC_TRNS, KC_TRNS, KC_TRNS,
  72. // right hand --------------------------------------------------------------------------------------------------
  73. KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS,
  74. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  75. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  76. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  77. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  78. // right thumb -------------------------------------------------------------------------------------------------
  79. KC_TRNS, KC_TRNS,
  80. KC_TRNS,
  81. KC_TRNS, KC_TRNS, KC_TRNS
  82. ),
  83. };
  84. // Code to handle layer tap.
  85. //
  86. // The default code doesn't work for me, so I rolled my own code. The main
  87. // difference is that it always tries to send modifier+key combination, rather
  88. // than two keys. One place where this is noticable is if you tap the key,
  89. // then try hold it.
  90. struct tap_start_info {
  91. uint16_t timer;
  92. uint16_t initial_press_counter;
  93. };
  94. static struct tap_start_info tap_start_infos[5];
  95. // Quick check to see if a key is down. Note that this requires NKRO enabled
  96. static bool key_down(uint8_t code) {
  97. return (keyboard_report->nkro.bits[code>>3] & 1<<(code&7)) != 0;
  98. }
  99. static bool handle_lt(uint16_t keycode, keyrecord_t *record, uint8_t layer, uint8_t index) {
  100. struct tap_start_info* tap_start = tap_start_infos + index;
  101. if(record->event.pressed) {
  102. layer_on(layer);
  103. tap_start->timer = timer_read();
  104. tap_start->initial_press_counter = press_counter;
  105. } else {
  106. layer_off(layer);
  107. // Only click the key if:
  108. // - No other keys are pressed between the press and release events
  109. // - The release happens before the ONESHOT_TIMEOUT
  110. if(press_counter == tap_start->initial_press_counter && timer_elapsed(tap_start->timer) < ONESHOT_TIMEOUT) {
  111. register_code16(keycode);
  112. unregister_code16(keycode);
  113. }
  114. }
  115. return false;
  116. }
  117. static bool handle_mt(uint16_t keycode, keyrecord_t *record, uint8_t mods, uint8_t index) {
  118. struct tap_start_info* tap_start = tap_start_infos + index;
  119. if(record->event.pressed) {
  120. register_mods(mods);
  121. tap_start->timer = timer_read();
  122. tap_start->initial_press_counter = press_counter;
  123. } else {
  124. unregister_mods(mods);
  125. // Only click the key if:
  126. // - No other keys are pressed between the press and release events
  127. // - The release happens before the ONESHOT_TIMEOUT
  128. if(press_counter == tap_start->initial_press_counter && timer_elapsed(tap_start->timer) < ONESHOT_TIMEOUT) {
  129. register_code16(keycode);
  130. unregister_code16(keycode);
  131. }
  132. }
  133. return false;
  134. }
  135. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  136. if(record->event.pressed) {
  137. press_counter++;
  138. // The symbol layer allows sequences repeat the same "physical" key.
  139. // For example "0)", which is like pressing the "0" key twice in a
  140. // row. It's very easy to make a sequence of these where the ")"
  141. // press comes before the "0" release. If that happens, force a
  142. // release of the "0" key, otherwise nothing will happen.
  143. uint8_t shortcode = keycode & 0xFF;
  144. if(key_down(shortcode)) {
  145. unregister_code(shortcode);
  146. }
  147. }
  148. switch(keycode) {
  149. case KC_ENTER:
  150. return handle_lt(keycode, record, SYMNUM, 0);
  151. case KC_TAB:
  152. return handle_mt(keycode, record, MOD_BIT(KC_LCTL), 1);
  153. case KC_LSFT:
  154. case KC_RSFT:
  155. return handle_mt(KC_ESC, record, MOD_BIT(keycode), 2);
  156. }
  157. return true;
  158. }