You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
17 KiB

  1. # Repeat Key
  2. The Repeat Key performs the action of the last pressed key. Tapping the Repeat
  3. Key after tapping the <kbd>Z</kbd> key types another "`z`." This is useful for
  4. typing doubled letters, like the `z` in "`dazzle`": a double tap on <kbd>Z</kbd>
  5. can instead be a roll from <kbd>Z</kbd> to <kbd>Repeat</kbd>, which is
  6. potentially faster and more comfortable. The Repeat Key is also useful for
  7. hotkeys, like repeating Ctrl + Shift + Right Arrow to select by word.
  8. Repeat Key remembers mods that were active with the last key press. These mods
  9. are combined with any additional mods while pressing the Repeat Key. If the last
  10. press key was <kbd>Ctrl</kbd> + <kbd>Z</kbd>, then <kbd>Shift</kbd> +
  11. <kbd>Repeat</kbd> performs Ctrl + Shift + `Z`.
  12. ## How do I enable Repeat Key
  13. In your `rules.mk`, add:
  14. ```make
  15. REPEAT_KEY_ENABLE = yes
  16. ```
  17. Then pick a key in your keymap and assign it the keycode `QK_REPEAT_KEY` (short
  18. alias `QK_REP`). Optionally, use the keycode `QK_ALT_REPEAT_KEY` (short alias
  19. `QK_AREP`) on another key.
  20. ## Keycodes
  21. |Keycode |Aliases |Description |
  22. |-----------------------|---------|-------------------------------------|
  23. |`QK_REPEAT_KEY` |`QK_REP` |Repeat the last pressed key |
  24. |`QK_ALT_REPEAT_KEY` |`QK_AREP`|Perform alternate of the last key |
  25. ## Alternate Repeating
  26. The Alternate Repeat Key performs the "alternate" action of the last pressed key
  27. if it is defined. By default, Alternate Repeat is defined for navigation keys to
  28. act in the reverse direction. When the last key is the common "select by word"
  29. hotkey Ctrl + Shift + Right Arrow, the Alternate Repeat Key performs Ctrl +
  30. Shift + Left Arrow, which together with the Repeat Key enables convenient
  31. selection by words in either direction.
  32. Alternate Repeat is enabled with the Repeat Key by default. Optionally, to
  33. reduce firmware size, Alternate Repeat may be disabled by adding in config.h:
  34. ```c
  35. #define NO_ALT_REPEAT_KEY
  36. ```
  37. The following alternate keys are defined by default. See
  38. `get_alt_repeat_key_keycode_user()` below for how to change or add to these
  39. definitions. Where it makes sense, these definitions also include combinations
  40. with mods, like Ctrl + Left &harr; Ctrl + Right Arrow.
  41. **Navigation**
  42. |Keycodes |Description |
  43. |-----------------------------------|-----------------------------------|
  44. |`KC_LEFT` &harr; `KC_RGHT` | Left &harr; Right Arrow |
  45. |`KC_UP` &harr; `KC_DOWN` | Up &harr; Down Arrow |
  46. |`KC_HOME` &harr; `KC_END` | Home &harr; End |
  47. |`KC_PGUP` &harr; `KC_PGDN` | Page Up &harr; Page Down |
  48. |`KC_MS_L` &harr; `KC_MS_R` | Mouse Cursor Left &harr; Right |
  49. |`KC_MS_U` &harr; `KC_MS_D` | Mouse Cursor Up &harr; Down |
  50. |`KC_WH_L` &harr; `KC_WH_R` | Mouse Wheel Left &harr; Right |
  51. |`KC_WH_U` &harr; `KC_WH_D` | Mouse Wheel Up &harr; Down |
  52. **Misc**
  53. |Keycodes |Description |
  54. |-----------------------------------|-----------------------------------|
  55. |`KC_BSPC` &harr; `KC_DEL` | Backspace &harr; Delete |
  56. |`KC_LBRC` &harr; `KC_RBRC` | `[` &harr; `]` |
  57. |`KC_LCBR` &harr; `KC_RCBR` | `{` &harr; `}` |
  58. **Media**
  59. |Keycodes |Description |
  60. |-----------------------------------|-----------------------------------|
  61. |`KC_WBAK` &harr; `KC_WFWD` | Browser Back &harr; Forward |
  62. |`KC_MNXT` &harr; `KC_MPRV` | Next &harr; Previous Media Track |
  63. |`KC_MFFD` &harr; `KC_MRWD` | Fast Forward &harr; Rewind Media |
  64. |`KC_VOLU` &harr; `KC_VOLD` | Volume Up &harr; Down |
  65. |`KC_BRIU` &harr; `KC_BRID` | Brightness Up &harr; Down |
  66. **Hotkeys in Vim, Emacs, and other programs**
  67. |Keycodes |Description |
  68. |-----------------------------------|-----------------------------------|
  69. |mod + `KC_F` &harr; mod + `KC_B` | Forward &harr; Backward |
  70. |mod + `KC_D` &harr; mod + `KC_U` | Down &harr; Up |
  71. |mod + `KC_N` &harr; mod + `KC_P` | Next &harr; Previous |
  72. |mod + `KC_A` &harr; mod + `KC_E` | Home &harr; End |
  73. |mod + `KC_O` &harr; mod + `KC_I` | Vim jump list Older &harr; Newer |
  74. |`KC_J` &harr; `KC_K` | Down &harr; Up |
  75. |`KC_H` &harr; `KC_L` | Left &harr; Right |
  76. |`KC_W` &harr; `KC_B` | Forward &harr; Backward by Word |
  77. (where above, "mod" is Ctrl, Alt, or GUI)
  78. ## Defining alternate keys
  79. Use the `get_alt_repeat_key_keycode_user()` callback to define the "alternate"
  80. for additional keys or override the default definitions. For example, to define
  81. Ctrl + Y as the alternate of Ctrl + Z, and vice versa, add the following in
  82. keymap.c:
  83. ```c
  84. uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  85. if ((mods & MOD_MASK_CTRL)) { // Was Ctrl held?
  86. switch (keycode) {
  87. case KC_Y: return C(KC_Z); // Ctrl + Y reverses to Ctrl + Z.
  88. case KC_Z: return C(KC_Y); // Ctrl + Z reverses to Ctrl + Y.
  89. }
  90. }
  91. return KC_TRNS; // Defer to default definitions.
  92. }
  93. ```
  94. The `keycode` and `mods` args are the keycode and mods that were active with the
  95. last pressed key. The meaning of the return value from this function is:
  96. * `KC_NO` &ndash; do nothing (any predefined alternate key is not used);
  97. * `KC_TRNS` &ndash; use the default alternate key if it exists;
  98. * anything else &ndash; use the specified keycode. Any keycode may be returned
  99. as an alternate key, including custom keycodes.
  100. Another example, defining Shift + Tab as the alternate of Tab, and vice versa:
  101. ```c
  102. uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  103. bool shifted = (mods & MOD_MASK_SHIFT); // Was Shift held?
  104. switch (keycode) {
  105. case KC_TAB:
  106. if (shifted) { // If the last key was Shift + Tab,
  107. return KC_TAB; // ... the reverse is Tab.
  108. } else { // Otherwise, the last key was Tab,
  109. return S(KC_TAB); // ... and the reverse is Shift + Tab.
  110. }
  111. }
  112. return KC_TRNS;
  113. }
  114. ```
  115. #### Eliminating SFBs
  116. Alternate Repeat can be configured more generally to perform an action that
  117. "complements" the last key. Alternate Repeat is not limited to reverse
  118. repeating, and it need not be symmetric. You can use it to eliminate cases of
  119. same-finger bigrams in your layout, that is, pairs of letters typed by the same
  120. finger. The following addresses the top 5 same-finger bigrams in English on
  121. QWERTY, so that for instance "`ed`" may be typed as <kbd>E</kbd>, <kbd>Alt
  122. Repeat</kbd>.
  123. ```c
  124. uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  125. switch (keycode) {
  126. case KC_E: return KC_D; // For "ED" bigram.
  127. case KC_D: return KC_E; // For "DE" bigram.
  128. case KC_C: return KC_E; // For "CE" bigram.
  129. case KC_L: return KC_O; // For "LO" bigram.
  130. case KC_U: return KC_N; // For "UN" bigram.
  131. }
  132. return KC_TRNS;
  133. }
  134. ```
  135. #### Typing shortcuts
  136. A useful possibility is having Alternate Repeat press [a
  137. macro](feature_macros.md). This way macros can be used without having to
  138. dedicate keys to them. The following defines a couple shortcuts.
  139. * Typing <kbd>K</kbd>, <kbd>Alt Repeat</kbd> produces "`keyboard`," with the
  140. initial "`k`" typed as usual and the "`eybord`" produced by the macro.
  141. * Typing <kbd>.</kbd>, <kbd>Alt Repeat</kbd> produces "`../`," handy for "up
  142. directory" on the shell. Similary, <kbd>.</kbd> types the initial "`.`" and
  143. "`./`" is produced by the macro.
  144. ```c
  145. enum custom_keycodes {
  146. M_KEYBOARD = SAFE_RANGE,
  147. M_UPDIR,
  148. // Other custom keys...
  149. };
  150. uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  151. switch (keycode) {
  152. case KC_K: return M_KEYBOARD;
  153. case KC_DOT: return M_UPDIR;
  154. }
  155. return KC_TRNS;
  156. }
  157. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  158. switch (keycode) {
  159. case M_KEYBOARD: SEND_STRING(/*k*/"eyboard"); break;
  160. case M_UPDIR: SEND_STRING(/*.*/"./"); break;
  161. }
  162. return true;
  163. }
  164. ```
  165. ## Ignoring certain keys and mods
  166. In tracking what is "the last key" to be repeated or alternate repeated,
  167. modifier and layer switch keys are always ignored. This makes it possible to set
  168. some mods and change layers between pressing a key and repeating it. By default,
  169. all other (non-modifier, non-layer switch) keys are remembered so that they are
  170. eligible for repeating. To configure additional keys to be ignored, define
  171. `remember_last_key_user()` in your keymap.c.
  172. #### Ignoring a key
  173. The following ignores the Backspace key:
  174. ```c
  175. bool remember_last_key_user(uint16_t keycode, keyrecord_t* record,
  176. uint8_t* remembered_mods) {
  177. switch (keycode) {
  178. case KC_BSPC:
  179. return false; // Ignore backspace.
  180. }
  181. return true; // Other keys can be repeated.
  182. }
  183. ```
  184. Then for instance, the Repeat key in <kbd>Left Arrow</kbd>,
  185. <kbd>Backspace</kbd>, <kbd>Repeat</kbd> sends Left Arrow again instead of
  186. repeating Backspace.
  187. The `remember_last_key_user()` callback is called on every key press excluding
  188. modifiers and layer switches. Returning true indicates the key is remembered,
  189. while false means it is ignored.
  190. #### Filtering remembered mods
  191. The `remembered_mods` arg represents the mods that will be remembered with
  192. this key. It can be modified to forget certain mods. This may be
  193. useful to forget capitalization when repeating shifted letters, so that "Aaron"
  194. does not becom "AAron":
  195. ```c
  196. bool remember_last_key_user(uint16_t keycode, keyrecord_t* record,
  197. uint8_t* remembered_mods) {
  198. // Forget Shift on letter keys when Shift or AltGr are the only mods.
  199. switch (keycode) {
  200. case KC_A ... KC_Z:
  201. if ((*remembered_mods & ~(MOD_MASK_SHIFT | MOD_BIT(KC_RALT))) == 0) {
  202. *remembered_mods &= ~MOD_MASK_SHIFT;
  203. }
  204. break;
  205. }
  206. return true;
  207. }
  208. ```
  209. #### Further conditions
  210. Besides checking the keycode, this callback could also make conditions based on
  211. the current layer state (with `IS_LAYER_ON(layer)`) or mods (`get_mods()`). For
  212. example, the following ignores keys on layer 2 as well as key combinations
  213. involving GUI:
  214. ```c
  215. bool remember_last_key_user(uint16_t keycode, keyrecord_t* record,
  216. uint8_t* remembered_mods) {
  217. if (IS_LAYER_ON(2) || (get_mods() & MOD_MASK_GUI)) {
  218. return false; // Ignore layer 2 keys and GUI chords.
  219. }
  220. return true; // Other keys can be repeated.
  221. }
  222. ```
  223. ?> See [Layer Functions](feature_layers.md#functions) and [Checking Modifier
  224. State](feature_advanced_keycodes.md#checking-modifier-state) for further
  225. details.
  226. ## Handle how a key is repeated
  227. By default, pressing the Repeat Key will simply behave as if the last key
  228. were pressed again. This also works with macro keys with custom handlers,
  229. invoking the macro again. In case fine-tuning is needed for sensible repetition,
  230. you can handle how a key is repeated with `get_repeat_key_count()` within
  231. `process_record_user()`.
  232. The `get_repeat_key_count()` function returns a signed count of times the key
  233. has been repeated or alternate repeated. When a key is pressed as usual,
  234. `get_repeat_key_count()` is 0. On the first repeat, it is 1, then the second
  235. repeat, 2, and so on. Negative counts are used similarly for alternate
  236. repeating. For instance supposing `MY_MACRO` is a custom keycode used in the
  237. layout:
  238. ```c
  239. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  240. switch (keycode) {
  241. case MY_MACRO:
  242. if (get_repeat_key_count() > 0) {
  243. // MY_MACRO is being repeated!
  244. if (record->event.pressed) {
  245. SEND_STRING("repeat!");
  246. }
  247. } else {
  248. // MY_MACRO is being used normally.
  249. if (record->event.pressed) {
  250. SEND_STRING("macro");
  251. }
  252. }
  253. return false;
  254. // Other macros...
  255. }
  256. return true;
  257. }
  258. ```
  259. ## Handle how a key is alternate repeated
  260. Pressing the Alternate Repeat Key behaves as if the "alternate" of the last
  261. pressed key were pressed, if an alternate is defined. To define how a particular
  262. key is alternate repeated, use the `get_alt_repeat_key_keycode_user()` callback
  263. as described above to define which keycode to use as its alternate. Beyond this,
  264. `get_repeat_key_count()` may be used in custom handlers to fine-tune behavior
  265. when alternate repeating.
  266. The following example defines `MY_MACRO` as its own alternate, and specially
  267. handles repeating and alternate repeating:
  268. ```c
  269. uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  270. switch (keycode) {
  271. case MY_MACRO: return MY_MACRO; // MY_MACRO is its own alternate.
  272. }
  273. return KC_TRNS;
  274. }
  275. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  276. switch (keycode) {
  277. case MY_MACRO:
  278. if (get_repeat_key_count() > 0) { // Repeating.
  279. if (record->event.pressed) {
  280. SEND_STRING("repeat!");
  281. }
  282. } else if (get_repeat_key_count() < 0) { // Alternate repeating.
  283. if (record->event.pressed) {
  284. SEND_STRING("alt repeat!");
  285. }
  286. } else { // Used normally.
  287. if (record->event.pressed) {
  288. SEND_STRING("macro");
  289. }
  290. }
  291. return false;
  292. // Other macros...
  293. }
  294. return true;
  295. }
  296. ```
  297. ## Functions
  298. | Function | Description |
  299. |--------------------------------|------------------------------------------------------------------------|
  300. | `get_last_keycode()` | The last key's keycode, the key to be repeated. |
  301. | `get_last_mods()` | Mods to apply when repeating. |
  302. | `set_last_keycode(kc)` | Set the keycode to be repeated. |
  303. | `set_last_mods(mods)` | Set the mods to apply when repeating. |
  304. | `get_repeat_key_count()` | Signed count of times the key has been repeated or alternate repeated. |
  305. | `get_alt_repeat_key_keycode()` | Keycode to be used for alternate repeating. |
  306. ## Additional "Alternate" keys
  307. By leveraging `get_last_keycode()` in macros, it is possible to define
  308. additional, distinct "Alternate Repeat"-like keys. The following defines two
  309. keys `ALTREP2` and `ALTREP3` and implements ten shortcuts with them for common
  310. English 5-gram letter patterns, taking inspiration from
  311. [Stenotype](feature_stenography.md):
  312. | Typing | Produces | Typing | Produces |
  313. |----------------------------------|----------|----------------------------------|----------|
  314. | <kbd>A</kbd>, <kbd>ALTREP2</kbd> | `ation` | <kbd>A</kbd>, <kbd>ALTREP3</kbd> | `about` |
  315. | <kbd>I</kbd>, <kbd>ALTREP2</kbd> | `ition` | <kbd>I</kbd>, <kbd>ALTREP3</kbd> | `inter` |
  316. | <kbd>S</kbd>, <kbd>ALTREP2</kbd> | `ssion` | <kbd>S</kbd>, <kbd>ALTREP3</kbd> | `state` |
  317. | <kbd>T</kbd>, <kbd>ALTREP2</kbd> | `their` | <kbd>T</kbd>, <kbd>ALTREP3</kbd> | `there` |
  318. | <kbd>W</kbd>, <kbd>ALTREP2</kbd> | `which` | <kbd>W</kbd>, <kbd>ALTREP3</kbd> | `would` |
  319. ```c
  320. enum custom_keycodes {
  321. ALTREP2 = SAFE_RANGE,
  322. ALTREP3,
  323. };
  324. // Use ALTREP2 and ALTREP3 in your layout...
  325. bool remember_last_key_user(uint16_t keycode, keyrecord_t* record,
  326. uint8_t* remembered_mods) {
  327. switch (keycode) {
  328. case ALTREP2:
  329. case ALTREP3:
  330. return false; // Ignore ALTREP keys.
  331. }
  332. return true; // Other keys can be repeated.
  333. }
  334. static void process_altrep2(uint16_t keycode, uint8_t mods) {
  335. switch (keycode) {
  336. case KC_A: SEND_STRING(/*a*/"tion"); break;
  337. case KC_I: SEND_STRING(/*i*/"tion"); break;
  338. case KC_S: SEND_STRING(/*s*/"sion"); break;
  339. case KC_T: SEND_STRING(/*t*/"heir"); break;
  340. case KC_W: SEND_STRING(/*w*/"hich"); break;
  341. }
  342. }
  343. static void process_altrep3(uint16_t keycode, uint8_t mods) {
  344. switch (keycode) {
  345. case KC_A: SEND_STRING(/*a*/"bout"); break;
  346. case KC_I: SEND_STRING(/*i*/"nter"); break;
  347. case KC_S: SEND_STRING(/*s*/"tate"); break;
  348. case KC_T: SEND_STRING(/*t*/"here"); break;
  349. case KC_W: SEND_STRING(/*w*/"ould"); break;
  350. }
  351. }
  352. bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  353. switch (keycode) {
  354. case ALTREP2:
  355. if (record->event.pressed) {
  356. process_altrep2(get_last_keycode(), get_last_mods());
  357. }
  358. return false;
  359. case ALTREP3:
  360. if (record->event.pressed) {
  361. process_altrep3(get_last_keycode(), get_last_mods());
  362. }
  363. return false;
  364. }
  365. return true;
  366. }
  367. ```