Created
May 27, 2023 07:32
-
-
Save EgZvor/00078c1d92e467b5835f0cfed3a4c22b to your computer and use it in GitHub Desktop.
[QMK] feature/streak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include QMK_KEYBOARD_H | |
#include "streak.h" | |
enum keycodes { | |
STREAK = SAFE_RANGE, | |
}; | |
bool process_record_user(uint16_t keycode, keyrecord_t *record) { | |
if (!process_streak(STREAK, keycode, record)) { return false; } | |
update_streak(STREAK, keycode, record); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include "streak.h" | |
typedef struct { | |
uint16_t keycode; | |
uint8_t mods; | |
} streak_item; | |
static streak_item streak[STREAK_N] = {{0}}; | |
void update_streak(uint16_t trigger, uint16_t keycode, keyrecord_t *record) { | |
static uint16_t streak_timer = 0; | |
if (record->event.pressed) { return; } | |
if (keycode == trigger) { return; } | |
switch (keycode) { | |
case KC_LCTL ... KC_RGUI: | |
// Ignore mods. | |
return; | |
} | |
if (timer_elapsed(streak_timer) >= STREAK_TERM) { | |
// Start a new streak. | |
streak_timer = timer_read(); | |
streak[0] = (streak_item){keycode, get_mods()}; | |
for (size_t i = 1; i < STREAK_N; i++) { | |
streak[i] = (streak_item){0, 0}; | |
} | |
} else { | |
// Append to existing streak. | |
for (size_t i = 0; i < STREAK_N; i++) { | |
if (streak[i].keycode != 0) { | |
// If the streak is full, don't do anything. | |
// To start a new streak, wait until STREAK_TERM | |
// elapses. | |
continue; | |
} | |
streak[i] = (streak_item){keycode, get_mods()}; | |
break; | |
} | |
} | |
} | |
bool process_streak(uint16_t trigger, uint16_t keycode, keyrecord_t *record) { | |
if (keycode != trigger) { return true; } | |
if (!record->event.pressed) { return false; } | |
for (size_t i = 0; i < STREAK_N; i++) { | |
// Reached the end of the streak. | |
if (streak[i].keycode == 0) { break; } | |
set_mods(streak[i].mods); | |
tap_code16(streak[i].keycode); | |
clear_mods(); | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include QMK_KEYBOARD_H | |
#ifndef STREAK_N | |
#define STREAK_N 3 | |
#endif | |
#ifndef STREAK_TERM | |
#define STREAK_TERM 1000 | |
#endif | |
void update_streak(uint16_t trigger, uint16_t keycode, keyrecord_t *record); | |
bool process_streak(uint16_t trigger, uint16_t keycode, keyrecord_t *record); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment