summaryrefslogtreecommitdiffstats
path: root/quantum
diff options
context:
space:
mode:
authorPascal Getreuer <50221757+getreuer@users.noreply.github.com>2023-05-20 05:35:06 -0700
committerGitHub <noreply@github.com>2023-05-20 22:35:06 +1000
commit3993b15f054265071730cdb450f43457dcf4c64a (patch)
tree61c5b980ed14428bae3c0278c27937dbb5d33627 /quantum
parente1766df185869d8a591228d37f3f7b6d5b4049b4 (diff)
[Core] Add Repeat Key ("repeat last key") as a core feature. (#19700)
Co-authored-by: casuanoob <96005765+casuanoob@users.noreply.github.com> Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
Diffstat (limited to 'quantum')
-rw-r--r--quantum/action.c4
-rw-r--r--quantum/action.h2
-rw-r--r--quantum/keycodes.h6
-rw-r--r--quantum/process_keycode/process_repeat_key.c109
-rw-r--r--quantum/process_keycode/process_repeat_key.h62
-rw-r--r--quantum/quantum.c5
-rw-r--r--quantum/quantum.h5
-rw-r--r--quantum/repeat_key.c282
-rw-r--r--quantum/repeat_key.h80
9 files changed, 550 insertions, 5 deletions
diff --git a/quantum/action.c b/quantum/action.c
index 59bfefc495..a45e70c557 100644
--- a/quantum/action.c
+++ b/quantum/action.c
@@ -285,7 +285,7 @@ void process_record(keyrecord_t *record) {
}
void process_record_handler(keyrecord_t *record) {
-#ifdef COMBO_ENABLE
+#if defined(COMBO_ENABLE) || defined(REPEAT_KEY_ENABLE)
action_t action;
if (record->keycode) {
action = action_for_keycode(record->keycode);
@@ -1109,7 +1109,7 @@ bool is_tap_record(keyrecord_t *record) {
return false;
}
-#ifdef COMBO_ENABLE
+#if defined(COMBO_ENABLE) || defined(REPEAT_KEY_ENABLE)
action_t action;
if (record->keycode) {
action = action_for_keycode(record->keycode);
diff --git a/quantum/action.h b/quantum/action.h
index 2a2c294c5a..d5b15c6f17 100644
--- a/quantum/action.h
+++ b/quantum/action.h
@@ -50,7 +50,7 @@ typedef struct {
#ifndef NO_ACTION_TAPPING
tap_t tap;
#endif
-#ifdef COMBO_ENABLE
+#if defined(COMBO_ENABLE) || defined(REPEAT_KEY_ENABLE)
uint16_t keycode;
#endif
} keyrecord_t;
diff --git a/quantum/keycodes.h b/quantum/keycodes.h
index 34b13c29af..bbf10da36d 100644
--- a/quantum/keycodes.h
+++ b/quantum/keycodes.h
@@ -721,6 +721,8 @@ enum qk_keycode_defines {
QK_AUTOCORRECT_TOGGLE = 0x7C76,
QK_TRI_LAYER_LOWER = 0x7C77,
QK_TRI_LAYER_UPPER = 0x7C78,
+ QK_REPEAT_KEY = 0x7C79,
+ QK_ALT_REPEAT_KEY = 0x7C7A,
QK_KB_0 = 0x7E00,
QK_KB_1 = 0x7E01,
QK_KB_2 = 0x7E02,
@@ -1362,6 +1364,8 @@ enum qk_keycode_defines {
AC_TOGG = QK_AUTOCORRECT_TOGGLE,
TL_LOWR = QK_TRI_LAYER_LOWER,
TL_UPPR = QK_TRI_LAYER_UPPER,
+ QK_REP = QK_REPEAT_KEY,
+ QK_AREP = QK_ALT_REPEAT_KEY,
};
// Range Helpers
@@ -1413,6 +1417,6 @@ enum qk_keycode_defines {
#define IS_MACRO_KEYCODE(code) ((code) >= QK_MACRO_0 && (code) <= QK_MACRO_31)
#define IS_BACKLIGHT_KEYCODE(code) ((code) >= QK_BACKLIGHT_ON && (code) <= QK_BACKLIGHT_TOGGLE_BREATHING)
#define IS_RGB_KEYCODE(code) ((code) >= RGB_TOG && (code) <= RGB_MODE_TWINKLE)
-#define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_TRI_LAYER_UPPER)
+#define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_ALT_REPEAT_KEY)
#define IS_KB_KEYCODE(code) ((code) >= QK_KB_0 && (code) <= QK_KB_31)
#define IS_USER_KEYCODE(code) ((code) >= QK_USER_0 && (code) <= QK_USER_31)
diff --git a/quantum/process_keycode/process_repeat_key.c b/quantum/process_keycode/process_repeat_key.c
new file mode 100644
index 0000000000..f819aa226e
--- /dev/null
+++ b/quantum/process_keycode/process_repeat_key.c
@@ -0,0 +1,109 @@
+// Copyright 2022-2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "process_repeat_key.h"
+
+// Default implementation of remember_last_key_user().
+__attribute__((weak)) bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
+ return true;
+}
+
+static bool remember_last_key(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
+ switch (keycode) {
+ // Ignore MO, TO, TG, TT, and TL layer switch keys.
+ case QK_MOMENTARY ... QK_MOMENTARY_MAX:
+ case QK_TO ... QK_TO_MAX:
+ case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
+ case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
+ // Ignore mod keys.
+ case KC_LCTL ... KC_RGUI:
+ case KC_HYPR:
+ case KC_MEH:
+#ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
+ case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
+ case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
+#endif // NO_ACTION_ONESHOT
+#ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
+ case QK_TRI_LAYER_LOWER:
+ case QK_TRI_LAYER_UPPER:
+#endif // TRI_LAYER_ENABLE
+ return false;
+
+ // Ignore hold events on tap-hold keys.
+#ifndef NO_ACTION_TAPPING
+ case QK_MOD_TAP ... QK_MOD_TAP_MAX:
+# ifndef NO_ACTION_LAYER
+ case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
+# endif // NO_ACTION_LAYER
+ if (record->tap.count == 0) {
+ return false;
+ }
+ break;
+#endif // NO_ACTION_TAPPING
+
+#ifdef SWAP_HANDS_ENABLE
+ case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
+ if (IS_SWAP_HANDS_KEYCODE(keycode) || record->tap.count == 0) {
+ return false;
+ }
+ break;
+#endif // SWAP_HANDS_ENABLE
+
+ case QK_REPEAT_KEY:
+#ifndef NO_ALT_REPEAT_KEY
+ case QK_ALT_REPEAT_KEY:
+#endif // NO_ALT_REPEAT_KEY
+ return false;
+ }
+
+ return remember_last_key_user(keycode, record, remembered_mods);
+}
+
+bool process_last_key(uint16_t keycode, keyrecord_t* record) {
+ if (get_repeat_key_count()) {
+ return true;
+ }
+
+ if (record->event.pressed) {
+ uint8_t remembered_mods = get_mods() | get_weak_mods();
+#ifndef NO_ACTION_ONESHOT
+ remembered_mods |= get_oneshot_mods();
+#endif // NO_ACTION_ONESHOT
+
+ if (remember_last_key(keycode, record, &remembered_mods)) {
+ set_last_record(keycode, record);
+ set_last_mods(remembered_mods);
+ }
+ }
+
+ return true;
+}
+
+bool process_repeat_key(uint16_t keycode, keyrecord_t* record) {
+ if (get_repeat_key_count()) {
+ return true;
+ }
+
+ if (keycode == QK_REPEAT_KEY) {
+ repeat_key_invoke(&record->event);
+ return false;
+#ifndef NO_ALT_REPEAT_KEY
+ } else if (keycode == QK_ALT_REPEAT_KEY) {
+ alt_repeat_key_invoke(&record->event);
+ return false;
+#endif // NO_ALT_REPEAT_KEY
+ }
+
+ return true;
+}
diff --git a/quantum/process_keycode/process_repeat_key.h b/quantum/process_keycode/process_repeat_key.h
new file mode 100644
index 0000000000..eddc50f254
--- /dev/null
+++ b/quantum/process_keycode/process_repeat_key.h
@@ -0,0 +1,62 @@
+// Copyright 2022-2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include "quantum.h"
+
+/**
+ * @brief Process handler for remembering the last key.
+ *
+ * @param keycode Keycode registered by matrix press, per keymap
+ * @param record keyrecord_t structure
+ * @return true Continue processing keycodes, and send to host
+ * @return false Stop processing keycodes, and don't send to host
+ */
+bool process_last_key(uint16_t keycode, keyrecord_t* record);
+
+/**
+ * @brief Optional callback defining which keys are remembered.
+ *
+ * @param keycode Keycode that was just pressed
+ * @param record keyrecord_t structure
+ * @param remembered_mods Mods that will be remembered with this key
+ * @return true Key is remembered
+ * @return false Key is ignored
+ *
+ * Modifier and layer switch keys are always ignored. For all other keys, this
+ * callback is called on every key press. Returning true means that the key is
+ * remembered, false means it is ignored. By default, all non-modifier,
+ * non-layer switch keys are remembered.
+ *
+ * The `remembered_mods` arg represents the mods that will be remembered with
+ * this key. It can be modified to forget certain mods, for instance to forget
+ * capitalization when repeating shifted letters:
+ *
+ * // Forget Shift on letter keys.
+ * if (KC_A <= keycode && keycode <= KC_Z && (*remembered_mods & ~MOD_MASK_SHIFT) == 0) {
+ * *remembered_mods = 0;
+ * }
+ */
+bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods);
+
+/**
+ * @brief Process handler for Repeat Key feature.
+ *
+ * @param keycode Keycode registered by matrix press, per keymap
+ * @param record keyrecord_t structure
+ * @return true Continue processing keycodes, and send to host
+ * @return false Stop processing keycodes, and don't send to host
+ */
+bool process_repeat_key(uint16_t keycode, keyrecord_t* record);
diff --git a/quantum/quantum.c b/quantum/quantum.c
index fdc24fa2d0..091cf298f7 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -176,7 +176,7 @@ void soft_reset_keyboard(void) {
/* Convert record into usable keycode via the contained event. */
uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
-#ifdef COMBO_ENABLE
+#if defined(COMBO_ENABLE) || defined(REPEAT_KEY_ENABLE)
if (record->keycode) {
return record->keycode;
}
@@ -273,6 +273,9 @@ bool process_record_quantum(keyrecord_t *record) {
// Must run asap to ensure all keypresses are recorded.
process_dynamic_macro(keycode, record) &&
#endif
+#ifdef REPEAT_KEY_ENABLE
+ process_last_key(keycode, record) && process_repeat_key(keycode, record) &&
+#endif
#if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
process_clicky(keycode, record) &&
#endif
diff --git a/quantum/quantum.h b/quantum/quantum.h
index fec92a5244..31a1a63a7a 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -251,6 +251,11 @@ extern layer_state_t layer_state;
# include "process_tri_layer.h"
#endif
+#ifdef REPEAT_KEY_ENABLE
+# include "repeat_key.h"
+# include "process_repeat_key.h"
+#endif
+
void set_single_persistent_default_layer(uint8_t default_layer);
#define IS_LAYER_ON(layer) layer_state_is(layer)
diff --git a/quantum/repeat_key.c b/quantum/repeat_key.c
new file mode 100644
index 0000000000..0689c6d454
--- /dev/null
+++ b/quantum/repeat_key.c
@@ -0,0 +1,282 @@
+// Copyright 2022-2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "repeat_key.h"
+
+// Variables saving the state of the last key press.
+static keyrecord_t last_record = {0};
+static uint8_t last_mods = 0;
+// Signed count of the number of times the last key has been repeated or
+// alternate repeated: it is 0 when a key is pressed normally, positive when
+// repeated, and negative when alternate repeated.
+static int8_t last_repeat_count = 0;
+// The repeat_count, but set to 0 outside of repeat_key_invoke() so that it is
+// nonzero only while a repeated key is being processed.
+static int8_t processing_repeat_count = 0;
+
+uint16_t get_last_keycode(void) {
+ return last_record.keycode;
+}
+
+uint8_t get_last_mods(void) {
+ return last_mods;
+}
+
+void set_last_keycode(uint16_t keycode) {
+ set_last_record(keycode, &(keyrecord_t){
+#ifndef NO_ACTION_TAPPING
+ .tap.interrupted = false,
+ .tap.count = 1,
+#endif
+ });
+}
+
+void set_last_mods(uint8_t mods) {
+ last_mods = mods;
+}
+
+void set_last_record(uint16_t keycode, keyrecord_t* record) {
+ last_record = *record;
+ last_record.keycode = keycode;
+ last_repeat_count = 0;
+}
+
+/** @brief Updates `last_repeat_count` in direction `dir`. */
+static void update_last_repeat_count(int8_t dir) {
+ if (dir * last_repeat_count < 0) {
+ last_repeat_count = dir;
+ } else if (dir * last_repeat_count < 127) {
+ last_repeat_count += dir;
+ }
+}
+
+int8_t get_repeat_key_count(void) {
+ return processing_repeat_count;
+}
+
+void repeat_key_invoke(const keyevent_t* event) {
+ // It is possible (e.g. in rolled presses) that the last key changes while
+ // the Repeat Key is pressed. To prevent stuck keys, it is important to
+ // remember separately what key record was processed on press so that the
+ // the corresponding record is generated on release.
+ static keyrecord_t registered_record = {0};
+ static int8_t registered_repeat_count = 0;
+ // Since this function calls process_record(), it may recursively call
+ // itself. We return early if `processing_repeat_count` is nonzero to
+ // prevent infinite recursion.
+ if (processing_repeat_count || !last_record.keycode) {
+ return;
+ }
+
+ if (event->pressed) {
+ update_last_repeat_count(1);
+ // On press, apply the last mods state, stacking on top of current mods.
+ register_weak_mods(last_mods);
+ registered_record = last_record;
+ registered_repeat_count = last_repeat_count;
+ }
+
+ // Generate a keyrecord and plumb it into the event pipeline.
+ registered_record.event = *event;
+ processing_repeat_count = registered_repeat_count;
+ process_record(&registered_record);
+ processing_repeat_count = 0;
+
+ // On release, restore the mods state.
+ if (!event->pressed) {
+ unregister_weak_mods(last_mods);
+ }
+}
+
+#ifndef NO_ALT_REPEAT_KEY
+/**
+ * @brief Find alternate keycode from a table of opposing keycode pairs.
+ * @param table Array of pairs of basic keycodes, declared as PROGMEM.
+ * @param table_size_bytes The size of the table in bytes.
+ * @param target The basic keycode to find.
+ * @return The alternate basic keycode, or KC_NO if none was found.
+ *
+ * @note The table keycodes and target must be basic keycodes.
+ *
+ * This helper is used several times below to define alternate keys. Given a
+ * table of pairs of basic keycodes, the function finds the pair containing
+ * `target` and returns the other keycode in the pair.
+ */
+static uint8_t find_alt_keycode(const uint8_t (*table)[2], uint8_t table_size_bytes, uint8_t target) {
+ const uint8_t* keycodes = (const uint8_t*)table;
+ for (uint8_t i = 0; i < table_size_bytes; ++i) {
+ if (target == pgm_read_byte(keycodes + i)) {
+ // Xor (i ^ 1) the index to get the other element in the pair.
+ return pgm_read_byte(keycodes + (i ^ 1));
+ }
+ }
+ return KC_NO;
+}
+
+uint16_t get_alt_repeat_key_keycode(void) {
+ uint16_t keycode = last_record.keycode;
+ uint8_t mods = last_mods;
+
+ // Call the user callback first to give it a chance to override the default
+ // alternate key definitions that follow.
+ uint16_t alt_keycode = get_alt_repeat_key_keycode_user(keycode, mods);
+
+ if (alt_keycode != KC_TRANSPARENT) {
+ return alt_keycode;
+ }
+
+ // Convert 8-bit mods to the 5-bit format used in keycodes. This is lossy:
+ // if left and right handed mods were mixed, they all become right handed.
+ mods = ((mods & 0xf0) ? /* set right hand bit */ 0x10 : 0)
+ // Combine right and left hand mods.
+ | (((mods >> 4) | mods) & 0xf);
+
+ switch (keycode) {
+ case QK_MODS ... QK_MODS_MAX: // Unpack modifier + basic key.
+ mods |= QK_MODS_GET_MODS(keycode);
+ keycode = QK_MODS_GET_BASIC_KEYCODE(keycode);
+ break;
+
+# ifndef NO_ACTION_TAPPING
+ case QK_MOD_TAP ... QK_MOD_TAP_MAX:
+ keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
+ break;
+# ifndef NO_ACTION_LAYER
+ case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
+ keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
+ break;
+# endif // NO_ACTION_LAYER
+# endif // NO_ACTION_TAPPING
+
+# ifdef SWAP_HANDS_ENABLE
+ case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
+ if (IS_SWAP_HANDS_KEYCODE(keycode)) {
+ return KC_NO;
+ }
+ keycode = QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode);
+ break;
+# endif // SWAP_HANDS_ENABLE
+ }
+
+ if (IS_QK_BASIC(keycode)) {
+ if ((mods & (MOD_LCTL | MOD_LALT | MOD_LGUI))) {
+ // The last key was pressed with a modifier other than Shift.
+ // The following maps
+ // mod + F <-> mod + B
+ // and a few others, supporting several core hotkeys used in
+ // Emacs, Vim, less, and other programs.
+ // clang-format off
+ static const uint8_t pairs[][2] PROGMEM = {
+ {KC_F , KC_B }, // Forward / Backward.
+ {KC_D , KC_U }, // Down / Up.
+ {KC_N , KC_P }, // Next / Previous.
+ {KC_A , KC_E }, // Home / End.
+ {KC_O , KC_I }, // Older / Newer in Vim jump list.
+ };
+ // clang-format on
+ alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
+ } else {
+ // The last key was pressed with no mods or only Shift. The
+ // following map a few more Vim hotkeys.
+ // clang-format off
+ static const uint8_t pairs[][2] PROGMEM = {
+ {KC_J , KC_K }, // Down / Up.
+ {KC_H , KC_L }, // Left / Right.
+ // These two lines map W and E to B, and B to W.
+ {KC_W , KC_B }, // Forward / Backward by word.
+ {KC_E , KC_B }, // Forward / Backward by word.
+ };
+ // clang-format on
+ alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
+ }
+
+ if (!alt_keycode) {
+ // The following key pairs are considered with any mods.
+ // clang-format off
+ static const uint8_t pairs[][2] PROGMEM = {
+ {KC_LEFT, KC_RGHT}, // Left / Right Arrow.
+ {KC_UP , KC_DOWN}, // Up / Down Arrow.
+ {KC_HOME, KC_END }, // Home / End.
+ {KC_PGUP, KC_PGDN}, // Page Up / Page Down.
+ {KC_BSPC, KC_DEL }, // Backspace / Delete.
+ {KC_LBRC, KC_RBRC}, // Brackets [ ] and { }.
+#ifdef EXTRAKEY_ENABLE
+ {KC_WBAK, KC_WFWD}, // Browser Back / Forward.
+ {KC_MNXT, KC_MPRV}, // Next / Previous Media Track.
+ {KC_MFFD, KC_MRWD}, // Fast Forward / Rewind Media.
+ {KC_VOLU, KC_VOLD}, // Volume Up / Down.
+ {KC_BRIU, KC_BRID}, // Brightness Up / Down.
+#endif // EXTRAKEY_ENABLE
+#ifdef MOUSEKEY_ENABLE
+ {KC_MS_L, KC_MS_R}, // Mouse Cursor Left / Right.
+ {KC_MS_U, KC_MS_D}, // Mouse Cursor Up / Down.
+ {KC_WH_L, KC_WH_R}, // Mouse Wheel Left / Right.
+ {KC_WH_U, KC_WH_D}, // Mouse Wheel Up / Down.
+#endif // MOUSEKEY_ENABLE
+ };
+ // clang-format on
+ alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
+ }
+
+ if (alt_keycode) {
+ // Combine basic keycode with mods.
+ return (mods << 8) | alt_keycode;
+ }
+ }
+
+ return KC_NO; // No alternate key found.
+}
+
+void alt_repeat_key_invoke(const keyevent_t* event) {
+ static keyrecord_t registered_record = {0};
+ static int8_t registered_repeat_count = 0;
+ // Since this function calls process_record(), it may recursively call
+ // itself. We return early if `processing_repeat_count` is nonzero to
+ // prevent infinite recursion.
+ if (processing_repeat_count) {
+ return;
+ }
+
+ if (event->pressed) {
+ registered_record = (keyrecord_t){
+# ifndef NO_ACTION_TAPPING
+ .tap.interrupted = false,
+ .tap.count = 0,
+# endif
+ .keycode = get_alt_repeat_key_keycode(),
+ };
+ }
+
+ // Early return if there is no alternate key defined.
+ if (!registered_record.keycode) {
+ return;
+ }
+
+ if (event->pressed) {
+ update_last_repeat_count(-1);
+ registered_repeat_count = last_repeat_count;
+ }
+
+ // Generate a keyrecord and plumb it into the event pipeline.
+ registered_record.event = *event;
+ processing_repeat_count = registered_repeat_count;
+ process_record(&registered_record);
+ processing_repeat_count = 0;
+}
+
+// Default implementation of get_alt_repeat_key_keycode_user().
+__attribute__((weak)) uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
+ return KC_TRANSPARENT;
+}
+#endif // NO_ALT_REPEAT_KEY
diff --git a/quantum/repeat_key.h b/quantum/repeat_key.h
new file mode 100644
index 0000000000..06e8364529
--- /dev/null
+++ b/quantum/repeat_key.h
@@ -0,0 +1,80 @@
+// Copyright 2022-2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include "quantum.h"
+
+uint16_t get_last_keycode(void); /**< Keycode of the last key. */
+uint8_t get_last_mods(void); /**< Mods active with the last key. */
+void set_last_keycode(uint16_t keycode); /**< Sets the last key. */
+void set_last_mods(uint8_t mods); /**< Sets the last mods. */
+
+/** @brief Gets the record for the last key. */
+keyrecord_t* get_last_record(void);
+
+/** @brief Sets keycode and record info for the last key. */
+void set_last_record(uint16_t keycode, keyrecord_t* record);
+
+/**
+ * @brief Signed count of times the key has been repeated or alternate repeated.
+ *
+ * @note The count is nonzero only while a repeated or alternate-repeated key is
+ * being processed.
+ *
+ * When a key is pressed normally, the count is 0. When the Repeat Key is used
+ * to repeat a key, the count is 1 on the first repeat, 2 on the second repeat,
+ * and continuing up to 127.
+ *
+ * Negative counts are used similarly for alternate repeating. When the
+ * Alternate Repeat Key is used, the count is -1 on the first alternate repeat,
+ * -2 on the second, continuing down to -127.
+ */
+int8_t get_repeat_key_count(void);
+
+/**
+ * @brief Calls `process_record()` on a generated record repeating the last key.
+ * @param event Event information in the generated record.
+ */
+void repeat_key_invoke(const keyevent_t* event);
+
+#ifndef NO_ALT_REPEAT_KEY
+
+/**
+ * @brief Keycode to be used for alternate repeating.
+ *
+ * Alternate Repeat performs this keycode based on the last eligible pressed key
+ * and mods, get_last_keycode() and get_last_mods(). For example, when the last
+ * key was KC_UP, this function returns KC_DOWN. The function returns KC_NO if
+ * the last key doesn't have a defined alternate.
+ */
+uint16_t get_alt_repeat_key_keycode(void);
+
+/**
+ * @brief Calls `process_record()` to alternate repeat the last key.
+ * @param event Event information in the generated record.
+ */
+void alt_repeat_key_invoke(const keyevent_t* event);
+
+/**
+ * @brief Optional user callback to define additional alternate keys.
+ *
+ * When `get_alt_repeat_key_keycode()` is called, it first calls this callback.
+ * It should return a keycode representing the "alternate" of the given keycode
+ * and mods. Returning KC_NO defers to the default definitions in
+ * `get_alt_repeat_key_keycode()`.
+ */
+uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods);
+
+#endif // NO_ALT_REPEAT_KEY