Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] get_keycode_string(): function to format keycodes as strings, for more readable debug logging. #24787

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

getreuer
Copy link
Contributor

@getreuer getreuer commented Jan 5, 2025

This PR adds get_keycode_string() as described in https://getreuer.info/posts/keyboards/keycode-string/index.html as a utility function in core.

Description

It's much nicer to read keycodes as names like "LT(2,KC_D)" than numerical codes like "0x4207." This PR adds a function get_keycode_string() for that purpose, based on my userspace implementation linked above.

How to use it

Add in rules.mk:

KEYCODE_STRING_ENABLE = yes

Then use the get_keycode_string(kc) function to convert a given 16-bit keycode to a human-readable string. Example:

uint16_t keycode = C(KC_PGUP);
const char *key_name = get_keycode_string(keycode);
dprintf("kc: %s\n", key_name); // Logs "kc: C(KC_PGUP)"

Limitations

  • Many common QMK keycodes are recognized by get_keycode_string(), but not all. These include some common basic keycodes, layer switch keycodes, mod-taps, one-shot keycodes, tap dance keycodes, and Unicode keycodes. As a fallback, an unrecognized keycode is written as a hex number. Names for additional keycodes can also be added, see below.

  • Keycodes are stringified according to the default KC_-prefixed keycodes, which is suboptimal when the computer is configured to a non-US-QWERTY layout.

Defining names for additional keycodes

Optionally, keycode_string_names_user or keycode_string_names_kb may be defined to add names for additional keycodes. For example, supposing keymap.c defines MYMACRO1 and MYMACRO2 as custom keycodes, the following adds their names:

const keycode_string_name_t keycode_string_names_user[] = {
    KEYCODE_STRING_NAME(MYMACRO1),
    KEYCODE_STRING_NAME(MYMACRO2),
    KEYCODE_STRING_NAMES_END // End of table sentinel.
};

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout (addition or update)
  • Documentation

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

This adds the `keycode_string()` function described in
https://getreuer.info/posts/keyboards/keycode-string/index.html
as a core feature.
quantum/keycode_string.c Outdated Show resolved Hide resolved
quantum/keycode_string.c Outdated Show resolved Hide resolved
@drashna drashna requested a review from a team January 5, 2025 17:45
* Rename keycode_string() -> get_keycode_string() for consistency with
  existing string utils like get_u8_str().

* Revise custom keycode names with separate _user and _kb tables.
@getreuer
Copy link
Contributor Author

getreuer commented Jan 6, 2025

Thanks for the quick review, @drashna! I've updated the PR according to your comments.

@getreuer getreuer changed the title [Core] keycode_string(): function to format keycodes as strings, for more readable debug logging. [Core] get_keycode_string(): function to format keycodes as strings, for more readable debug logging. Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants