From 3345ce268610edbca8f53bc2909c547485531603 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 17 Feb 2021 07:26:52 +1100 Subject: [PATCH] Add `tap_code_delay(code, delay)` (#11913) Co-authored-by: Drashna Jaelre --- docs/feature_macros.md | 10 ++++++++-- tmk_core/common/action.c | 23 ++++++++++++++--------- tmk_core/common/action.h | 1 + 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/feature_macros.md b/docs/feature_macros.md index aa1ebc337..6e69ad642 100644 --- a/docs/feature_macros.md +++ b/docs/feature_macros.md @@ -233,9 +233,15 @@ Parallel to `register_code` function, this sends the `` keyup event to the c ### `tap_code();` -This will send `register_code()` and then `unregister_code()`. This is useful if you want to send both the press and release events ("tap" the key, rather than hold it). +Sends `register_code()` and then `unregister_code()`. This is useful if you want to send both the press and release events ("tap" the key, rather than hold it). -If you're having issues with taps (un)registering, you can add a delay between the register and unregister events by setting `#define TAP_CODE_DELAY 100` in your `config.h` file. The value is in milliseconds. +If `TAP_CODE_DELAY` is defined (default 0), this function waits that many milliseconds before calling `unregister_code()`. This can be useful when you are having issues with taps (un)registering. + +If the keycode is `KC_CAPS`, it waits `TAP_HOLD_CAPS_DELAY` milliseconds instead (default 80), as macOS prevents accidental Caps Lock activation by waiting for the key to be held for a certain amount of time. + +### `tap_code_delay(, );` + +Like `tap_code()`, but with a `delay` parameter for specifying arbitrary intervals before sending the unregister event. ### `register_code16();`, `unregister_code16();` and `tap_code16();` diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c index e4a97e0bc..74bfc0626 100644 --- a/tmk_core/common/action.c +++ b/tmk_core/common/action.c @@ -940,18 +940,23 @@ void unregister_code(uint8_t code) { #endif } -/** \brief Utilities for actions. (FIXME: Needs better description) +/** \brief Tap a keycode with a delay. * - * FIXME: Needs documentation. + * \param code The basic keycode to tap. + * \param delay The amount of time in milliseconds to leave the keycode registered, before unregistering it. + */ +void tap_code_delay(uint8_t code, uint16_t delay) { + register_code(code); + wait_ms(delay); + unregister_code(code); +} + +/** \brief Tap a keycode with the default delay. + * + * \param code The basic keycode to tap. If `code` is `KC_CAPS`, the delay will be `TAP_HOLD_CAPS_DELAY`, otherwise `TAP_CODE_DELAY`, if defined. */ void tap_code(uint8_t code) { - register_code(code); - if (code == KC_CAPS) { - wait_ms(TAP_HOLD_CAPS_DELAY); - } else { - wait_ms(TAP_CODE_DELAY); - } - unregister_code(code); + tap_code_delay(code, code == KC_CAPS ? TAP_HOLD_CAPS_DELAY : TAP_CODE_DELAY); } /** \brief Adds the given physically pressed modifiers and sends a keyboard report immediately. diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h index 81cd54369..9a991de1c 100644 --- a/tmk_core/common/action.h +++ b/tmk_core/common/action.h @@ -100,6 +100,7 @@ void process_action(keyrecord_t *record, action_t action); void register_code(uint8_t code); void unregister_code(uint8_t code); void tap_code(uint8_t code); +void tap_code_delay(uint8_t code, uint16_t delay); void register_mods(uint8_t mods); void unregister_mods(uint8_t mods); void register_weak_mods(uint8_t mods);