Advertisement
hakbraley

QMK ModTap Alt+Tab

Oct 25th, 2021
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. /*  https://www.reddit.com/r/olkb/comments/qfkzan/quick_qmk_question/
  2.  *  Code to include a macro key that is normally ALT, but sends ALT+TAB on tap.
  3.  *  This goes into keymap.c
  4.  *  
  5.  *  If you press another key while ALT_TAB is held down, this code assumes you're using ALT like normal
  6.  *  Otherwise, it will tap ALT+TAB if you've held ALT_TAB 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. enum custom_keycodes {
  11.     ALT_TAB = SAFE_RANGE,   //the custom macro key that you'll place into your keymap
  12.     //other macros,
  13. };
  14.  
  15.  
  16. static uint16_t ALT_TAB_timer = 0;  //declare a timer variable to track hold time for ALT_TAB
  17. static bool key_pressed = false;    //variable used to check if a key was pressed while ALT_TAB was held
  18.  
  19. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  20.     if (record->event.pressed)
  21.         key_pressed = true;  //if any key is pressed, set key_pressed to true
  22.    
  23.     switch(keycode) {
  24.         case ALT_TAB:
  25.             if (record->event.pressed) {        //when ALT_TAB is pressed
  26.                 register_code(KC_LALT);         //  press left alt
  27.                 ALT_TAB_timer = timer_read();   //  mark the time ALT_TAB is pressed
  28.                 key_pressed = false;            //  set key_pressed to false
  29.             } else {                            //when ALT_TAB is released
  30.                 unregister_code(KC_LALT);       //  release left alt
  31.                
  32.                 if (key_pressed)                //if another key was pressed while ALT_TAB was held,
  33.                     return false;               //  do nothing else
  34.                
  35.                 if (timer_elapsed(ALT_TAB_timer) < TAPPING_TERM) {  //if ALT_TAB was held for less than TAPPING_TERM
  36.                     tap_code16( LALT(KC_TAB) );                     //  tap ALT+TAB
  37.                 }
  38.             }
  39.             break;
  40.     }
  41.    
  42.     return true;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement