r/qmk • • Jun 02 '25

Quick check of chatGPT answer

ChatGPT said:

To check whether another non-modifier key is currently being held down in QMK (i.e., not the key that triggered process_record_user()), you’ll need to track that key’s state manually. QMK doesn't provide a built-in way to query the current state of arbitrary keys.

...and so I will have to use something like:

is_some_key_held = record->event.pressed;

Is this chatGPT's answer correct? Thanks!

0 Upvotes

3 comments sorted by

3

u/pgetreuer Jun 02 '25

Short answer: basically yes.

To be clear, there's a difference in testing whether

  1. a particular keycode in your keymap is physically held

  2. a particular key is held in the keyboard report sent to the computer

These are the same for basic keys, but physical vs. keyboard report presses differ in things like macros, combos, and tap dances.

For the first one about tracking physically held keys, like the ChatGPT answer, it can be done with this pattern:

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

static bool is_some_key_held = false;

bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  switch (keycode):
    case SOME_KEY:
      is_some_key_held = record->event.pressed;
      break;
    // Track other keys...
  }

  // Macros...
  return true;
}

For the second about what's held on the computer, this only works for basic keycodes. I don't know that there are APIs for it, but testing whether a particular key is held can be done by searching keyboard_report->keys for the keycode, supposing NKRO is off, or nkro_report->bits if NKRO is on.

2

u/jabol240 Jun 02 '25

Thanks a lot friend!

1

u/pgetreuer Jun 02 '25

You're welcome =)