keymap.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // An Ergodox EZ keymap mostly following the programmer's dvorak layout.
  2. // There is a standard QWERTY layer as well
  3. //
  4. // See the README.md file for an image of this keymap.
  5. #include QMK_KEYBOARD_H
  6. // The layers that we are defining for this keyboards.
  7. #define BASE 0
  8. #define NUM 1
  9. #define QWERTY 2
  10. #define MAC 3
  11. // The Tap Dance identifiers, used in the TD keycode and tap_dance_actions array.
  12. #define TAP_MACRO 0
  13. // SAFE_RANGE must be used to tag the first element of the enum.
  14. // DYNAMIC_MACRO_RANGE must always be the last element of the enum if other
  15. // values are added (as its value is used to create a couple of other keycodes
  16. // after it).
  17. enum custom_keycodes {
  18. MC_ARROW = SAFE_RANGE,
  19. DYNAMIC_MACRO_RANGE
  20. };
  21. // A 'transparent' key code (that falls back to the layers below it).
  22. #define ___ KC_TRANSPARENT
  23. // A 'blocking' key code. Does nothing but prevent falling back to another layer.
  24. #define XXX KC_NO
  25. // Some combined keys (one normal keycode when tapped and one modifier or layer
  26. // toggle when held).
  27. #define SPC_RALT MT(MOD_RALT, KC_SPC) // SPACE key and right alt modifier.
  28. // The most portable copy/paste keys (windows (mostly), linux, and some terminal emulators).
  29. #define MK_CUT LSFT(KC_DEL) // shift + delete
  30. #define MK_COPY LCTL(KC_INS) // ctrl + insert
  31. #define MK_PASTE LSFT(KC_INS) // shift + insert
  32. //Move mac desktop spaces
  33. #define MAC_L LGUI(LSFT(KC_UP)) // cmd + shift + up
  34. #define MAC_R LGUI(LSFT(KC_DOWN)) // cmd + shift + down
  35. // This file must be included after DYNAMIC_MACRO_RANGE is defined...
  36. #include "dynamic_macro.h"
  37. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  38. // Layer 0: basic keys.
  39. [BASE] = LAYOUT_ergodox_pretty(
  40. KC_DLR, KC_AMPR, KC_LBRC, KC_LCBR, KC_RCBR, KC_LPRN, KC_CIRC, KC_F4, KC_EQUAL,KC_ASTR, KC_BSLS, KC_PLUS, KC_RBRC, KC_EXLM,
  41. GUI_T(KC_BSLS), KC_SCLN, KC_COMMA, KC_DOT, KC_P, KC_Y, KC_PERC, KC_DELETE, KC_F, KC_G, KC_C, KC_R, KC_L, KC_SLASH,
  42. MO(NUM), KC_A, KC_O, KC_E, KC_U, KC_I, KC_D, KC_H, KC_T, KC_N, KC_S, KC_MINUS,
  43. SC_LSPO, KC_QUOTE, KC_Q, KC_J, KC_K, KC_X, KC_LALT, TD(TAP_MACRO),KC_B, KC_M, KC_W, KC_V, KC_Z, SC_RSPC,
  44. KC_AT, KC_HASH, KC_GRAVE, KC_LEFT, KC_RIGHT, KC_UP, KC_DOWN, KC_HOME, KC_END, TO(QWERTY),
  45. ALT_T(KC_APPLICATION), ALL_T(KC_NO), KC_PGUP, KC_LGUI,
  46. KC_BSLS, TO(MAC),
  47. KC_ENTER, KC_TAB, CTL_T(KC_ESCAPE), CTL_T(KC_ESCAPE), KC_BSPC, KC_SPACE),
  48. // Layer 1: function and numpad keys.
  49. [NUM] = LAYOUT_ergodox_pretty(
  50. ___, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, ___, ___, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11,
  51. ___, KC_EXLM, KC_COMMA, KC_DOT, KC_MS_BTN1, KC_MS_BTN2, ___, ___, KC_UP, KC_7, KC_8, KC_9, KC_ASTR, KC_F12,
  52. XXX, ___, KC_LCBR, KC_RCBR, MC_ARROW, KC_GRAVE, KC_DOWN, KC_4, KC_5, KC_6, KC_PLUS, KC_UNDS,
  53. ___, ___, KC_CIRC, KC_LBRC, KC_RBRC, KC_TILD, ___, ___, KC_0, KC_1, KC_2, KC_3, KC_KP_SLASH, KC_BSLS,
  54. ___, ___, ___, ___, ___, ___, KC_0, KC_KP_DOT, KC_EQUAL, ___,
  55. ___, ___, KC_KP_ASTERISK, KC_KP_SLASH,
  56. KC_LCTL, ___,
  57. KC_KP_PLUS, KC_KP_MINUS, KC_DLR, KC_DELETE, KC_BSPC, KC_SPACE),
  58. // Layer 2: QWERTY control.
  59. [QWERTY] = LAYOUT_ergodox_pretty(
  60. /* left hand */
  61. KC_EQUAL, KC_1, KC_2, KC_3, KC_4, KC_5, KC_LEFT, KC_RIGHT, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINUS,
  62. KC_DELETE, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, TO(BASE), KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSLS,
  63. MO(NUM), KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, GUI_T(KC_QUOT),
  64. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, ALT_T(KC_NO), MEH_T(KC_NO), KC_N, KC_M, KC_COMMA, KC_DOT, KC_SLASH, SC_RSPC,
  65. KC_GRAVE, KC_QUOTE, LALT(KC_TAB), KC_LEFT, KC_RIGHT, KC_UP, KC_DOWN, KC_LBRC, KC_RBRC, TO(BASE),
  66. ___, KC_LGUI, ___, KC_ESCAPE,
  67. KC_HOME, KC_PGUP,
  68. KC_ENTER, KC_TAB, KC_END, KC_PGDN, KC_BSPC, KC_SPACE),
  69. // Layer 3: Mac layer
  70. [MAC] = LAYOUT_ergodox_pretty(
  71. /* left hand */
  72. ___, KC_1, KC_2, KC_3, KC_4, KC_5, ___, ___, KC_6, KC_7, KC_8, KC_9, KC_0, ___,
  73. ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
  74. ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
  75. ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
  76. ___, ___, ___, MAC_L, MAC_R, ___, ___, ___, ___, ___,
  77. ___, ___, ___, ___,
  78. ___, TO(BASE),
  79. KC_LGUI, KC_LALT, KC_LGUI, KC_LGUI, KC_LALT, KC_LGUI),
  80. };
  81. // Whether the macro 1 is currently being recorded.
  82. static bool is_macro1_recording = false;
  83. // The current set of active layers (as a bitmask).
  84. // There is a global 'layer_state' variable but it is set after the call
  85. // to layer_state_set_user().
  86. static layer_state_t current_layer_state = 0;
  87. layer_state_t layer_state_set_user(layer_state_t state);
  88. // Method called at the end of the tap dance on the TAP_MACRO key. That key is
  89. // used to start recording a macro (double tap or more), to stop recording (any
  90. // number of tap), or to play the recorded macro (1 tap).
  91. void macro_tapdance_fn(qk_tap_dance_state_t *state, void *user_data) {
  92. uint16_t keycode;
  93. keyrecord_t record;
  94. dprintf("macro_tap_dance_fn %d\n", state->count);
  95. if (is_macro1_recording) {
  96. keycode = DM_RSTP;
  97. is_macro1_recording = false;
  98. layer_state_set_user(current_layer_state);
  99. } else if (state->count == 1) {
  100. keycode = DM_PLY1;
  101. } else {
  102. keycode = DM_REC1;
  103. is_macro1_recording = true;
  104. layer_state_set_user(current_layer_state);
  105. }
  106. record.event.pressed = true;
  107. process_record_dynamic_macro(keycode, &record);
  108. record.event.pressed = false;
  109. process_record_dynamic_macro(keycode, &record);
  110. }
  111. // The definition of the tap dance actions:
  112. qk_tap_dance_action_t tap_dance_actions[] = {
  113. // This Tap dance plays the macro 1 on TAP and records it on double tap.
  114. [TAP_MACRO] = ACTION_TAP_DANCE_FN(macro_tapdance_fn)
  115. };
  116. // Runs for each key down or up event.
  117. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  118. if (keycode != TD(TAP_MACRO)) {
  119. // That key is processed by the macro_tapdance_fn. Not ignoring it here is
  120. // mostly a no-op except that it is recorded in the macros (and uses space).
  121. // We can't just return false when the key is a tap dance, because
  122. // process_record_user, is called before the tap dance processing (and
  123. // returning false would eat the tap dance).
  124. if (!process_record_dynamic_macro(keycode, record)) {
  125. return false;
  126. }
  127. if(record->event.pressed) {
  128. switch(keycode) {
  129. case MC_ARROW:
  130. SEND_STRING("=>");
  131. return false;
  132. break;
  133. }
  134. }
  135. }
  136. return true; // Let QMK send the enter press/release events
  137. }
  138. // Runs just one time when the keyboard initializes.
  139. void matrix_init_user(void) {
  140. ergodox_right_led_1_off();
  141. ergodox_right_led_2_off();
  142. ergodox_right_led_3_off();
  143. };
  144. // Runs constantly in the background, in a loop.
  145. void matrix_scan_user(void) {
  146. };
  147. // Value to use to switch LEDs on. The default value of 255 is far too bright.
  148. static const uint8_t max_led_value = 20;
  149. // Whether the given layer (one of the constant defined at the top) is active.
  150. #define LAYER_ON(layer) (current_layer_state & (1<<layer))
  151. void led_1_on(void) {
  152. ergodox_right_led_1_on();
  153. ergodox_right_led_1_set(max_led_value);
  154. }
  155. void led_2_on(void) {
  156. ergodox_right_led_2_on();
  157. ergodox_right_led_2_set(max_led_value);
  158. }
  159. void led_3_on(void) {
  160. ergodox_right_led_3_on();
  161. ergodox_right_led_3_set(max_led_value);
  162. }
  163. void led_1_off(void) {
  164. ergodox_right_led_1_off();
  165. }
  166. void led_2_off(void) {
  167. ergodox_right_led_2_off();
  168. }
  169. void led_3_off(void) {
  170. ergodox_right_led_3_off();
  171. }
  172. layer_state_t layer_state_set_user(layer_state_t state) {
  173. current_layer_state = state;
  174. if (is_macro1_recording) {
  175. led_1_on();
  176. led_2_on();
  177. led_3_on();
  178. return state;
  179. }
  180. if (LAYER_ON(NUM)) {
  181. led_1_on();
  182. } else {
  183. led_1_off();
  184. }
  185. if (LAYER_ON(MAC)) {
  186. led_2_on();
  187. } else {
  188. led_2_off();
  189. }
  190. if (LAYER_ON(QWERTY)) {
  191. led_3_on();
  192. } else {
  193. led_3_off();
  194. }
  195. return state;
  196. };