Advertisement
hakbraley

QMK Layer Cycling key

Nov 27th, 2022
960
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/z6lm89/help_with_layer_switching_stack_overflow_the_key/
  2.  *  Code to create a button that will cycle through the layers, looping around to the beginning.
  3.  *  This can go into keymap.c
  4.  */
  5.  
  6. enum layer_names {
  7.     _LAYER0,
  8.     _LAYER1,
  9.     _LAYER2,
  10.     NUM_LAYERS,  // Leave this at the end of the list
  11. };
  12.  
  13. enum custom_keycodes {
  14.     CYCLE_LAYERS = SAFE_RANGE,  // Rename CYCLE_LAYERS to whatever you want, but leave SAFE_RANGE
  15.     // Other macros...
  16. };
  17.  
  18. /* This function is called automatically any time there is a layer change. */
  19. layer_state_t layer_state_set_user(layer_state_t state) {
  20.     switch(get_highest_layer(state) {
  21.         case _LAYER0:                   // If _LAYER0 is the highest active layer
  22.             rgblight_setrgb(RGB_RED);   // Set RGB to RED
  23.             break;
  24.         case _LAYER1:                   // If _LAYER1 is the highest active layer
  25.             rgblight_setrgb(RGB_BLUE);  // Set RGB to BLUE
  26.             break;
  27.         case _LAYER2:                   // If _LAYER2 is the highest active layer
  28.             rgblight_setrgb(RGB_GREEN); // Set RGB to GREEN
  29.             break;
  30.         default:
  31.             // instructions for any other layer
  32.             break;
  33.     }
  34.     return true;
  35. }
  36.  
  37.  
  38. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  39.     switch (keycode) {
  40.         case CYCLE_LAYERS:
  41.             if (record->event.pressed) {
  42.                 // Go to the next layer up, looping around to the beginning when the end is reached
  43.                 uint8_t next_layer = (get_highest_layer(layer_state) + 1) % NUM_LAYERS;
  44.                 layer_move(next_layer);  // layer_move(layer) is the same as the TO(layer) keycode
  45.                 break;
  46.             }
  47.     }
  48.     return true;
  49. }
  50.    
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement