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

Add tap_code_delay(code, delay) #11913

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/feature_macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,15 @@ Parallel to `register_code` function, this sends the `<kc>` keyup event to the c

### `tap_code(<kc>);`

This will send `register_code(<kc>)` and then `unregister_code(<kc>)`. This is useful if you want to send both the press and release events ("tap" the key, rather than hold it).
Sends `register_code(<kc>)` and then `unregister_code(<kc>)`. 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(<kc>)`. This can be useful when you are having issues with taps (un)registering.

If the keycode is `KC_CAPS`, it waits `CAPS_LOCK_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.
fauxpark marked this conversation as resolved.
Show resolved Hide resolved

### `tap_code_delay(<kc>, <delay>);`

Like `tap_code(<kc>)`, but with a `delay` parameter for specifying arbitrary intervals before sending the unregister event.

### `register_code16(<kc>);`, `unregister_code16(<kc>);` and `tap_code16(<kc>);`

Expand Down
21 changes: 13 additions & 8 deletions tmk_core/common/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,20 +940,25 @@ 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(uint8_t code) {
void tap_code_delay(uint8_t code, uint16_t delay) {
register_code(code);
if (code == KC_CAPS) {
wait_ms(TAP_HOLD_CAPS_DELAY);
} else {
wait_ms(TAP_CODE_DELAY);
}
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) {
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.
*
* \param mods A bitfield of modifiers to register.
Expand Down
1 change: 1 addition & 0 deletions tmk_core/common/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down