Updated Custom Quantum Functions (markdown)
[jackhill/qmk/firmware.git] / Custom-Quantum-Functions.md
CommitLineData
4f48dd7c 1A custom keyboard is about more than sending button presses to your computer. QMK has designed hooks to allow you to inject, override, and otherwise customize how your keyboard responds in different situations. These allow you to control status LED's, write complex macros, or even change a key's behavior based on the keyboard's state.
2
3## A Word on Keyboards vs Keymap
4
5We have structured QMK as a hierarchy:
6
7* Core
8 * Keyboard/Revision (`_kb`)
9 * Keymap (`_user`)
10
36757de1 11Each of the functions described below can be defined with a `_kb()` suffix or an `_user()` suffix. We intend for you to use the `_kb()` suffix at the Keyboard/Revision level, while the `_user()` suffix should be used at the Keymap level.
4f48dd7c 12
13When defining functions at the Keyboard/Revision level it is important that your `_kb()` implementation call `*_user()` before executing anything else- otherwise the keymap level function will never be called.
14
b65282f6 15## Matrix Initialization Code
4f48dd7c 16
b65282f6 17* Keyboard/Revision: `void matrix_init_kb(void)`
18* Keymap: `void matrix_init_user(void)`
4f48dd7c 19
b65282f6 20This function gets called when the matrix is initiated. You should use this function to initialize any custom hardware you may have, such as speakers, LED drivers, or other features which need to be setup after the keyboard powers on.
4f48dd7c 21
b65282f6 22#### Example
23
24```
25void matrix_init_kb(void) {
26 // put your keyboard start-up code here
27 // runs once when the firmware starts up
28 matrix_init_user();
29
30 // JTAG disable for PORT F. write JTD bit twice within four cycles.
31 MCUCR |= (1<<JTD);
32 MCUCR |= (1<<JTD);
33
34 // * Set our LED pins as output
35 DDRB |= (1<<0);
36 DDRB |= (1<<1);
37 DDRB |= (1<<2);
38 DDRB |= (1<<3);
39 DDRB |= (1<<4);
40}
41```
42
43## Matrix Scanning Code
44
45* Keyboard/Revision: `void matrix_scan_kb(void)`
46* Keymap: `void matrix_scan_user(void)`
4f48dd7c 47
48This function gets called at every matrix scan, which is basically as often as the MCU can handle. Be careful what you put here, as it will get run a lot.
49
50You should use this function if you need custom matrix scanning code. It can also be used for custom status output (such as LED's or a display) or other functionality that you want to trigger regularly even when the user isn't typing.
51
52## `bool process_record_kb(uint16_t keycode, keyrecord_t *record)` and `bool process_record_user(uint16_t keycode, keyrecord_t *record)`
53
54This function gets called every time a key is pressed or released. This is where you should define most custom functionality. The return value is whether or not QMK should continue processing the keycode - returning `false` stops the execution.
55
56The `keycode` variable is whatever is defined in your keymap, eg `MO(1)`, `KC_L`, etc. and can be switch-cased to execute code whenever a particular code is pressed.
57
58The `record` variable contains infomation about the actual press:
59
60```
61keyrecord_t record {
62 keyevent_t event {
63 keypos_t key {
64 uint8_t col
65 uint8_t row
66 }
67 bool pressed
68 uint16_t time
69 }
70}
71```
72
73The conditional `if (record->event.pressed)` can tell if the key is being pressed or released, and you can execute code based on that.
74
75## `void led_set_kb(uint8_t usb_led)` and `void led_set_user(uint8_t usb_led)`
76
77This allows you to control the 5 LED's defined as part of the USB Keyboard spec. It will be called when the state of one of those 5 LEDs changes.
78
79* `USB_LED_NUM_LOCK`
80* `USB_LED_CAPS_LOCK`
81* `USB_LED_SCROLL_LOCK`
82* `USB_LED_COMPOSE`
83* `USB_LED_KANA`
84
85This is a typical pattern for lighting LED's to match the `USB_LED_*` state
86
87```
88void led_set_kb(uint8_t usb_led) {
89 if (usb_led & (1<<USB_LED_NUM_LOCK)) {
90 PORTB |= (1<<0);
91 } else {
92 PORTB &= ~(1<<0);
93 }
94 if (usb_led & (1<<USB_LED_CAPS_LOCK)) {
95 PORTB |= (1<<1);
96 } else {
97 PORTB &= ~(1<<1);
98 }
99 if (usb_led & (1<<USB_LED_SCROLL_LOCK)) {
100 PORTB |= (1<<2);
101 } else {
102 PORTB &= ~(1<<2);
103 }
104 if (usb_led & (1<<USB_LED_COMPOSE_LOCK)) {
105 PORTB |= (1<<3);
106 } else {
107 PORTB &= ~(1<<3);
108 }
109 if (usb_led & (1<<USB_LED_KANA_LOCK)) {
110 PORTB |= (1<<4);
111 } else {
112 PORTB &= ~(1<<4);
113 }
114}
115```