Skip to content

Instantly share code, notes, and snippets.

@bottilabo
Last active July 10, 2020 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bottilabo/4ff5b869e1d79b56defe308f8a228fcc to your computer and use it in GitHub Desktop.
Save bottilabo/4ff5b869e1d79b56defe308f8a228fcc to your computer and use it in GitHub Desktop.
MOD TAP filtering without modifing QMK firmware
#define ssend_string(...) {char ssendbuf[32];sprintf(ssendbuf,__VA_ARGS__);send_string(ssendbuf);}
static int8_t cntPressing = 0;
static uint16_t timeReleased = 0;
static bool fTappable = false;
static bool fTapping = false;
static uint16_t valTimeMODTAP = 300;
static uint16_t selParam = (uint16_t)SEL_MODTAP;
bool process_modtap_filter(uint16_t keycode, keyrecord_t *record) {
if (!record->event.pressed) {
cntPressing--;
if(cntPressing == 0) {
timeReleased = timer_read();
}
}
else {
cntPressing++;
if(cntPressing == 1) {
uint16_t elapsed = TIMER_DIFF_16(record->event.time,timeReleased );
fTappable = ( valTimeMODTAP < elapsed );
}
switch(keycode) {
case VAL_UP:
case VAL_DN:
{
bool fup = (keycode == VAL_UP);
switch(selParam) {
case SEL_MODTAP:
{
if(fup)
valTimeMODTAP += 10;
else
valTimeMODTAP -= 10;
if(valTimeMODTAP<30)
valTimeMODTAP = 30;
if(valTimeMODTAP>500)
valTimeMODTAP = 500;
}
break;
}
}
break;
case SEL_VOL: selParam = (uint16_t)SEL_VOL; break;
case SEL_MODTAP: selParam = (uint16_t)SEL_MODTAP; break;
case REPORT:
ssend_string("modtap:%u",valTimeMODTAP);
break;
}
}
//
uint8_t modkey = 0;
bool fTapcode = QK_MOD_TAP <= keycode && keycode <= QK_MOD_TAP_MAX;
if(fTapcode) {
modkey = (keycode >> 8) & 0x1f;
if( modkey == MOD_LSFT || modkey == MOD_RSFT )
fTapcode = false;
}
if(cntPressing == 1 && record->event.pressed ) {
if(fTapcode) {
if( fTappable ) {
fTapping = true;
return true;
}
else {
fTappable = false;
fTapping = false;
register_code(keycode & 0xff);
unregister_code(keycode & 0xff);
return false;
}
}
else {
fTappable = false;
fTapping = false;
}
}
else if (!fTapping){
if( fTapcode ) {
if(record->event.pressed) {
register_code(keycode & 0xff);
unregister_code(keycode & 0xff);
return false;
}
else {
return false;
}
}
}
return true;
}
#define MODTAP_FILTER_PROC \
if( ! process_modtap_filter(keycode,record) ) return false;
@bottilabo
Copy link
Author

bottilabo commented Jul 22, 2019

keymap.c で、
#include に、次のように追加する。
#include "modtap-filter.c"

次のようなカスタムキーコードの定義を追加する。

enum custom_keycodes {
  BASE = SAFE_RANGE,
  REPORT,
  SEL_VOL,
  SEL_MODTAP,
  VAL_UP,
  VAL_DN,
};

process_record_userの先頭に、次のように追加する。

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    MODTAP_FILTER_PROC;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment