Advertisement
hakbraley

QMK Custom Layer Backspace

Apr 13th, 2023 (edited)
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. /*  QMK code to create a custom keycode that allows access to a target layer within a short time frame,
  2.  *  but otherwise acts as a backspace key.
  3.  */
  4.  
  5.  
  6. // config.h -------------------------------------------------------------------
  7.  
  8. #define LT_BSPC_LAYER      _NUMPAD  // set your target layer here
  9. #define LT_BSPC_HOLD_TIME  500      // set the amount of time in ms that the target layer is available
  10.  
  11.  
  12.  
  13. // keymap.c ----------------------------------------------------------------
  14.  
  15. enum custom_keycodes {
  16.     LT_BSPC = SAFE_RANGE,
  17. };
  18.  
  19.  
  20. uint16_t last_keypress_time = 0;    // timer variable to track when the last key was pressed
  21. uint16_t lt_bspc_pressed_time = 0;  // timer variable to track when LT_BSPC was pressed
  22. bool     bspc_held = false;         // tracks whether KC_BACKSPACE is currently registered
  23.  
  24.  
  25. static bool key_was_pressed_after_lt_bspc(void) {
  26.     return (lt_bspc_pressed_time != 0)  &&  (last_keypress_time != lt_bspc_pressed_time);
  27. }
  28.  
  29. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  30.     if (record->event.pressed)
  31.         last_keypress_time = record->event.time;  // update on every keypress with the time of the keypress
  32.  
  33.     switch (keycode) {
  34.         case LT_BSPC:
  35.             if (record->event.pressed) {                    // on LT_BSPC keypress
  36.                 layer_on(LT_BSPC_LAYER);                    //   activate the target layer
  37.                 lt_bspc_pressed_time = record->event.time;  //   mark the time that LT_BSPC was pressed
  38.             } else {                                        // on LT_BSPC keyrelease
  39.                 layer_off(LT_BSPC_LAYER);                   //   deactivate the target layer
  40.                
  41.                 // if no other key was pressed after LT_BSPC, and LT_BSPC has not been held for longer than LT_BSPC_HOLD_TIME
  42.                 if (!key_was_pressed_after_lt_bspc()  &&  timer_elapsed(lt_bspc_pressed_time) < LT_BSPC_HOLD_TIME) {
  43.                     tap_code(KC_BACKSPACE);
  44.                 }
  45.                 // unregister KC_BACKSPACE if it is registered
  46.                 if (bspc_held) {
  47.                     unregister_code(KC_BACKSPACE);
  48.                     bspc_held = false;
  49.                 }
  50.                 lt_bspc_pressed_time = 0;  // reset the pressed time
  51.             }
  52.             break;
  53.     }
  54.     return true;
  55. }
  56.  
  57.  
  58. void housekeeping_task_user(void) {
  59.     if (!bspc_held  &&  lt_bspc_pressed_time != 0) {
  60.         if (!key_was_pressed_after_lt_bspc()  &&  timer_elapsed(lt_bspc_pressed_time) >= LT_BSPC_HOLD_TIME) {
  61.             register_code(KC_BACKSPACE);
  62.             bspc_held = true;
  63.             layer_off(LT_BSPC_LAYER);
  64.         }
  65.     }
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement