Skip to content

Commit c126882

Browse files
authored
[Keymap] fix NKRO - switch to get_mods() and refactor encoder action code (qmk#14278)
Co-authored-by: Jonavin <=>
1 parent 968a5c0 commit c126882

File tree

3 files changed

+87
-57
lines changed

3 files changed

+87
-57
lines changed

users/jonavin/jonavin.c

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -104,75 +104,94 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
104104
#endif // IDLE_TIMEOUT_ENABLE
105105

106106

107-
#if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
107+
#ifdef ENCODER_ENABLE
108108
#ifndef DYNAMIC_KEYMAP_LAYER_COUNT
109109
#define DYNAMIC_KEYMAP_LAYER_COUNT 4 //default in case this is not already defined elsewhere
110110
#endif
111111
#ifndef ENCODER_DEFAULTACTIONS_INDEX
112112
#define ENCODER_DEFAULTACTIONS_INDEX 0 // can select encoder index if there are multiple encoders
113113
#endif
114114

115-
uint8_t selected_layer = 0;
115+
void encoder_action_volume(bool clockwise) {
116+
if (clockwise)
117+
tap_code(KC_VOLU);
118+
else
119+
tap_code(KC_VOLD);
120+
}
116121

117-
__attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
122+
void encoder_action_mediatrack(bool clockwise) {
123+
if (clockwise)
124+
tap_code(KC_MEDIA_NEXT_TRACK);
125+
else
126+
tap_code(KC_MEDIA_PREV_TRACK);
127+
}
118128

119-
bool encoder_update_user(uint8_t index, bool clockwise) {
120-
if (!encoder_update_keymap(index, clockwise)) { return false; }
121-
if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
122-
if ( clockwise ) {
123-
if (keyboard_report->mods & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
124-
if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
125-
selected_layer ++;
126-
layer_move(selected_layer);
127-
}
128-
} else if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up
129-
unregister_mods(MOD_BIT(KC_RSFT));
130-
register_code(KC_PGDN);
131-
register_mods(MOD_BIT(KC_RSFT));
132-
} else if (keyboard_report->mods & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next word
133-
tap_code16(LCTL(KC_RGHT));
134-
} else if (keyboard_report->mods & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next track
135-
tap_code(KC_MEDIA_NEXT_TRACK);
136-
} else {
137-
switch (selected_layer) {
138-
case _FN1:
139-
#ifdef IDLE_TIMEOUT_ENABLE
140-
timeout_update_threshold(true);
141-
#endif
142-
break;
143-
default:
144-
tap_code(KC_VOLU); // Otherwise it just changes volume
145-
break;
146-
}
129+
void encoder_action_navword(bool clockwise) {
130+
if (clockwise)
131+
tap_code16(LCTL(KC_RGHT));
132+
else
133+
tap_code16(LCTL(KC_LEFT));
134+
}
135+
136+
void encoder_action_navpage(bool clockwise) {
137+
if (clockwise)
138+
tap_code16(KC_PGUP);
139+
else
140+
tap_code16(KC_PGDN);
141+
}
142+
143+
// LAYER HANDLING
144+
uint8_t selected_layer = 0;
145+
146+
uint8_t get_selected_layer(void) {
147+
return selected_layer;
148+
}
149+
150+
void encoder_action_layerchange(bool clockwise) {
151+
if (clockwise) {
152+
if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
153+
selected_layer ++;
154+
layer_move(selected_layer);
147155
}
148156
} else {
149-
if (keyboard_report->mods & MOD_BIT(KC_LSFT) ) {
150-
if (selected_layer > 0) {
151-
selected_layer --;
152-
layer_move(selected_layer);
153-
}
154-
} else if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) {
155-
unregister_mods(MOD_BIT(KC_RSFT));
156-
register_code(KC_PGUP);
157-
register_mods(MOD_BIT(KC_RSFT));
158-
} else if (keyboard_report->mods & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate previous word
159-
tap_code16(LCTL(KC_LEFT));
160-
} else if (keyboard_report->mods & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media previous track
161-
tap_code(KC_MEDIA_PREV_TRACK);
162-
} else {
163-
switch (selected_layer) {
164-
case _FN1:
165-
#ifdef IDLE_TIMEOUT_ENABLE
166-
timeout_update_threshold(false);
167-
#endif
168-
break;
169-
default:
170-
tap_code(KC_VOLD);
171-
break;
172-
}
157+
if (selected_layer > 0) {
158+
selected_layer --;
159+
layer_move(selected_layer);
173160
}
174161
}
162+
}
163+
#endif // ENCODER_ENABLE
164+
165+
#if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
175166

167+
__attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
168+
169+
bool encoder_update_user(uint8_t index, bool clockwise) {
170+
if (!encoder_update_keymap(index, clockwise)) { return false; }
171+
if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
172+
uint8_t mods_state = get_mods();
173+
if (mods_state & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
174+
encoder_action_layerchange(clockwise);
175+
} else if (mods_state & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up/dn
176+
unregister_mods(MOD_BIT(KC_RSFT));
177+
encoder_action_navpage(clockwise);
178+
register_mods(MOD_BIT(KC_RSFT));
179+
} else if (mods_state & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next/prev word
180+
encoder_action_navword(clockwise);
181+
} else if (mods_state & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next/prev track
182+
encoder_action_mediatrack(clockwise);
183+
} else {
184+
switch(get_highest_layer(layer_state)) {
185+
case _FN1:
186+
#ifdef IDLE_TIMEOUT_ENABLE
187+
timeout_update_threshold(clockwise);
188+
#endif
189+
break;
190+
default:
191+
encoder_action_volume(clockwise); // Otherwise it just changes volume
192+
break;
193+
}
194+
}
176195
return true;
177196
}
178197
#endif // ENCODER_ENABLE

users/jonavin/jonavin.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ enum custom_user_keycodes {
5858
#endif // TD_LSFT_CAPSLOCK_ENABLE
5959

6060

61+
// ENCODER ACTIONS
62+
#ifdef ENCODER_ENABLE
63+
void encoder_action_volume(bool clockwise);
64+
void encoder_action_mediatrack(bool clockwise);
65+
void encoder_action_navword(bool clockwise);
66+
void encoder_action_navpage(bool clockwise);
67+
68+
uint8_t get_selected_layer(void);
69+
void encoder_action_layerchange(bool clockwise);
70+
#endif // ENCODER_ENABLE
71+
72+
6173
#ifdef RGB_MATRIX_ENABLE
6274
//RGB custom colours
6375
#define RGB_GODSPEED 0x00, 0xE4, 0xFF // colour for matching keycaps

users/jonavin/readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ KEYMAP LEVEL ADDITIONAL PROCESSING FUNCTIONS
6565
void keyboard_post_init_keymap(void)
6666

6767
LIST OF COMPATIBLE KEYMAPS
68-
- gmmk/pro
6968
- gmmk/pro/ansi
7069
- keebio/quefrency/rev3
7170
- mechwild/mercutio
72-
- mechwild/murphpad (*)
71+
- mechwild/murphpad
7372
- mechwild/OBE (*)
7473
- nopunin10did/kastenwagen (*)
7574

0 commit comments

Comments
 (0)