Skip to content

Commit

Permalink
Caps Word: make word breaking keys customizable.
Browse files Browse the repository at this point in the history
Adds an optional callback `caps_word_press_user()` to determine whether
a given key should continue Caps Word. Returning true from the callback
continues the current "word", while returning false is "word breaking"
and deactivates Caps Word. The callback also decides which keys to shift
(i.e., the letter keys). The default callback is

     bool caps_word_press_user(uint16_t keycode) {
       switch (keycode) {
         // Keycodes that continue Caps Word, with shift applied.
         case KC_A ... KC_Z:
           add_weak_mods(MOD_BIT(KC_LSFT));  // Apply shift to the next key.
           return true;

         // Keycodes that continue Caps Word, without shifting.
         case KC_1 ... KC_0:
         case KC_BSPC:
         case KC_MINS:
         case KC_UNDS:
           return true;

         default:
           return false;  // Deactivate Caps Word.
       }
     }

This commit also adds logic to handle swap hands keys.
  • Loading branch information
getreuer committed Jan 20, 2022
1 parent b102238 commit 7679002
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
42 changes: 28 additions & 14 deletions features/caps_word.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,17 @@ bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
keycode &= 0xff;
break;
#endif // NO_ACTION_TAPPING
}

switch (keycode) {
// Letter keys should be shifted.
case KC_A ... KC_Z:
add_weak_mods(MOD_BIT(KC_LSFT));
return true;

// Keycodes that continue Caps Word but shouldn't get shifted.
case KC_1 ... KC_0:
case KC_BSPC:
case KC_MINS:
case KC_UNDS:
return true;
#ifdef SWAP_HANDS_ENABLE
case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
if (keycode > 0x56F0 || record->tap.count == 0) { return true; }
keycode &= 0xff;
break;
#endif // SWAP_HANDS_ENABLE
}

// Any other keycode deactivates Caps Word.
if (caps_word_press_user(keycode)) {
return true;
}
}

Expand All @@ -106,3 +101,22 @@ bool caps_word_get(void) { return caps_word_active; }

__attribute__((weak)) void caps_word_set_user(bool active) {}

__attribute__((weak)) bool caps_word_press_user(uint16_t keycode) {
switch (keycode) {
// Keycodes that continue Caps Word, with shift applied.
case KC_A ... KC_Z:
add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to the next key.
return true;

// Keycodes that continue Caps Word, without shifting.
case KC_1 ... KC_0:
case KC_BSPC:
case KC_MINS:
case KC_UNDS:
return true;

default:
return false; // Deactivate Caps Word.
}
}

33 changes: 33 additions & 0 deletions features/caps_word.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ bool caps_word_get(void);
// }
void caps_word_set_user(bool active);

// An optional callback which is called on every key press while Caps Word is
// active. When the key should be shifted (that is, a letter key), the callback
// should call `add_weak_mods(MOD_BIT(KC_LSFT))` to shift the key. The callback
// also determines whether the key should continue Caps Word. Returning true
// continues the current "word", while returning false is "word breaking" and
// deactivates Caps Word. The default callback is
//
// bool caps_word_press_user(uint16_t keycode) {
// switch (keycode) {
// // Keycodes that continue Caps Word, with shift applied.
// case KC_A ... KC_Z:
// add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to the next key.
// return true;
//
// // Keycodes that continue Caps Word, without shifting.
// case KC_1 ... KC_0:
// case KC_BSPC:
// case KC_MINS:
// case KC_UNDS:
// return true;
//
// default:
// return false; // Deactivate Caps Word.
// }
// }
//
// To customize, copy the above function into your keymap and add/remove
// keycodes to the above cases.
//
// NOTE: Outside of this callback, you can use `caps_word_set(false)` to
// deactivate Caps Word.
bool caps_word_press_user(uint16_t keycode);

0 comments on commit 7679002

Please sign in to comment.