Skip to content

Instantly share code, notes, and snippets.

@grafov
Last active September 9, 2024 10:25
Show Gist options
  • Save grafov/c5833c9c3763d265c6dc6108c5fca76c to your computer and use it in GitHub Desktop.
Save grafov/c5833c9c3763d265c6dc6108c5fca76c to your computer and use it in GitHub Desktop.
How to implement "SNAP TAP" like feature on QMK keyboard
// Not for copypaste! It is only part of keymap.c!
// You should adapt the code above for your keyboard especially for your layers.
// WARNING! You could be banned in Valve games because the feautre treated by Valve as a cheat.
//
// It uses two user hooks: layer_state_set_user() and process_record_user() as defined by QMK.
// Example enum with layers.
enum layers {
DEFAULT_LAYER,
GAME_LAYER,
// ...
}
// global var that keeps current layer
uint8_t cur_layer = YOUR_DEFAULT_LAYER;
// Hook that triggered on a layer state change.
layer_state_t layer_state_set_user(layer_state_t state) {
static uint8_t old_layer = 0xff;
cur_layer = get_highest_layer(state);
// ... handle other things on layer change
}
// Hook on the each key press/release
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
// SNAP TAP (separate A/D key press in games)
static bool apressed = false;
static bool dpressed = false;
// Apply only for GAME_LAYER and not mess A/D keys for other layers.
if (cur_layer == GAME_LAYER) {
switch(keycode) {
case KC_A:
if (record->event.pressed) {
apressed = true;
if (dpressed) {
SEND_STRING(SS_UP(X_D));
dpressed = false;
};
} else {
// to prevent double key release
if (!apressed) {
return false;
}
apressed = false;
}
return true;
case KC_D:
if (record->event.pressed) {
dpressed = true;
if (apressed) {
SEND_STRING(SS_UP(X_A));
};
} else {
// to prevent double key release
if (!dpressed) {
return false;
}
dpressed = false;
}
if (!record->event.pressed) {
// to prevent double release
if (dpressed) {
return false;
}
}
return true;
}
}
// ... handle other key presses
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment