hakbraley

QMK Leader alt

Sep 10th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. /*  https://www.reddit.com/r/olkb/comments/plqic8/ctrla_macro_sends_a_7_instead/
  2.  *  Code to include a macro key that is normally alt, but sends CTRL+A on tap.
  3.  *  This goes into keymap.c
  4.  *  
  5.  *  If you press another key while LDR_ALT is held down, this code assumes you're using ALT like normal
  6.  *  Otherwise, it will tap CTRL+A if you've held LDR_ALT for less than TAPPING_TERM.
  7.  *  This defaults to 200ms, and can be changed by defining it in config.h with   #define TAPPING_TERM 150
  8.  */
  9.  
  10. #define LEADER LCTL(KC_A)  //CTRL+A, your leader shortcut
  11.  
  12. enum custom_keycodes {
  13.     LDR_ALT = SAFE_RANGE, //normally alt, Ctrl+A on tap
  14.     //other macros,
  15. };
  16.  
  17.  
  18. bool key_pressed = false;    //variable used to check if a key is pressed while LDR_ALT is held
  19. uint16_t LDR_ALT_timer = 0;  //declare a timer variable to track hold time for LDR_ALT
  20.  
  21. bool process_record_user(uint16_t keycode, keyrecord_t *record) {  
  22.     if (record->event.pressed)
  23.         key_pressed = true;  //if any key is pressed, set key_pressed to true
  24.    
  25.     switch (keycode) {
  26.         case LDR_ALT:
  27.             if (record->event.pressed) {       //when LDR_ALT is pressed
  28.                 register_code(KC_RALT);        //  press right alt
  29.                 key_pressed = false;           //  set key_pressed to false  
  30.                 LDR_ALT_timer = timer_read();  //  mark the time LDR_ALT was pressed
  31.             } else {                           //when LDR_ALT is released
  32.                 unregister_code(KC_RALT);      //  release right alt
  33.                 if (key_pressed) break;  //  if another key was pressed while alt was held, exit and do not tap leader shortcut
  34.                
  35.                 if (timer_elapsed(LDR_ALT_timer) < TAPPING_TERM) {  //if LDR_ALT was held for less than TAPPING_TERM (default 200ms)
  36.                     tap_code16(LEADER);                             //  tap the LEADER shortcut, LCTL(KC_A) CTRL+A
  37.                 }
  38.             }
  39.             break;
  40.     }
  41.     return true;
  42. };
Add Comment
Please, Sign In to add comment