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.
5 The following hooks are available available:
8 --------------------------------|-----------------------------------------------
9 `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.
10 `hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized.
11 `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.
12 `hook_usb_wakeup(void)` | When the device wakes up from USB suspend state.
13 `hook_usb_suspend_entry(void)` | When the device enters USB suspend state.
14 `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.
15 `hook_keyboard_loop(void)` | Continuously, during the main loop, after the matrix is checked.
16 `hook_matrix_change(keyevent_t event)` | When a matrix state change is detected, before any other actions are processed.
17 `hook_layer_change(uint32_t layer_state)` | When any layer is changed.
18 `hook_default_layer_change(uint32_t default_layer_state)` | When any default layer is changed.
19 `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)`
27 You can try these out by copying the code to your keymap file, or any .c file in the Makefile `SRC`.
29 #### Activate keymap layer 5 on startup
32 #include "action_layer.h"
34 void hook_late_init(void)
37 print("Layer 5 enabled!");
41 #### Blink the Caps Lock LED every .5 seconds
47 bool my_led_status = 0;
48 uint16_t my_led_timer;
50 void hook_keyboard_loop(void)
52 // check if we've reached 500 milliseconds yet...
53 if (timer_elapsed(my_led_timer) > 500)
55 // we've reached 500 milliseconds!
57 my_led_timer = timer_read();
59 // check the current LED state
62 // LED is on, so let's turn it off
63 led_set(host_keyboard_leds() & ~(1<<USB_LED_CAPS_LOCK));
68 // LED is off, so let's turn it on
69 led_set(host_keyboard_leds() | (1<<USB_LED_CAPS_LOCK));
76 #### Flash the Caps Lock LED for 20ms on every keypress
81 bool my_led_status = 0;
82 uint16_t my_led_timer;
84 void hook_matrix_change(keyevent_t event)
86 // only flash LED for key press events, not key release events.
89 // check the current LED status and reverse it
90 led_set(host_keyboard_leds() ^ (1<<USB_LED_CAPS_LOCK));
93 my_led_timer = timer_read();
97 void hook_keyboard_loop(void)
101 // check if we've reached 20 milliseconds yet...
102 if (timer_elapsed(my_led_timer) > 50)
104 led_set(host_keyboard_leds());