keymap.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // this is the style you want to emulate.
  2. // This is the canonical layout file for the Quantum project. If you want to add another keyboard,
  3. #include QMK_KEYBOARD_H
  4. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  5. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  6. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  7. // entirely and just use numbers.
  8. enum mitosis_layers
  9. {
  10. _MALT,
  11. _SHIFTED,
  12. _FUNCTION,
  13. _FUNCSHIFT
  14. };
  15. enum mitosis_keycodes
  16. {
  17. FNKEY = SAFE_RANGE,
  18. SHIFT
  19. };
  20. // Macro definitions for readability
  21. enum mitosis_macros
  22. {
  23. VOLU,
  24. VOLD,
  25. ESCM
  26. };
  27. #define LONGPRESS_DELAY 150
  28. #define LAYER_TOGGLE_DELAY 300
  29. // Fillers to make layering more clear
  30. #define _______ KC_TRNS
  31. #define XXXXXXX KC_NO
  32. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  33. [_MALT] = LAYOUT( /* Malt Layout, customised for reduced columns (ex: quote and shift locations) */
  34. KC_Q, KC_P, KC_Y, KC_C, KC_B, KC_V, KC_M, KC_U, KC_Z, KC_L,
  35. KC_A, KC_N, KC_I, KC_S, KC_F, KC_D, KC_T, KC_H, KC_O, KC_R,
  36. KC_COMM, KC_DOT, KC_J, KC_G, KC_SLSH, KC_SCLN, KC_W, KC_K, KC_QUOT, KC_X,
  37. M(VOLU), M(ESCM), KC_TAB, KC_LCTL, KC_LALT, KC_ENT, KC_DEL, KC_PGUP,
  38. M(VOLD), KC_LGUI, KC_E, FNKEY, SHIFT, KC_SPC, KC_BSPC, KC_PGDN
  39. ),
  40. [_SHIFTED] = LAYOUT( /* Shifted Layer, layered so that tri_layer can be used, or selectively
  41. able to modify individual key's shifted behaviour */
  42. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  43. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  44. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  45. _______, _______, _______, _______, _______, _______, _______, _______,
  46. _______, _______, _______, _______, _______, _______, _______, _______
  47. ),
  48. [_FUNCTION] = LAYOUT( /* Function Layer, primary alternative layer featuring numpad on right hand,
  49. cursor keys on left hand, and all symbols*/
  50. KC_AMPR, KC_PERC, KC_UP, KC_CIRC, KC_PIPE, KC_LBRC, KC_7, KC_8, KC_9, KC_MINS,
  51. KC_AT, KC_LEFT, KC_DOWN, KC_RGHT, KC_HASH, KC_LPRN, KC_4, KC_5, KC_6, KC_PLUS,
  52. KC_ASTR, KC_UNDS, KC_EXLM, KC_DLR, KC_BSLS, KC_LCBR, KC_1, KC_2, KC_3, KC_ENT,
  53. KC_HOME, KC_GRV, KC_PWR, _______, _______, KC_EQL, KC_TILD, KC_DOT,
  54. KC_END, _______, _______, _______, _______, KC_0, _______, KC_PSCR
  55. ),
  56. [_FUNCSHIFT] = LAYOUT( /* Function Shifted Layer, secondary alternative layer with closing brackets,
  57. and F-keys under their numpad equivalents*/
  58. _______, _______, _______, _______, _______, KC_RBRC, KC_F7, KC_F8, KC_F9, KC_F10,
  59. _______, _______, _______, _______, _______, KC_RPRN, KC_F4, KC_F5, KC_F6, KC_F11,
  60. _______, _______, _______, _______, _______, KC_RCBR, KC_F1, KC_F2, KC_F3, KC_F12,
  61. _______, _______, _______, _______, _______, _______, _______, _______,
  62. _______, _______, _______, _______, _______, _______, _______, _______
  63. )
  64. };
  65. static uint16_t key_timer;
  66. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  67. {
  68. // MACRODOWN only works in this function
  69. switch(id) {
  70. //switch multiplexing for media, short tap for volume up, long press for play/pause
  71. case VOLU:
  72. if (record->event.pressed) {
  73. key_timer = timer_read(); // if the key is being pressed, we start the timer.
  74. } else { // this means the key was just released, so we can figure out how long it was pressed for (tap or "held down").
  75. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) { // LONGPRESS_DELAY being 150ms, the threshhold we pick for counting something as a tap.
  76. return MACRO(T(MPLY), END);
  77. } else {
  78. return MACRO(T(VOLU), END);
  79. }
  80. }
  81. break;
  82. //switch multiplexing for media, short tap for volume down, long press for next track
  83. case VOLD:
  84. if (record->event.pressed) {
  85. key_timer = timer_read();
  86. } else {
  87. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
  88. return MACRO(T(MNXT), END);
  89. } else {
  90. return MACRO(T(VOLD), END);
  91. }
  92. }
  93. break;
  94. //switch multiplexing for escape, short tap for escape, long press for context menu
  95. case ESCM:
  96. if (record->event.pressed) {
  97. key_timer = timer_read();
  98. } else {
  99. if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
  100. return MACRO(T(APP), END);
  101. } else {
  102. return MACRO(T(ESC), END);
  103. }
  104. }
  105. break;
  106. break;
  107. }
  108. return MACRO_NONE;
  109. };
  110. static bool singular_key = false;
  111. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  112. uint8_t layer;
  113. layer = biton32(layer_state); // get the current layer
  114. //custom layer handling for tri_layer,
  115. switch (keycode) {
  116. case FNKEY:
  117. if (record->event.pressed) {
  118. key_timer = timer_read();
  119. singular_key = true;
  120. layer_on(_FUNCTION);
  121. } else {
  122. if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
  123. layer_off(_FUNCTION);
  124. }
  125. }
  126. update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
  127. return false;
  128. break;
  129. //SHIFT is handled as LSHIFT in the general case
  130. case SHIFT:
  131. if (record->event.pressed) {
  132. key_timer = timer_read();
  133. singular_key = true;
  134. layer_on(_SHIFTED);
  135. register_code(KC_LSFT);
  136. } else {
  137. if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
  138. layer_off(_SHIFTED);
  139. unregister_code(KC_LSFT);
  140. }
  141. }
  142. update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
  143. return false;
  144. break;
  145. //If any other key was pressed during the layer mod hold period,
  146. //then the layer mod was used momentarily, and should block latching
  147. default:
  148. singular_key = false;
  149. break;
  150. }
  151. //FUNCSHIFT has been shifted by the SHIFT handling, some keys need to be excluded
  152. if (layer == _FUNCSHIFT) {
  153. //F1-F12 should be sent as unshifted keycodes,
  154. //and ] needs to be unshifted or it is sent as }
  155. if ( (keycode >= KC_F1 && keycode <= KC_F12)
  156. || keycode == KC_RBRC ) {
  157. if (record->event.pressed) {
  158. unregister_mods(MOD_LSFT);
  159. } else {
  160. register_mods(MOD_LSFT);
  161. }
  162. }
  163. }
  164. return true;
  165. };
  166. void matrix_scan_user(void) {
  167. uint8_t layer = biton32(layer_state);
  168. switch (layer) {
  169. case _MALT:
  170. set_led_off;
  171. break;
  172. case _FUNCTION:
  173. set_led_blue;
  174. break;
  175. case _SHIFTED:
  176. set_led_red;
  177. break;
  178. case _FUNCSHIFT:
  179. set_led_green;
  180. break;
  181. default:
  182. break;
  183. }
  184. };