aboutsummaryrefslogtreecommitdiff
path: root/users/ericgebhart/caps_word.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/ericgebhart/caps_word.c')
-rw-r--r--users/ericgebhart/caps_word.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/users/ericgebhart/caps_word.c b/users/ericgebhart/caps_word.c
new file mode 100644
index 000000000..ba81c15d6
--- /dev/null
+++ b/users/ericgebhart/caps_word.c
@@ -0,0 +1,81 @@
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15//
16// For full documentation, see
17// https://getreuer.info/posts/keyboards/caps-word
18
19#include "caps_word.h"
20
21bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
22 static bool caps_word_enabled = false;
23 static bool shifted = false;
24#ifndef NO_ACTION_ONESHOT
25 const uint8_t mods = get_mods() | get_oneshot_mods();
26#else
27 const uint8_t mods = get_mods();
28#endif // NO_ACTION_ONESHOT
29
30 if (!caps_word_enabled) {
31 // Pressing both shift keys at the same time enables caps word.
32 if ((mods & MOD_MASK_SHIFT) == MOD_MASK_SHIFT) {
33 clear_mods();
34#ifndef NO_ACTION_ONESHOT
35 clear_oneshot_mods();
36#endif // NO_ACTION_ONESHOT
37 shifted = false;
38 caps_word_enabled = true;
39 return false;
40 }
41 return true;
42 }
43
44 if (!record->event.pressed) { return true; }
45
46 if (!(mods & ~MOD_MASK_SHIFT)) {
47 switch (keycode) {
48 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
49 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
50 // Earlier return if this has not been considered tapped yet.
51 if (record->tap.count == 0) { return true; }
52 // Get the base tapping keycode of a mod- or layer-tap key.
53 keycode &= 0xff;
54 }
55
56 switch (keycode) {
57 // Letter keys should be shifted.
58 case KC_A ... KC_Z:
59 if (!shifted) { register_code(KC_LSFT); }
60 shifted = true;
61 return true;
62
63 // Keycodes that continue caps word but shouldn't get shifted.
64 case KC_1 ... KC_0:
65 case KC_BSPC:
66 case KC_MINS:
67 case KC_UNDS:
68 if (shifted) { unregister_code(KC_LSFT); }
69 shifted = false;
70 return true;
71
72 // Any other keycode disables caps word.
73 }
74 }
75
76 // Disable caps word.
77 caps_word_enabled = false;
78 if (shifted) { unregister_code(KC_LSFT); }
79 shifted = false;
80 return true;
81}