Parent directory

process_grave_esc.c

2377 bytes
 1/* Copyright 2020
 2 *
 3 * This program is free software: you can redistribute it and/or modify
 4 * it under the terms of the GNU General Public License as published by
 5 * the Free Software Foundation, either version 2 of the License, or
 6 * (at your option) any later version.
 7 *
 8 * This program is distributed in the hope that it will be useful,
 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16#include "process_grave_esc.h"
17#include "keycodes.h"
18#include "modifiers.h"
19#include "action_util.h"
20
21/* true if the last press of QK_GRAVE_ESCAPE was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
22 * Used to ensure that the correct keycode is released if the key is released.
23 */
24static bool grave_esc_was_shifted = false;
25
26bool process_grave_esc(uint16_t keycode, keyrecord_t *record) {
27    if (keycode == QK_GRAVE_ESCAPE) {
28        const uint8_t mods    = get_mods();
29        uint8_t       shifted = mods & MOD_MASK_SG;
30
31#ifdef GRAVE_ESC_ALT_OVERRIDE
32        // if ALT is pressed, ESC is always sent
33        // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
34        if (mods & MOD_MASK_ALT) {
35            shifted = 0;
36        }
37#endif
38
39#ifdef GRAVE_ESC_CTRL_OVERRIDE
40        // if CTRL is pressed, ESC is always sent
41        // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
42        if (mods & MOD_MASK_CTRL) {
43            shifted = 0;
44        }
45#endif
46
47#ifdef GRAVE_ESC_GUI_OVERRIDE
48        // if GUI is pressed, ESC is always sent
49        if (mods & MOD_MASK_GUI) {
50            shifted = 0;
51        }
52#endif
53
54#ifdef GRAVE_ESC_SHIFT_OVERRIDE
55        // if SHIFT is pressed, ESC is always sent
56        if (mods & MOD_MASK_SHIFT) {
57            shifted = 0;
58        }
59#endif
60
61        if (record->event.pressed) {
62            grave_esc_was_shifted = shifted;
63            add_key(shifted ? KC_GRAVE : KC_ESCAPE);
64        } else {
65            del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
66        }
67
68        send_keyboard_report();
69        return false;
70    }
71
72    // Not a grave keycode so continue processing
73    return true;
74}