Skip to content

Instantly share code, notes, and snippets.

@brunomlopes
Last active May 5, 2021 18:53
Show Gist options
  • Save brunomlopes/2ae331253d68c62206164a4beb300826 to your computer and use it in GitHub Desktop.
Save brunomlopes/2ae331253d68c62206164a4beb300826 to your computer and use it in GitHub Desktop.
A remix of the REPEAT key from precondition which allows for numeric repeat. Repeat works on key up, and while pressed, any numbers are counted to repeat. So, press T, then REPEAT,1,0 and it should repeat T 10 times. Currently it's failing at the modifiers
// Used to extract the basic tapping keycode from a dual-role key.
// Example: GET_TAP_KC(MT(MOD_RSFT, KC_E)) == KC_E
#define GET_TAP_KC(dual_role_key) dual_role_key & 0xFF
uint16_t repeat_last_keycode = KC_NO;
uint8_t repeat_last_modifier = 0;
uint8_t repeat_count = 0;
// Initialize variables holding the bitfield
// representation of active modifiers.
uint8_t repeat_mod_state;
uint8_t repeat_oneshot_mod_state;
bool repeat_is_counting = false;
bool process_repeat_key(uint16_t keycode, const keyrecord_t *record) {
if (keycode != REPEAT) {
repeat_last_modifier = repeat_oneshot_mod_state > repeat_mod_state ? repeat_oneshot_mod_state : repeat_mod_state;
switch (keycode) {
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
case QK_MOD_TAP ... QK_MOD_TAP_MAX:
if (record->event.pressed) {
repeat_last_keycode = GET_TAP_KC(keycode);
}
break;
case KC_1 ... KC_9:
if(repeat_is_counting && record->event.pressed){
repeat_count *= 10;
repeat_count += keycode - (KC_1-1);
return false;
}
break;
case KC_0:
if(repeat_is_counting && record->event.pressed){
repeat_count *= 10;
return false;
}
break;
default:
if(record->event.pressed){
repeat_last_keycode = keycode;
}
break;
}
} else { // keycode == REPEAT
if (record->event.pressed) {
repeat_is_counting = true;
repeat_count=0;
} else {
repeat_is_counting = false;
if(repeat_count>0){
for (size_t i = 0; i < repeat_count; i++)
{
register_mods(repeat_last_modifier);
tap_code16(repeat_last_keycode);
unregister_mods(repeat_last_modifier);
}
} else {
register_mods(repeat_last_modifier);
tap_code16(repeat_last_keycode);
unregister_mods(repeat_last_modifier);
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment