Advertisement
hakbraley

QMK oneshot layer with tapped mod

Sep 10th, 2021 (edited)
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. /*  https://www.reddit.com/r/olkb/comments/plk4ts/shift_tap_one_shot/
  2.  *  Code to include a macro key that is normally shift, but activates a oneshot layer on tap.
  3.  *  This goes into keymap.c
  4.  *  
  5.  *  The macro name used here is SH_LAY2, for SHIFT_LAYER2.  Call it what you want, but update everywhere it is mentioned.
  6.  *  It will press(register_code) and release(unregister_code) KC_LSFT, left shift.  Change this key on lines 31 and 35.  
  7.  *  Update the oneshot layer on line 39.
  8.  *  SH_LAY2_timer is specific to SH_LAY2.  Rename it if you rename SH_LAY2 to avoid confusion, and create another timer for each key like this.
  9.  *
  10.  *  If you press another key while SH_LAY2 is held down, this code assumes you're using shift like normal, and will not activate the oneshot layer.
  11.  *  Otherwise, the oneshot will activate if you've held SH_LAY2 for less than TAPPING_TERM.
  12.  *  This defaults to 200ms, and can be changed by defining it in config.h   #define TAPPING_TERM 150
  13.  */
  14.  
  15. enum custom_keycodes {
  16.     SH_LAY2 = SAFE_RANGE, //normal shift, oneshot layer 2 on tap
  17.     //other macros,
  18. };
  19.  
  20.  
  21. bool key_pressed = false;    //variable used to check if a key is pressed while SH_LAY2 is held
  22. uint16_t SH_LAY2_timer = 0;  //declare a timer variable to track hold time for SH_LAY2
  23.  
  24. bool process_record_user(uint16_t keycode, keyrecord_t *record) {  
  25.     if (record->event.pressed)
  26.         key_pressed = true;  //if any key is pressed, set key_pressed to true
  27.    
  28.     switch (keycode) {
  29.         case SH_LAY2:
  30.             if (record->event.pressed) {       //when SH_LAY2 is pressed
  31.                 register_code(KC_LSFT);        //  press left shift
  32.                 key_pressed = false;           //  set key_pressed to false  
  33.                 SH_LAY2_timer = timer_read();  //  mark the time SH_LAY2 was pressed
  34.             } else {                           //when SH_LAY2 is released
  35.                 unregister_code(KC_LSFT);      //  release left shift
  36.                 if (key_pressed) break;  //  if another key was pressed while shift was held, exit and do not use one shot layer
  37.                
  38.                 if (timer_elapsed(SH_LAY2_timer) < TAPPING_TERM) {  //if SH_LAY2 was held for less than TAPPING_TERM (default 200ms)
  39.                     set_oneshot_layer( 2 , ONESHOT_START);          //  activate oneshot layer 2 (replace '2' with layer name or #)
  40.                     clear_oneshot_layer_state(ONESHOT_PRESSED);     //  necessary for oneshots in macros.  don't touch this line
  41.                 }
  42.             }
  43.             break;
  44.     }
  45.     return true;
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement