Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* QMK code to create a custom keycode that allows access to a target layer within a short time frame,
- * but otherwise acts as a backspace key.
- */
- // config.h -------------------------------------------------------------------
- #define LT_BSPC_LAYER _NUMPAD // set your target layer here
- #define LT_BSPC_HOLD_TIME 500 // set the amount of time in ms that the target layer is available
- // keymap.c ----------------------------------------------------------------
- enum custom_keycodes {
- LT_BSPC = SAFE_RANGE,
- };
- uint16_t last_keypress_time = 0; // timer variable to track when the last key was pressed
- uint16_t lt_bspc_pressed_time = 0; // timer variable to track when LT_BSPC was pressed
- bool bspc_held = false; // tracks whether KC_BACKSPACE is currently registered
- static bool key_was_pressed_after_lt_bspc(void) {
- return (lt_bspc_pressed_time != 0) && (last_keypress_time != lt_bspc_pressed_time);
- }
- bool process_record_user(uint16_t keycode, keyrecord_t *record) {
- if (record->event.pressed)
- last_keypress_time = record->event.time; // update on every keypress with the time of the keypress
- switch (keycode) {
- case LT_BSPC:
- if (record->event.pressed) { // on LT_BSPC keypress
- layer_on(LT_BSPC_LAYER); // activate the target layer
- lt_bspc_pressed_time = record->event.time; // mark the time that LT_BSPC was pressed
- } else { // on LT_BSPC keyrelease
- layer_off(LT_BSPC_LAYER); // deactivate the target layer
- // if no other key was pressed after LT_BSPC, and LT_BSPC has not been held for longer than LT_BSPC_HOLD_TIME
- if (!key_was_pressed_after_lt_bspc() && timer_elapsed(lt_bspc_pressed_time) < LT_BSPC_HOLD_TIME) {
- tap_code(KC_BACKSPACE);
- }
- // unregister KC_BACKSPACE if it is registered
- if (bspc_held) {
- unregister_code(KC_BACKSPACE);
- bspc_held = false;
- }
- lt_bspc_pressed_time = 0; // reset the pressed time
- }
- break;
- }
- return true;
- }
- void housekeeping_task_user(void) {
- if (!bspc_held && lt_bspc_pressed_time != 0) {
- if (!key_was_pressed_after_lt_bspc() && timer_elapsed(lt_bspc_pressed_time) >= LT_BSPC_HOLD_TIME) {
- register_code(KC_BACKSPACE);
- bspc_held = true;
- layer_off(LT_BSPC_LAYER);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement