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,
6 // Each layer gets a name for readability, which is then used in the keymap matrix below.
7 // The underscores don't mean anything - you can have a layer called STUFF or any other name.
8 // Layer names don't all need to be of the same length, obviously, and you can also skip them
9 // entirely and just use numbers.
10 enum dichotomy_layers {
18 #define LONGPRESS_COUNT 4
20 enum dichotomy_keycodes
25 CK_TE, //these 4 CK_XXXX keys are special "alternate long-press" keys controlled with unique timers. Make sure you understand them before you mess with them.
36 #define CUSTOM_LONGPRESS 150
37 #define CUSTOM_TOGGLE_TIME 300
39 #define RED_BRIGHTNESS 3
40 #define GREEN_BRIGHTNESS 2
41 #define BLUE_BRIGHTNESS 2
43 // Fillers to make layering more clear
44 #define _______ KC_TRNS
47 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
48 [_BS] = LAYOUT( /* Base layout, nearly qwerty but with modifications because it's not a full keyboard. Obviously. */
49 CK_TE, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,
50 NUMKEY, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, CK_QE,
51 SFTKEY, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, MOUKEY,
52 KC_LCTL, KC_LALT, KC_LGUI, KC_RGUI, KC_RALT, KC_RCTL,
53 MS_BTN3, KC_LBRC, KC_LPRN, KC_SPC, KC_SPC, KC_RPRN, KC_RBRC, MS_BTN3
56 [_SF] = LAYOUT( /* Shifted layout, small changes (because angle brackets have been moved to thumb cluster buttons) */
57 _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
58 _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
59 _______, _______, _______, _______, _______, _______, _______, _______, NS_HYPH, KC_UNDS, _______, _______,
60 _______, _______, _______, _______, _______, _______,
61 _______, _______, KC_LABK, _______, _______, KC_RABK, _______, _______
64 [_NM] = LAYOUT( /* Number layout, basically the main function layer */
65 _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, _______,
66 _______, CK_1G, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, CK_BSPE,
67 _______, KC_F11, KC_F12, KC_F13, KC_F14, KC_F15, KC_F16, KC_F17, KC_F18, KC_F19, KC_F20, _______,
68 _______, _______, _______, _______, _______, _______,
69 _______, _______, _______, _______, _______, _______, _______, _______
72 [_NS] = LAYOUT( /* Shifted number/function layout, for per-key control. Only active when shift is held, and number is toggled or held */
73 _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
74 _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_PLUS, NS_EQU, _______,
75 _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
76 _______, _______, _______, _______, _______, _______,
77 _______, _______, _______, _______, _______, _______, _______, _______
80 [_MS] = LAYOUT( /* Mouse layer, including buttons for clicking. */
81 _______, _______, _______, _______, _______, _______, KC_VOLU, KC_HOME, KC_PGUP, _______, _______, _______,
82 _______, _______, _______, _______, _______, _______, _______, MS_BTN1, MS_BTN2, _______, _______, _______,
83 _______, _______, _______, _______, _______, _______, KC_VOLD, KC_END, KC_PGDN, _______, _______, _______,
84 _______, _______, _______, _______, KC_UP, _______,
85 _______, _______, _______, _______, KC_LEFT, KC_DOWN, KC_RGHT, _______
90 static uint16_t special_timers[LONGPRESS_COUNT] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
91 static bool special_key_states[LONGPRESS_COUNT] = {0,0,0,0};
92 static bool special_key_pressed[LONGPRESS_COUNT] = {0,0,0,0};
94 static uint16_t shift_timer;
95 static uint16_t num_timer;
96 static uint16_t mouse_timer;
98 static uint8_t red_timer;
99 static uint8_t green_timer;
100 static uint8_t blue_timer;
102 static bool shift_singular_key = false;
103 static bool number_singular_key = false;
104 static bool mouse_singular_key = false;
105 static bool capsLED = false;
106 static bool shiftLED = false;
107 static bool numLED = false;
108 static bool mouseLED = false;
110 static bool shift_held = false;
111 static bool shift_suspended = false;
112 report_mouse_t currentReport = {};
114 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
116 //layer = biton32(layer_state); // get the current layer //Or don't, I didn't use it.
117 bool returnVal = true; //this is to determine if more key processing is needed.
119 //custom layer handling for tri_layer,
122 if (record->event.pressed) {
123 num_timer = timer_read();
124 number_singular_key = true;
128 if (timer_elapsed(num_timer) < CUSTOM_TOGGLE_TIME && number_singular_key) {
129 //do nothing, the layer has already been inverted
135 update_tri_layer(_NM, _SF, _NS);
138 //SHIFT is handled as LSHIFT in the general case - 'toggle' shoudl activate caps, while the layer is only active when shift is held.
140 if (record->event.pressed) {
143 shift_suspended = false;
144 shift_timer = timer_read();
145 shift_singular_key = true;
147 register_code(KC_LSFT);
151 if (timer_elapsed(shift_timer) < CUSTOM_TOGGLE_TIME && shift_singular_key) {
152 //this was basically a toggle, so activate/deactivate caps lock.
153 SEND_STRING(SS_TAP(X_CAPSLOCK));
157 unregister_code(KC_LSFT);
159 update_tri_layer(_NM, _SF, _NS);
162 //MOUSE layer needs to be handled the same way as NUMKEY, but differently from shift
164 if (record->event.pressed) {
165 mouse_timer = timer_read();
166 mouse_singular_key = true;
168 mouseLED = !mouseLED;
170 if (timer_elapsed(mouse_timer) < CUSTOM_TOGGLE_TIME && mouse_singular_key){
171 //do nothing, it was a toggle (and it's already been toggled)
174 mouseLED = !mouseLED;
179 //Custom macros for strange keys with different long-tap behavior
181 if (shift_held && shift_suspended){
182 register_code(KC_LSFT);
183 shift_suspended = false;
185 if (record->event.pressed) {
186 special_timers[CK_1G-SAFE_RANGE] = timer_read();
187 special_key_pressed[CK_1G-SAFE_RANGE] = 1;
189 if (special_key_states[CK_1G-SAFE_RANGE]){
190 //key was activated after custom_longpress, need to close those keycodes
191 special_key_states[CK_1G-SAFE_RANGE] = 0;
192 unregister_code(KC_GRAVE);
194 if (special_key_pressed[CK_1G-SAFE_RANGE]){
195 //key was not activated, return macro activating proper, pre-long-tap key
196 SEND_STRING(SS_TAP(X_1));
197 special_key_pressed[CK_1G-SAFE_RANGE] = 0;
199 //the short key was already sent, because another key was pressed.
208 if (shift_held && shift_suspended){
209 register_code(KC_LSFT);
210 shift_suspended = false;
212 if (record->event.pressed) {
213 special_timers[CK_BSPE-SAFE_RANGE] = timer_read();
214 special_key_pressed[CK_BSPE-SAFE_RANGE] = 1;
216 if (special_key_states[CK_BSPE-SAFE_RANGE]){
217 //key was activated after custom_longpress, need to close those keycodes
218 special_key_states[CK_BSPE-SAFE_RANGE] = 0;
219 unregister_code(KC_ENTER);
221 if (special_key_pressed[CK_BSPE-SAFE_RANGE]){
222 //key was not activated, return macro activating proper, pre-long-tap key
223 SEND_STRING(SS_TAP(X_BSLASH));
224 special_key_pressed[CK_BSPE-SAFE_RANGE] = 0;
226 //the short key was already sent, because another key was pressed.
234 if (shift_held && shift_suspended){
235 register_code(KC_LSFT);
236 shift_suspended = false;
238 if (record->event.pressed) {
239 special_timers[CK_QE-SAFE_RANGE] = timer_read();
240 special_key_pressed[CK_QE-SAFE_RANGE] = 1;
242 if (special_key_states[CK_QE-SAFE_RANGE]){
243 //key was activated after custom_longpress, need to close those keycodes
244 special_key_states[CK_QE-SAFE_RANGE] = 0;
245 unregister_code(KC_ENTER);
247 if (special_key_pressed[CK_QE-SAFE_RANGE]){
248 //the long-press key was not activated, return macro activating proper, pre-long-tap key
249 SEND_STRING(SS_TAP(X_QUOTE));
250 special_key_pressed[CK_QE-SAFE_RANGE] = 0;
252 //the short key was already sent, because another key was pressed.
260 if (shift_held && shift_suspended){
261 register_code(KC_LSFT);
262 shift_suspended = false;
264 if (record->event.pressed) {
265 special_timers[CK_TE-SAFE_RANGE] = timer_read();
266 special_key_pressed[CK_TE-SAFE_RANGE] = 1;
268 if (special_key_states[CK_TE-SAFE_RANGE]){
269 //key was activated after custom_longpress, need to close those keycodes
270 special_key_states[CK_TE-SAFE_RANGE] = 0;
271 unregister_code(KC_ESCAPE);
273 if (special_key_pressed[CK_TE-SAFE_RANGE]){
274 //the long-press key was not activated, return macro activating proper, pre-long-tap key
275 SEND_STRING(SS_TAP(X_TAB));
276 special_key_pressed[CK_TE-SAFE_RANGE] = 0;
278 //the short key was already sent, because another key was pressed.
285 //No-shift keys, they unregister the KC_LSFT code so they can send
286 //unshifted values - but they don't change the bool. if any other
287 //key is pressed and the bool is set, KC_LSFT is registered again.
289 if (record->event.pressed) {
290 shift_suspended = true;
291 unregister_code(KC_LSFT);
292 register_code(KC_MINS);
294 unregister_code(KC_MINS);
295 if (shift_held && shift_suspended){
296 register_code(KC_LSFT);
297 shift_suspended = false;
303 if (record->event.pressed) {
304 shift_suspended = true;
305 unregister_code(KC_LSFT);
306 register_code(KC_EQUAL);
308 unregister_code(KC_EQUAL);
309 if (shift_held && shift_suspended){
310 register_code(KC_LSFT);
311 shift_suspended = false;
317 //mouse buttons, for 1-3, to update the mouse report:
319 currentReport = pointing_device_get_report();
320 if (record->event.pressed) {
321 if (shift_held && shift_suspended){
322 register_code(KC_LSFT);
323 shift_suspended = false;
325 //update mouse report here
326 currentReport.buttons |= MOUSE_BTN1; //MOUSE_BTN1 is a const defined in report.h
328 //update mouse report here
329 currentReport.buttons &= ~MOUSE_BTN1;
331 pointing_device_set_report(currentReport);
335 currentReport = pointing_device_get_report();
336 if (record->event.pressed) {
337 if (shift_held && shift_suspended){
338 register_code(KC_LSFT);
339 shift_suspended = false;
341 //update mouse report here
342 currentReport.buttons |= MOUSE_BTN2; //MOUSE_BTN2 is a const defined in report.h
344 //update mouse report here
345 currentReport.buttons &= ~MOUSE_BTN2;
347 pointing_device_set_report(currentReport);
351 currentReport = pointing_device_get_report();
352 if (record->event.pressed) {
353 if (shift_held && shift_suspended){
354 register_code(KC_LSFT);
355 shift_suspended = false;
357 //update mouse report here
358 currentReport.buttons |= MOUSE_BTN3; //MOUSE_BTN3 is a const defined in report.h
360 //update mouse report here
361 currentReport.buttons &= ~MOUSE_BTN3;
363 pointing_device_set_report(currentReport);
366 //Additionally, if NS_ keys are in use, then shift may be held (but is
367 //disabled for the unshifted keycodes to be send. Check the bool and
368 //register shift as necessary.
371 register_code(KC_LSFT);
381 //don't want to reset single key variables
384 //If any other key was pressed during the layer mod hold period,
385 //then the layer mod was used momentarily, and should block latching
386 shift_singular_key = false;
387 number_singular_key = false;
388 mouse_singular_key = false;
410 //Do nothing, don't want to trigger the timer key rollover
413 //Now we're checking to see if any of the special timer keys are pressed
414 //if so, we need to activate their short-press features
415 if (record->event.pressed) {
416 for (uint8_t i = 0; i<LONGPRESS_COUNT; i++){
417 if ((!special_key_states[i]) && special_key_pressed[i]){
418 switch (i + SAFE_RANGE){
420 SEND_STRING(SS_TAP(X_1));
423 SEND_STRING(SS_TAP(X_BSLASH));
426 SEND_STRING(SS_TAP(X_QUOTE));
429 SEND_STRING(SS_TAP(X_TAB));
432 special_key_pressed[i] = 0;
436 //do nothing, we don't want to trigger short presses on key releases.
443 void matrix_scan_user(void) {
444 //uint8_t layer = biton32(layer_state);
445 for (uint8_t i = 0; i<LONGPRESS_COUNT; i++){
446 if ((timer_elapsed(special_timers[i]) >= CUSTOM_LONGPRESS) && (!special_key_states[i]) && special_key_pressed[i]){
447 switch (i + SAFE_RANGE){
449 register_code(KC_GRAVE);
452 register_code(KC_ENTER);
455 register_code(KC_ENTER);
458 register_code(KC_ESCAPE);
461 special_key_pressed[i] = 0;
462 special_key_states[i] = 1;
465 if (shiftLED || capsLED){
467 if (red_timer < RED_BRIGHTNESS){
479 if (green_timer < GREEN_BRIGHTNESS){
491 if (blue_timer < BLUE_BRIGHTNESS){