diff --git a/docs/config_options.md b/docs/config_options.md index 90a708dd99c2..ab5a58a67fde 100644 --- a/docs/config_options.md +++ b/docs/config_options.md @@ -156,6 +156,8 @@ If you define these options you will enable the associated feature, which may in * See [Retro Tapping](tap_hold#retro-tapping) for details * `#define RETRO_TAPPING_PER_KEY` * enables handling for per key `RETRO_TAPPING` settings +* `#define RETRO_TAPPING_TIMEOUT 500` + * how long until `RETRO_TAPPING` times out * `#define TAPPING_TOGGLE 2` * how many taps before triggering the toggle * `#define PERMISSIVE_HOLD` diff --git a/docs/tap_hold.md b/docs/tap_hold.md index 254d5de5ec1e..fbbf3e76ba7e 100644 --- a/docs/tap_hold.md +++ b/docs/tap_hold.md @@ -631,6 +631,14 @@ bool get_retro_tapping(uint16_t keycode, keyrecord_t *record) { } ``` +With RETRO_TAPPING_TIMEOUT enabled, holding a dual function key for longer than the specified timeout duration will result in no keycode being sent when the key is released. + +This setting can be enabled in your `config.h` and takes an integer that is the timeout duration in milliseconds. + +```c +#define RETRO_TAPPING_TIMEOUT 500 +``` + If the programs you use bind an action to taps of modifier keys (e.g. tapping left GUI to bring up the applications menu or tapping left Alt to focus the menu bar), you may find that using retro-tapping falsely triggers those actions. To counteract this, you can define a `DUMMY_MOD_NEUTRALIZER_KEYCODE` in `config.h` that will get sent in between the register and unregister events of a held mod-tap key. That way, the programs on your computer will no longer interpret the mod suppression induced by retro-tapping as a lone tap of a modifier key and will thus not falsely trigger the undesired action. Naturally, for this technique to be effective, you must choose a `DUMMY_MOD_NEUTRALIZER_KEYCODE` for which no keyboard shortcuts are bound to. Recommended values are: `KC_RIGHT_CTRL` or `KC_F18`. diff --git a/quantum/action.c b/quantum/action.c index be85192d25ab..e9f867539e68 100644 --- a/quantum/action.c +++ b/quantum/action.c @@ -53,6 +53,10 @@ uint16_t retro_tap_curr_key = 0; uint8_t retro_tap_curr_mods = 0; uint8_t retro_tap_next_mods = 0; # endif +# if defined(RETRO_TAPPING_TIMEOUT) && !(defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) +uint16_t retro_last_press_time = 0; +uint16_t retro_last_release_time = 0; +# endif #endif #if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING) @@ -86,8 +90,14 @@ void action_exec(keyevent_t event) { if (event.pressed) { retro_tap_primed = false; retro_tap_curr_key = event_keycode; +# if defined(RETRO_TAPPING_TIMEOUT) && !(defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) + retro_last_press_time = timer_read(); +# endif } else if (retro_tap_curr_key == event_keycode) { retro_tap_primed = true; +# if defined(RETRO_TAPPING_TIMEOUT) && !(defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) + retro_last_release_time = timer_read(); +# endif } #endif } @@ -862,6 +872,9 @@ void process_action(keyrecord_t *record, action_t action) { if ( # ifdef RETRO_TAPPING_PER_KEY get_retro_tapping(event_keycode, record) && +# endif +# if defined(RETRO_TAPPING_TIMEOUT) && !(defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)) + retro_last_release_time - retro_last_press_time < RETRO_TAPPING_TIMEOUT && # endif retro_tap_primed) { # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) diff --git a/tests/tap_hold_configurations/retro_tapping/config.h b/tests/tap_hold_configurations/retro_tapping/config.h index cc9f1624770e..af5c83847e27 100644 --- a/tests/tap_hold_configurations/retro_tapping/config.h +++ b/tests/tap_hold_configurations/retro_tapping/config.h @@ -22,3 +22,4 @@ #define DUMMY_MOD_NEUTRALIZER_KEYCODE KC_RIGHT_CTRL #define MODS_TO_NEUTRALIZE \ { MOD_BIT(KC_LEFT_GUI) } +#define RETRO_TAPPING_TIMEOUT TAPPING_TERM * 2 + 1 diff --git a/tests/tap_hold_configurations/retro_tapping/test_timeout.cpp b/tests/tap_hold_configurations/retro_tapping/test_timeout.cpp new file mode 100644 index 000000000000..03f51abb911b --- /dev/null +++ b/tests/tap_hold_configurations/retro_tapping/test_timeout.cpp @@ -0,0 +1,61 @@ +/* Copyright 2024 John Rigoni + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "keycodes.h" +#include "test_common.hpp" +#include "action_tapping.h" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class RetroTapTimeout : public TestFixture {}; + +TEST_F(RetroTapTimeout, retro_tap_timeout_left_shift) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.press(); + idle_for(RETRO_TAPPING_TIMEOUT); + mod_tap_hold_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); +} + +TEST_F(RetroTapTimeout, retro_tap_timeout_left_mod) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, LGUI_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + EXPECT_REPORT(driver, (KC_LEFT_GUI)); + EXPECT_REPORT(driver, (KC_LGUI, DUMMY_MOD_NEUTRALIZER_KEYCODE)); + EXPECT_REPORT(driver, (KC_LEFT_GUI)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.press(); + idle_for(RETRO_TAPPING_TIMEOUT); + mod_tap_hold_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); +}