Skip to content

Instantly share code, notes, and snippets.

@nixpulvis nixpulvis/keymap.c
Created Jan 31, 2019

Embed
What would you like to do?
#include QMK_KEYBOARD_H
enum mitosis_layers
{
_QWERTY,
_SHIFTED,
_FUNCTION,
_FUNCSHIFT,
};
enum mitosis_keycodes
{
FNKEY = SAFE_RANGE,
SHIFT,
DYNAMIC_MACRO_RANGE,
};
// Macro definitions for readability
enum mitosis_macros
{
VOLU,
VOLD,
ESCM
};
#include "dynamic_macro.h"
#define LONGPRESS_DELAY 150
#define LAYER_TOGGLE_DELAY 900
// Fillers to make layering more clear
#define _______ KC_TRNS
#define __MOD__ KC_TRNS
#define XXXXXXX KC_NO
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_QWERTY] = LAYOUT( /* QWERTY adapted to this crazy thing */
KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P,
KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN,
KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH,
/* TODO */
KC_LCTL, M(ESCM), KC_TAB, KC_QUOT, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT,
KC_LALT, KC_LGUI, KC_SPC, SHIFT, FNKEY, KC_BSPC, KC_ENT, _______
),
[_SHIFTED] = LAYOUT( /* Shifted Layer, layered so that tri_layer can be used, or selectively
able to modify individual key's shifted behaviour */
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
__MOD__, KC_DEL, _______, _______, _______, _______, _______, _______,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, XXXXXXX
),
[_FUNCTION] = LAYOUT( /* Function Layer mimicks planck's raise layer somewhat */
KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,
LCTL(KC_A), LCTL(KC_S), _______, LCTL(KC_F),_______, _______, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC,
LCTL(KC_Z), LCTL(KC_X), LCTL(KC_C), LCTL(KC_V),_______, _______, KC_BSLS, _______, _______, KC_QUOT,
__MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, XXXXXXX
),
[_FUNCSHIFT] = LAYOUT( /* Function Shifted Layer mimicks planck's lower layer somewhat */
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, _______, _______, KC_LCBR, KC_RCBR,
KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______, _______, _______,
__MOD__, KC_DEL, _______, KC_TILD, _______, _______, _______, _______,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, _______
),
};
const uint16_t PROGMEM fn_actions[] = {};
static uint16_t key_timer;
const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
// MACRODOWN only works in this function
switch(id) {
//switch multiplexing for media, short tap for volume up, long press for play/pause
case VOLU:
if (record->event.pressed) {
key_timer = timer_read(); // if the key is being pressed, we start the timer.
} else { // this means the key was just released, so we can figure out how long it was pressed for (tap or "held down").
if (timer_elapsed(key_timer) > LONGPRESS_DELAY) { // LONGPRESS_DELAY being 150ms, the threshhold we pick for counting something as a tap.
return MACRO(T(MPLY), END);
} else {
return MACRO(T(VOLU), END);
}
}
break;
//switch multiplexing for media, short tap for volume down, long press for next track
case VOLD:
if (record->event.pressed) {
key_timer = timer_read();
} else {
if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
return MACRO(T(MNXT), END);
} else {
return MACRO(T(VOLD), END);
}
}
break;
//switch multiplexing for escape, short tap for escape, long press for context menu
case ESCM:
if (record->event.pressed) {
key_timer = timer_read();
} else {
if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
return MACRO(T(APP), END);
} else {
return MACRO(T(ESC), END);
}
}
break;
}
return MACRO_NONE;
};
static bool singular_key = false;
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
// get the current layer
uint8_t layer = biton32(layer_state);
//custom layer handling for tri_layer,
switch (keycode) {
case FNKEY:
if (record->event.pressed) {
key_timer = timer_read();
singular_key = true;
layer_on(_FUNCTION);
} else {
if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
layer_off(_FUNCTION);
}
}
update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
return false;
break;
//SHIFT is handled as LSHIFT in the general case
case SHIFT:
if (record->event.pressed) {
key_timer = timer_read();
singular_key = true;
layer_on(_SHIFTED);
register_code(KC_LSFT);
} else {
if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
layer_off(_SHIFTED);
unregister_code(KC_LSFT);
}
}
update_tri_layer(_FUNCTION, _SHIFTED, _FUNCSHIFT);
return false;
break;
//If any other key was pressed during the layer mod hold period,
//then the layer mod was used momentarily, and should block latching
default:
singular_key = false;
break;
}
//FUNCSHIFT has been shifted by the SHIFT handling, some keys need to be excluded
if (layer == _FUNCSHIFT) {
//F1-F12 should be sent as unshifted keycodes,
//and ] needs to be unshifted or it is sent as }
if ( (keycode >= KC_F1 && keycode <= KC_F12)
|| keycode == KC_RBRC ) {
if (record->event.pressed) {
unregister_mods(MOD_LSFT);
} else {
register_mods(MOD_LSFT);
}
}
}
return true;
};
void matrix_scan_user(void) {
uint8_t layer = biton32(layer_state);
switch (layer) {
case _QWERTY:
set_led_off;
break;
case _FUNCTION:
set_led_blue;
break;
case _SHIFTED:
set_led_red;
break;
case _FUNCSHIFT:
set_led_green;
break;
default:
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.