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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

hook.txt 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Hooks
  2. -----
  3. Hooks allow you to execute custom code at certain predefined points in the firmware execution. To use them, just define the hook function in your keymap file.
  4. The following hooks are available available:
  5. Hook function | Timing
  6. --------------------------------|-----------------------------------------------
  7. `hook_early_init(void)` | Early in the boot process, before the matrix is initialized and before a connection is made with the host. Thus, this hook has access to very few parameters, but it is a good place to define any custom parameters needed by other early processes.
  8. `hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized.
  9. `hook_bootmagic(void)` | During the Boot Magic window, after EEPROM and Bootloader checks are made, but before any other built-in Boot Magic checks are made.
  10. `hook_usb_wakeup(void)` | When the device wakes up from USB suspend state.
  11. `hook_usb_suspend_entry(void)` | When the device enters USB suspend state.
  12. `hook_usb_suspend_loop(void)` | Continuously, while the device is in USB suspend state. *Default action:* power down and periodically check the matrix, causing wakeup if needed.
  13. `hook_keyboard_loop(void)` | Continuously, during the main loop, after the matrix is checked.
  14. `hook_matrix_change(keyevent_t event)` | When a matrix state change is detected, before any other actions are processed.
  15. `hook_layer_change(uint32_t layer_state)` | When any layer is changed.
  16. `hook_default_layer_change(uint32_t default_layer_state)` | When any default layer is changed.
  17. `hook_keyboard_leds_change(uint8_t led_status)` | Whenever a change in the LED status is performed. *Default action:* call `keyboard_set_leds(led_status)`
  18. ### Hooks Examples
  19. You can try these out by copying the code to your keymap file, or any .c file in the Makefile `SRC`.
  20. #### Activate keymap layer 5 on startup
  21. ```C
  22. #include "action_layer.h"
  23. void hook_late_init(void)
  24. {
  25. layer_on(5);
  26. print("Layer 5 enabled!");
  27. }
  28. ```
  29. #### Blink the Caps Lock LED every .5 seconds
  30. ```C
  31. #include "timer.h"
  32. #include "led.h"
  33. bool my_led_status = 0;
  34. uint16_t my_led_timer;
  35. void hook_keyboard_loop(void)
  36. {
  37. // check if we've reached 500 milliseconds yet...
  38. if (timer_elapsed(my_led_timer) > 500)
  39. {
  40. // we've reached 500 milliseconds!
  41. // reset the timer
  42. my_led_timer = timer_read();
  43. // check the current LED state
  44. if (my_led_status)
  45. {
  46. // LED is on, so let's turn it off
  47. led_set(host_keyboard_leds() & ~(1<<USB_LED_CAPS_LOCK));
  48. my_led_status = 0;
  49. }
  50. else
  51. {
  52. // LED is off, so let's turn it on
  53. led_set(host_keyboard_leds() | (1<<USB_LED_CAPS_LOCK));
  54. my_led_status = 1;
  55. }
  56. }
  57. }
  58. ```
  59. #### Flash the Caps Lock LED for 20ms on every keypress
  60. ```C
  61. include "timer.h"
  62. #include "led.h"
  63. bool my_led_status = 0;
  64. uint16_t my_led_timer;
  65. void hook_matrix_change(keyevent_t event)
  66. {
  67. // only flash LED for key press events, not key release events.
  68. if (event.pressed)
  69. {
  70. // check the current LED status and reverse it
  71. led_set(host_keyboard_leds() ^ (1<<USB_LED_CAPS_LOCK));
  72. my_led_status = 1;
  73. my_led_timer = timer_read();
  74. }
  75. }
  76. void hook_keyboard_loop(void)
  77. {
  78. if (my_led_status)
  79. {
  80. // check if we've reached 20 milliseconds yet...
  81. if (timer_elapsed(my_led_timer) > 50)
  82. {
  83. led_set(host_keyboard_leds());
  84. my_led_status = 0;
  85. }
  86. }
  87. }
  88. ```