← More about keyboards

Overview

A frequently asked question about QMK is how to change what a key types when it is shifted. For instance, how to make a key with “inverted” shifting such that it types : when pressed normally and ; when pressed shifted. Or how to implement “programmer” layouts having keys that type symbols normally and type the digits when pressed shifted.

Some example custom shifts.

It’s surprisingly tricky to get a custom shift key implemented just right. I’ve seen a lot of proposed solutions, and tried a few things myself. Some subtle gotchas:

Implementation

Here is my solution. It correctly handles key repeating and rolled presses, and I’ve tested that it works predictably in combination with one-shot mods, mod-taps, and Space Cadet Shift. It does not work with Auto Shift. To get the analogous effect with Auto Shift, use Auto Shift’s custom shifted values configuration.

Step 1: In your keymap.c, define a table of “custom_shift_key_t” structs. Each row defines one key. The keycode is the keycode as it appears in your layout and determines what is typed normally. The shifted_keycode is what you want the key to type when shifted. (See the QMK keycodes documentation for possible keycodes.)

Here is an example. The first row in my table has a . (KC_DOT) key that types ? (KC_QUES) when pressed shifted.

#include "features/custom_shift_keys.h"

const custom_shift_key_t custom_shift_keys[] = {
  {KC_DOT , KC_QUES}, // Shift . is ?
  {KC_COMM, KC_EXLM}, // Shift , is !
  {KC_MINS, KC_EQL }, // Shift - is =
  {KC_COLN, KC_SCLN}, // Shift : is ; 
};
uint8_t NUM_CUSTOM_SHIFT_KEYS =
    sizeof(custom_shift_keys) / sizeof(custom_shift_key_t);

Special cases:

Step 2: Handle custom shift keys from your process_record_user() function like so:

bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  if (!process_custom_shift_keys(keycode, record)) { return false; }
  // Your macros ...

  return true;
}

Step 3: In your rules.mk file, add

SRC += features/custom_shift_keys.c

Step 4: In the directory containing your keymap.c, create a features subdirectory and copy custom_shift_keys.h and custom_shift_keys.c there. This is the meat of the implementation.

custom_shift_keys.h

// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0

#pragma once
#include QMK_KEYBOARD_H

typedef struct {
  uint16_t keycode;
  uint16_t shifted_keycode;
} custom_shift_key_t;

extern const custom_shift_key_t custom_shift_keys[];
extern uint8_t NUM_CUSTOM_SHIFT_KEYS;

bool process_custom_shift_keys(uint16_t keycode, keyrecord_t *record);

custom_shift_keys.c

// Copyright 2021-2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0

#include "custom_shift_keys.h"

bool process_custom_shift_keys(uint16_t keycode, keyrecord_t *record) {
  static uint16_t registered_keycode = KC_NO;

  // If a custom shift key is registered, then this event is either
  // releasing it or manipulating another key at the same time. Either way,
  // we release the currently registered key.
  if (registered_keycode != KC_NO) {
    unregister_code16(registered_keycode);
    registered_keycode = KC_NO;
  }

  if (record->event.pressed) {  // Press event.
    const uint8_t mods = get_mods();
#ifndef NO_ACTION_ONESHOT
    if ((mods | get_weak_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT) {
#else
    if ((mods | get_weak_mods()) & MOD_MASK_SHIFT) {  // Shift is held.
#endif  // NO_ACTION_ONESHOT
      // Search for a custom key with keycode equal to `keycode`.
      for (int i = 0; i < NUM_CUSTOM_SHIFT_KEYS; ++i) {
        if (keycode == custom_shift_keys[i].keycode) {
          // Continue default handling if this is a tap-hold key being held.
          if (((QK_MOD_TAP <= keycode && keycode <= QK_MOD_TAP_MAX) ||
               (QK_LAYER_TAP <= keycode && keycode <= QK_LAYER_TAP_MAX)) &&
              record->tap.count == 0) {
            return true;
          }
#ifndef NO_ACTION_ONESHOT
          del_oneshot_mods(MOD_MASK_SHIFT);
#endif  // NO_ACTION_ONESHOT
          del_mods(MOD_MASK_SHIFT);
          del_weak_mods(MOD_MASK_SHIFT);
          send_keyboard_report();
          registered_keycode = custom_shift_keys[i].shifted_keycode;
          register_code16(registered_keycode);
          set_mods(mods);  // Restore the mods.
          return false;
        }
      }
    }
  }

  return true;  // Continue with default handling.
}

Compared to Key Overrides

In August 2021, QMK added Key Overrides, which “overrides” the keys sent for specified modifier-key combinations. In particular, it can be used to implement custom shift keys. Add “KEY_OVERRIDE_ENABLE = yes” in rules.mk to enable it, then the example above is analogously implemented as:

const key_override_t dot_key_override =
    ko_make_basic(MOD_MASK_SHIFT, KC_DOT, KC_QUES);  // Shift . is ?
const key_override_t comm_key_override =
    ko_make_basic(MOD_MASK_SHIFT, KC_COMM, KC_EXLM); // Shift , is !
const key_override_t mins_key_override =
    ko_make_basic(MOD_MASK_SHIFT, KC_MINS, KC_EQL);  // Shift - is =
const key_override_t coln_key_override =
    ko_make_basic(MOD_MASK_SHIFT, KC_COLN, KC_SCLN); // Shift : is ;

const key_override_t** key_overrides = (const key_override_t*[]){
    &dot_key_override,
    &comm_key_override,
    &mins_key_override,
    &coln_key_override,
    NULL
};

Advantages of custom_shift_keys:

Advantages of Key Overrides:

If you are already using Key Overrides for other purposes or have a couple kilobytes to spare, it is a great solution.

Explanation

The registered_keycode variable is the keycode of the custom shift key that is currently pressed or otherwise KC_NO. Only one custom key can be pressed at a time. Attempting to hold multiple custom shift keys releases all but the last one.

On each press or release of any key:

  1. If registered_keycode is not KC_NO, we release the currently active custom shift key (unregister_code16). To avoid stuck keys, this is always the right thing to do: either the event is releasing the active custom shift key (so we should release it), or it is a rolled press manipulating another key while the active custom shift key is still held (so again, we should release it).

  2. In the loop, we check whether the current key event is pressing a custom shift key. If so, we clear the shift mods, press the appropriate key depending on whether shift was held (register_code16), and restore the mods.

Acknowledgements

Thanks a bunch to @wheredoesyourmindgo on GitHub and u/uolot on Reddit for feedback and improvements to make custom shift keys better.

← More about keyboards