Advertisement
hakbraley

QMK Highlight Keys with Keycodes

Apr 24th, 2023
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. /*  https://www.reddit.com/r/olkb/comments/12wg84s/is_there_a_way_to_determine_if_a_key_is_bound_or/
  2.  *  QMK code to set the color of the LEDs on keys that have a keycode assigned to them, on certain layers.
  3.  *  Replace _MODLAYER1 and _MODLAYER2 with the names or numbers of your desired layers.
  4.  *  Replace RGB_GREEN and RGB_MAGENTA with the desired color for each layer.
  5.  *  This can be a named color from https://github.com/qmk/qmk_firmware/blob/master/quantum/color.h
  6.  *    or 3 separate values for red, green, and blue.  e.g. highlight_bound_keys(_MODLAYER1, 0, 255, 0);
  7.  *
  8.  *  This can go into keymap.c
  9.  */
  10.  
  11.  
  12. void highlight_bound_keys(uint8_t layer, uint8_t red, uint8_t green, uint8_t blue) {
  13.     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  14.         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  15.             uint8_t led_index;
  16.             // Returns false if there is no LED at this key position
  17.             // Otherwise, it writes the index of the LED to the given variable
  18.             if (!rgb_matrix_map_row_column_to_led(row, col, &led_index))
  19.                 continue;  // Skip any key with no led
  20.            
  21.             // If the current key has a keycode assigned to it, set the LED to the given color
  22.             // If the key is KC_NO or KC_TRANSPARENT, set the LED color to black/off
  23.             if (keycode_at_keymap_location(layer, row, col) > KC_TRANSPARENT) {
  24.                 rgb_matrix_set_color(led_index, red, green, blue);
  25.             } else {
  26.                 rgb_matrix_set_color(led_index, RGB_OFF);
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  33.     switch(get_highest_layer(layer_state)) {
  34.         case _MODLAYER1:                                    // if _MODLAYER1 is the top active layer
  35.             highlight_bound_keys(_MODLAYER1, RGB_GREEN);    // highlight keys that have keycodes with the color GREEN
  36.             break;
  37.         case _MODLAYER2:                                    // if _MODLAYER2 is the top active layer
  38.             highlight_bound_keys(_MODLAYER2, RGB_MAGENTA);  // highlight keys that have keycodes with the color MAGENTA
  39.             break;
  40.     }
  41.     return true;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement