summaryrefslogtreecommitdiff
path: root/users/drashna/keyrecords/caps_word.c
blob: 0c7cd6cfe553e260e4c45c1f3d3c15f61f41a474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0

#include "caps_word.h"

#ifndef IS_COMMAND
#    define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT)
#endif

bool caps_word_enabled = false;
bool caps_word_shifted = false;

/**
 * @brief Handler for Caps Word feature.
 *
 * This checks the keycodes, and applies shift to the correct keys, if and when needid.
 *
 * @param keycode Keycode from matrix
 * @param record keyrecord_t data structure
 * @return true Continue processing keycode and sent to host
 * @return false Stop processing keycode, and do not send to host
 */
bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
    if (!caps_word_enabled) {
        // Pressing both shift keys at the same time enables caps word.
        if (IS_COMMAND()) {
            clear_mods();
            clear_oneshot_mods();
            caps_word_shifted = false;
            caps_word_enabled = true;
            return false;
        }
        return true;
    }

    if (!record->event.pressed) {
        return true;
    }

    if (!((get_mods() | get_oneshot_mods()) & ~MOD_MASK_SHIFT)) {
        switch (keycode) {
            case QK_MOD_TAP ... QK_MOD_TAP_MAX:
            case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
                // Earlier return if this has not been considered tapped yet.
                if (record->tap.count == 0) {
                    return true;
                }
                // Get the base tapping keycode of a mod- or layer-tap key.
                keycode &= 0xff;
        }

        switch (keycode) {
            // Letter keys should be shifted.
            case KC_A ... KC_Z:
                if (!caps_word_shifted) {
                    register_code(KC_LSFT);
                }
                caps_word_shifted = true;
                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:
                if (caps_word_shifted) {
                    unregister_code(KC_LSFT);
                }
                caps_word_shifted = false;
                return true;

                // Any other keycode disables caps word.
        }
    }

    // Disable caps word.
    caps_word_enabled = false;
    if (caps_word_shifted) {
        unregister_code(KC_LSFT);
    }
    caps_word_shifted = false;
    return true;
}