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.

85 lines
2.6 KiB

  1. # User Space Code for Paul Ewing
  2. This folder contains my user space code.
  3. ## Key Repeater
  4. I've implemented a key repeater utility in [./key_repeater.h](./key_repeater.h)
  5. and [./key_repeater.c](./key_repeater.c) that is similar in concept to the
  6. "rapid fire" feature many game controllers come with. The intent behind this is
  7. that the user can hold down a key and while it is pressed, a keycode will be
  8. repeatedly sent. I have found this useful in certain games and during game
  9. development.
  10. The duration of the key press as well as the time between key presses is
  11. slightly randomized by design. This is to simulate more realistic human
  12. behavior. By setting the minimum and maximum duration fields to the same value
  13. in the configuration, this randomization can be disabled.
  14. **Note:** Please be aware that this might be against the terms of service in
  15. certain games so use your own discretion before using this feature.
  16. ### How to Use
  17. Define the repeater and then configure and allocate it in your keymap's
  18. initialization process:
  19. ```c
  20. static struct key_repeater_t* click_repeater = NULL;
  21. void keyboard_post_init_user(void) {
  22. // Seed the random number generator which is used by the key repeater
  23. srand(timer_read32());
  24. // Configure and instantiate a key repeater for mouse button 1 "rapid fire"
  25. struct key_repeater_config_t cfg = {
  26. .key = KC_BTN1, // Press mouse button 1 (Left click)
  27. .key_duration_min = 20, // Press key for 20 to 50 milliseconds
  28. .key_duration_max = 50,
  29. .wait_duration_min = 90, // Wait for 90 to 140 milliseconds before pressing again
  30. .wait_duration_max = 140,
  31. };
  32. click_repeater = kr_new(&cfg);
  33. }
  34. ```
  35. Make sure the key repeater is polled during matrix scanning:
  36. ```c
  37. void matrix_scan_user(void) {
  38. kr_poll(click_repeater);
  39. }
  40. ```
  41. Define a custom keycode that will enable/disable the repeater:
  42. ```c
  43. enum {
  44. RP_BTN1 = SAFE_RANGE, // Click repeatedly while key is held
  45. };
  46. ```
  47. Assign the keycode to a key in your `LAYOUT(...)` macro.
  48. Define the logic to enable/disable the repeater when the custom keycode is
  49. pressed or released:
  50. ```c
  51. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  52. switch (keycode) {
  53. case RP_BTN1:
  54. if (record->event.pressed) {
  55. kr_enable(click_repeater);
  56. } else {
  57. kr_disable(click_repeater);
  58. }
  59. return false;
  60. default:
  61. return true;
  62. }
  63. }
  64. ```
  65. For a full working example in own of my own keymaps, see:
  66. [keyboards/cozykeys/speedo/v3/keymaps/pcewing/keymap.c](../../keyboards/cozykeys/speedo/v3/keymaps/pcewing/keymap.c)