qmk_firmware/custom_quantum_functions.html

147 lines
155 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>How to Customize Your Keyboard's Behavior | QMK Firmware</title>
<meta name="description" content="Documentation for QMK Firmware">
<meta name="generator" content="VitePress v1.1.3">
<link rel="preload stylesheet" href="/assets/style.No8zk9aC.css" as="style">
<script type="module" src="/assets/app.DyBQnxno.js"></script>
<link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
<link rel="modulepreload" href="/assets/chunks/theme.CuUwN9QV.js">
<link rel="modulepreload" href="/assets/chunks/framework.B9AX-CPi.js">
<link rel="modulepreload" href="/assets/custom_quantum_functions.md.7Wtzp8l7.lean.js">
<script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
<script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
</head>
<body>
<div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar has-sidebar top" data-v-ae24b3ad data-v-ccf7ddec><div class="wrapper" data-v-ccf7ddec><div class="container" data-v-ccf7ddec><div class="title" data-v-ccf7ddec><div class="VPNavBarTitle has-sidebar" data-v-ccf7ddec data-v-ab179fa1><a class="title" href="/" data-v-ab179fa1><!--[--><!--]--><!--[--><!--[--><!--[--><img class="VPImage dark logo" src="/qmk-logo-dark.svg" alt data-v-8426fc1a><!--]--><!--[--><img class="VPImage light logo" src="/qmk-logo-light.svg" alt data-v-8426fc1a><!--]--><!--]--><!--]--><span data-v-ab179fa1>QMK Firmware</span><!--[--><!--]--></a></div></div><div class="content" data-v-ccf7ddec><div class="content-body" data-v-ccf7ddec><!--[--><!--]--><div class="VPNavBarSearch search" data-v-ccf7ddec><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-ccf7ddec data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>Home</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-ccf7ddec data-v-e6aabb21><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-e6aabb21 data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-ccf7ddec data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://reddit.com/r/olkb" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="50px" height="50px"><path d="M 29 3 C 28.0625 3 27.164063 3.382813 26.5 4 C 25.835938 4.617188 25.363281 5.433594 25 6.40625 C 24.355469 8.140625 24.085938 10.394531 24.03125 13.03125 C 19.234375 13.179688 14.820313 14.421875 11.28125 16.46875 C 10.214844 15.46875 8.855469 14.96875 7.5 14.96875 C 6.089844 14.96875 4.675781 15.511719 3.59375 16.59375 C 1.425781 18.761719 1.425781 22.238281 3.59375 24.40625 L 3.84375 24.65625 C 3.3125 26.035156 3 27.488281 3 29 C 3 33.527344 5.566406 37.585938 9.5625 40.4375 C 13.558594 43.289063 19.007813 45 25 45 C 30.992188 45 36.441406 43.289063 40.4375 40.4375 C 44.433594 37.585938 47 33.527344 47 29 C 47 27.488281 46.6875 26.035156 46.15625 24.65625 L 46.40625 24.40625 C 48.574219 22.238281 48.574219 18.761719 46.40625 16.59375 C 45.324219 15.511719 43.910156 14.96875 42.5 14.96875 C 41.144531 14.96875 39.785156 15.46875 38.71875 16.46875 C 35.195313 14.433594 30.800781 13.191406 26.03125 13.03125 C 26.09375 10.546875 26.363281 8.46875 26.875 7.09375 C 27.164063 6.316406 27.527344 5.757813 27.875 5.4375 C 28.222656 5.117188 28.539063 5 29 5 C 29.460938 5 29.683594 5.125 30.03125 5.40625 C 30.378906 5.6875 30.785156 6.148438 31.3125 6.6875 C 32.253906 7.652344 33.695313 8.714844 36.09375 8.9375 C 36.539063 11.238281 38.574219 13 41 13 C 43.75 13 46 10.75 46 8 C 46 5.25 43.75 3 41 3 C 38.605469 3 36.574219 4.710938 36.09375 6.96875 C 34.3125 6.796875 33.527344 6.109375 32.75 5.3125 C 32.300781 4.851563 31.886719 4
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FOO </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> SAFE_RANGE,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> BAR</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span></code></pre></div><h2 id="programming-the-behavior-of-any-keycode" tabindex="-1">Programming the Behavior of Any Keycode <a class="header-anchor" href="#programming-the-behavior-of-any-keycode" aria-label="Permalink to &quot;Programming the Behavior of Any Keycode {#programming-the-behavior-of-any-keycode}&quot;"></a></h2><p>When you want to override the behavior of an existing key, or define the behavior for a new key, you should use the <code>process_record_kb()</code> and <code>process_record_user()</code> functions. These are called by QMK during key processing before the actual key event is handled. If these functions return <code>true</code> QMK will process the keycodes as usual. That can be handy for extending the functionality of a key rather than replacing it. If these functions return <code>false</code> QMK will skip the normal key handling, and it will be up to you to send any key up or down events that are required.</p><p>These function are called every time a key is pressed or released.</p><h3 id="example-process-record-user-implementation" tabindex="-1">Example <code>process_record_user()</code> Implementation <a class="header-anchor" href="#example-process-record-user-implementation" aria-label="Permalink to &quot;Example `process_record_user()` Implementation&quot;"></a></h3><p>This example does two things. It defines the behavior for a custom keycode called <code>FOO</code>, and it supplements our Enter key by playing a tone whenever it is pressed.</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> process_record_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint16_t</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> keycode</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">keyrecord_t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">record</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> switch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (keycode) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> case</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FOO:</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (record</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">event.pressed) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Do something when pressed</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Do something else when release</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Skip all further processing of this key</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> case</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> KC_ENTER:</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Play a tone when enter is pressed</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (record</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">event.pressed) {</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PLAY_SONG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(tone_qwerty);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Let QMK send the enter press/release events</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Process all other keycodes normally</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="process-record-function-documentation" tabindex="-1"><code>process_record_*</code> Function Documentation <a class="header-anchor" href="#process-record-function-documentation" aria-label="Permalink to &quot;`process_record_*` Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>bool process_record_kb(uint16_t keycode, keyrecord_t *record)</code></li><li>Keymap: <code>bool process_record_user(uint16_t keycode, keyrecord_t *record)</code></li></ul><p>The <code>keycode</code> argument is whatever is defined in your keymap, eg <code>MO(1)</code>, <code>KC_L</code>, etc. You should use a <code>switch...case</code> block to handle these events.</p><p>The <code>record</code> argument contains information about the actual press:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">keyrecord_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> record {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> keyevent_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> event {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> keypos_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> key {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> uint8_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> col</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> uint8_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> row</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> bool</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> pressed</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> uint16_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h1 id="keyboard-initialization-code" tabindex="-1">Keyboard Initialization Code <a class="header-anchor" href="#keyboard-initialization-code" aria-label="Permalink to &quot;Keyboard Initialization Code&quot;"></a></h1><p>There are several steps in the keyboard initialization process. Depending on what you want to do, it will influence which function you should use.</p><p>These are the three main initialization functions, listed in the order that they&#39;re called.</p><ul><li><code>keyboard_pre_init_*</code> - Happens before most anything is started. Good for hardware setup that you want running very early.</li><li><code>matrix_init_*</code> - Happens midway through the firmware&#39;s startup process. Hardware is initialized, but features may not be yet.</li><li><code>keyboard_post_init_*</code> - Happens at the end of the firmware&#39;s startup process. This is where you&#39;d want to put &quot;customization&quot; code, for the most part.</li></ul><div class="warning custom-block"><p class="custom-block-title">WARNING</p><p>For most people, the <code>keyboard_post_init_user</code> function is what you want to call. For instance, this is where you want to set up things for RGB Underglow.</p></div><h2 id="keyboard-pre-initialization-code" tabindex="-1">Keyboard Pre Initialization code <a class="header-anchor" href="#keyboard-pre-initialization-code" aria-label="Permalink to &quot;Keyboard Pre Initialization code&quot;"></a></h2><p>This runs very early during startup, even before the USB has been started.</p><p>Shortly after this, the matrix is initialized.</p><p>For most users, this shouldn&#39;t be used, as it&#39;s primarily for hardware oriented initialization.</p><p>However, if you have hardware stuff that you need initialized, this is the best place for it (such as initializing LED pins).</p><h3 id="example-keyboard-pre-init-user-implementation" tabindex="-1">Example <code>keyboard_pre_init_user()</code> Implementation <a class="header-anchor" href="#example-keyboard-pre-init-user-implementation" aria-label="Permalink to &quot;Example `keyboard_pre_init_user()` Implementation&quot;"></a></h3><p>This example, at the keyboard level, sets up B0, B1, B2, B3, and B4 as LED pins.</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> keyboard_pre_init_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Call the keyboard pre init code.</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Set our LED pins as output</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_output</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B0);</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_output</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B1);</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_output</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B2);</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_output</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B3);</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> gpio_set_pin_output</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(B4);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="keyboard-pre-init-function-documentation" tabindex="-1"><code>keyboard_pre_init_*</code> Function Documentation <a class="header-anchor" href="#keyboard-pre-init-function-documentation" aria-label="Permalink to &quot;`keyboard_pre_init_*` Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>void keyboard_pre_init_kb(void)</code></li><li>Keymap: <code>void keyboard_pre_init_user(void)</code></li></ul><h2 id="matrix-initialization-code" tabindex="-1">Matrix Initialization Code <a class="header-anchor" href="#matrix-initialization-code" aria-label="Permalink to &quot;Matrix Initialization Code&quot;"></a></h2><p>This is called when the matrix is initialized, and after some of the hardware has been set up, but before many of the features have been initialized.</p><p>This is useful for setting up stuff that you may need elsewhere, but isn&#39;t hardware related nor is dependant on where it&#39;s started.</p><h3 id="matrix-init-function-documentation" tabindex="-1"><code>matrix_init_*</code> Function Documentation <a class="header-anchor" href="#matrix-init-function-documentation" aria-label="Permalink to &quot;`matrix_init_*` Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>void matrix_init_kb(void)</code></li><li>Keymap: <code>void matrix_init_user(void)</code></li></ul><h3 id="low-level-matrix-overrides" tabindex="-1">Low-level Matrix Overrides Function Documentation <a class="header-anchor" href="#low-level-matrix-overrides" aria-label="Permalink to &quot;Low-level Matrix Overrides Function Documentation {#low-level-matrix-overrides}&quot;"></a></h3><ul><li>GPIO pin initialisation: <code>void matrix_init_pins(void)</code><ul><li>This needs to perform the low-level initialisation of all row and column pins. By default this will initialise the input/output state of each of the GPIO pins listed in <code>MATRIX_ROW_PINS</code> and <code>MATRIX_COL_PINS</code>, based on whether or not the keyboard is set up for <code>ROW2COL</code>, <code>COL2ROW</code>, or <code>DIRECT_PINS</code>. Should the keyboard designer override this function, no initialisation of pin state will occur within QMK itself, instead deferring to the keyboard&#39;s override.</li></ul></li><li><code>COL2ROW</code>-based row reads: <code>void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)</code></li><li><code>ROW2COL</code>-based column reads: <code>void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter)</code></li><li><code>DIRECT_PINS</code>-based reads: <code>void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)</code><ul><li>These three functions need to perform the low-level retrieval of matrix state of relevant input pins, based on the matrix type. Only one of the functions should be implemented, if needed. By default this will iterate through <code>MATRIX_ROW_PINS</code> and <code>MATRIX_COL_PINS</code>, configuring the inputs and outputs based on whether or not the keyboard is set up for <code>ROW2COL</code>, <code>COL2ROW</code>, or <code>DIRECT_PINS</code>. Should the keyboard designer override this function, no manipulation of matrix GPIO pin state will occur within QMK itself, instead deferring to the keyboard&#39;s override.</li></ul></li></ul><h2 id="keyboard-post-initialization-code" tabindex="-1">Keyboard Post Initialization code <a class="header-anchor" href="#keyboard-post-initialization-code" aria-label="Permalink to &quot;Keyboard Post Initialization code&quot;"></a></h2><p>This is ran as the very last task in the keyboard initialization process. This is useful if you want to make changes to certain features, as they should be initialized by this point.</p><h3 id="example-keyboard-post-init-user-implementation" tabindex="-1">Example <code>keyboard_post_init_user()</code> Implementation <a class="header-anchor" href="#example-keyboard-post-init-user-implementation
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Call the post init code.</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_enable_noeeprom</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // enables Rgb, without saving settings</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_sethsv_noeeprom</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">180</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">255</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">255</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // sets the color to teal/cyan without saving</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_mode_noeeprom</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGBLIGHT_MODE_BREATHING </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // sets mode to Fast breathing without saving</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="keyboard-post-init-function-documentation" tabindex="-1"><code>keyboard_post_init_*</code> Function Documentation <a class="header-anchor" href="#keyboard-post-init-function-documentation" aria-label="Permalink to &quot;`keyboard_post_init_*` Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>void keyboard_post_init_kb(void)</code></li><li>Keymap: <code>void keyboard_post_init_user(void)</code></li></ul><h1 id="matrix-scanning-code" tabindex="-1">Matrix Scanning Code <a class="header-anchor" href="#matrix-scanning-code" aria-label="Permalink to &quot;Matrix Scanning Code&quot;"></a></h1><p>Whenever possible you should customize your keyboard by using <code>process_record_*()</code> and hooking into events that way, to ensure that your code does not have a negative performance impact on your keyboard. However, in rare cases it is necessary to hook into the matrix scanning. Be extremely careful with the performance of code in these functions, as it will be called at least 10 times per second.</p><h3 id="example-matrix-scan-implementation" tabindex="-1">Example <code>matrix_scan_*</code> Implementation <a class="header-anchor" href="#example-matrix-scan-implementation" aria-label="Permalink to &quot;Example `matrix_scan_*` Implementation&quot;"></a></h3><p>This example has been deliberately omitted. You should understand enough about QMK internals to write this without an example before hooking into such a performance sensitive area. If you need help please <a href="https://github.com/qmk/qmk_firmware/issues/new" target="_blank" rel="noreferrer">open an issue</a> or <a href="https://discord.gg/qmk" target="_blank" rel="noreferrer">chat with us on Discord</a>.</p><h3 id="matrix-scan-function-documentation" tabindex="-1"><code>matrix_scan_*</code> Function Documentation <a class="header-anchor" href="#matrix-scan-function-documentation" aria-label="Permalink to &quot;`matrix_scan_*` Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>void matrix_scan_kb(void)</code></li><li>Keymap: <code>void matrix_scan_user(void)</code></li></ul><p>This 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.</p><p>You should use this function if you need custom matrix scanning code. It can also be used for custom status output (such as LEDs or a display) or other functionality that you want to trigger regularly even when the user isn&#39;t typing.</p><h1 id="keyboard-housekeeping" tabindex="-1">Keyboard housekeeping <a class="header-anchor" href="#keyboard-housekeeping" aria-label="Permalink to &quot;Keyboard housekeeping&quot;"></a></h1><ul><li>Keyboard/Revision: <code>void housekeeping_task_kb(void)</code></li><li>Keymap: <code>void housekeeping_task_user(void)</code></li></ul><p>This function gets called at the end of all QMK processing, before starting the next iteration. You can safely assume that QMK has dealt with the last matrix scan at the time that these functions are invoked -- layer states have been updated, USB reports have been sent, LEDs have been updated, and displays have been drawn.</p><p>Similar to <code>matrix_scan_*</code>, these are called as often as the MCU can handle. To keep your board responsive, it&#39;s suggested to do as little as possible during these function calls, potentially throtting their behaviour if you do indeed require implementing something special.</p><h3 id="example-void-housekeeping-task-user-void-implementation" tabindex="-1">Example <code>void housekeeping_task_user(void)</code> implementation <a class="header-anchor" href="#example-void-housekeeping-task-user-void-implementation" aria-label="Permalink to &quot;Example `void housekeeping_task_user(void)` implementation&quot;"></a></h3><p>This example will show you how to use <code>void housekeeping_task_user(void)</code> to turn off <a href="./features/rgblight">RGB Light</a>.
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RGBLIGHT_TIMEOUT</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 900000</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // ms to wait until rgblight time out, 900K ms is 15min.</span></span></code></pre></div><p>Next, add the following code to your <code>keymap.c</code>:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> uint32_t</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> key_timer;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // timer for last keyboard activity, use 32bit value and function to make longer idle time possible</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> refresh_rgb</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // refreshes the activity timer and RGB, invoke whenever any activity happens</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> check_rgb_timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // checks if enough time has passed for RGB to timeout</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> is_rgb_timeout </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // store if RGB has timed out or not in a boolean</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> refresh_rgb</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> key_timer </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> timer_read32</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // store time of last refresh</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (is_rgb_timeout)</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> is_rgb_timeout </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_wakeup</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> check_rgb_timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">!</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">is_rgb_timeout </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&amp;&amp;</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> timer_elapsed32</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(key_timer) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&gt;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> RGBLIGHT_TIMEOUT)</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // check if RGB has already timeout and if enough time has passed</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_suspend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> is_rgb_timeout </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Then, call the above functions from QMK&#39;s built in post processing functions like so */</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Runs at the end of each scan loop, check if RGB timeout has occured or not */</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> housekeeping_task_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#ifdef</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RGBLIGHT_TIMEOUT</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> check_rgb_timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Runs after each key press, check if activity occurred */</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> post_process_record_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint16_t</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> keycode</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">keyrecord_t</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">record</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#ifdef</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RGBLIGHT_TIMEOUT</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (record-&gt;event.pressed)</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> refresh_rgb</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Runs after each encoder tick, check if activity occurred */</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> post_encoder_update_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint8_t</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> index</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> clockwise</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#ifdef</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RGBLIGHT_TIMEOUT</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> refresh_rgb</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h1 id="keyboard-idling-wake-code" tabindex="-1">Keyboard Idling/Wake Code <a class="header-anchor" href="#keyboard-idling-wake-code" aria-label="Permalink to &quot;Keyboard Idling/Wake Code&quot;"></a></h1><p>If the board supports it, it can be &quot;idled&quot;, by stopping a number of functions. A good example of this is RGB lights or backlights. This can save on power consumption, or may be better behavior for your keyboard.</p><p>This is controlled by two functions: <code>suspend_power_down_*</code> and <code>suspend_wakeup_init_*</code>, which are called when the system board is idled and when it wakes up, respectively.</p><h3 id="example-suspend-power-down-user-and-suspend-wakeup-init-user-implementation" tabindex="-1">Example <code>suspend_power_down_user()</code> and <code>suspend_wakeup_init_user()</code> Implementation <a class="header-anchor" href="#example-suspend-power-down-user-and-suspend-wakeup-init-user-implementation" aria-label="Permalink to &quot;Example `suspend_power_down_user()` and `suspend_wakeup_init_user()` Implementation&quot;"></a></h3><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> suspend_power_down_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // code will run multiple times while keyboard is suspended</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> suspend_wakeup_init_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // code will run on keyboard wakeup</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="keyboard-suspend-wake-function-documentation" tabindex="-1">Keyboard suspend/wake Function Documentation <a class="header-anchor" href="#keyboard-suspend-wake-function-documentation" aria-label="Permalink to &quot;Keyboard suspend/wake Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>void suspend_power_down_kb(void)</code> and <code>void suspend_wakeup_init_user(void)</code></li><li>Keymap: <code>void suspend_power_down_kb(void)</code> and <code>void suspend_wakeup_init_user(void)</code></li></ul><h1 id="keyboard-shutdown-reboot-code" tabindex="-1">Keyboard Shutdown/Reboot Code <a class="header-anchor" href="#keyboard-shutdown-reboot-code" aria-label="Permalink to &quot;Keyboard Shutdown/Reboot Code {#keyboard-shutdown-reboot-code}&quot;"></a></h1><p>This function gets called whenever the firmware is reset, whether it&#39;s a soft reset or reset to the bootloader. This is the spot to use for any sort of cleanup, as this happens right before the actual reset. And it can be useful for turning off different systems (such as RGB, onboard screens, etc).</p><p>Additionally, it differentiates between the soft reset (eg, rebooting back into the firmware) or jumping to the bootloader.</p><p>Certain tasks are performed during shutdown too. The keyboard is cleared, music and midi is stopped (if enabled), the shutdown chime is triggered (if audio is enabled), and haptic is stopped.</p><p>If <code>jump_to_bootloader</code> is set to <code>true</code>, this indicates that the board will be entering the bootloader for a new firmware flash, whereas <code>false</code> indicates that this is happening for a soft reset and will load the firmware agaim immediately (such as when using <code>QK_REBOOT</code> or <code>QK_CLEAR_EEPROM</code>).</p><p>As there is a keyboard and user level function, returning <code>false</code> for the user function will disable the keyboard level function, allowing for customization.</p><div class="tip custom-block"><p class="custom-block-title">TIP</p><p>Bootmagic does not trigger <code>shutdown_*()</code> as it happens before most of the initialization process.</p></div><h3 id="example-shutdown-kb-implementation" tabindex="-1">Example <code>shutdown_kb()</code> Implementation <a class="header-anchor" href="#example-shutdown-kb-implementation" aria-label="Permalink to &quot;Example `shutdown_kb()` Implementation&quot;"></a></h3><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> shutdown_kb</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> jump_to_bootloader</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">!</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">shutdown_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(jump_to_bootloader)) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (jump_to_bootloader) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // red for bootloader</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_set_color_all</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGB_OFF);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // off for soft reset</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_set_color_all</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGB_GREEN);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // force flushing -- otherwise will never happen</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_update_pwm_buffers</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="example-shutdown-user-implementation" tabindex="-1">Example <code>shutdown_user()</code> Implementation <a class="header-anchor" href="#example-shutdown-user-implementation" aria-label="Permalink to &quot;Example `shutdown_user()` Implementation&quot;"></a></h3><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> shutdown_user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> jump_to_bootloader</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (jump_to_bootloader) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // red for bootloader</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_set_color_all</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGB_RED);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // off for soft reset</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_set_color_all</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGB_OFF);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // force flushing -- otherwise will never happen</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgb_matrix_update_pwm_buffers</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // false to not process kb level</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="keyboard-shutdown-reboot-function-documentation" tabindex="-1">Keyboard shutdown/reboot Function Documentation <a class="header-anchor" href="#keyboard-shutdown-reboot-function-documentation" aria-label="Permalink to &quot;Keyboard shutdown/reboot Function Documentation&quot;"></a></h3><ul><li>Keyboard/Revision: <code>bool shutdown_kb(bool jump_to_bootloader)</code></li><li>Keymap: <code>bool shutdown_user(bool jump_to_bootloader)</code></li></ul><h1 id="deferred-execution" tabindex="-1">Deferred Execution <a class="header-anchor" href="#deferred-execution" aria-label="Permalink to &quot;Deferred Execution {#deferred-execution}&quot;"></a></h1><p>QMK has the ability to execute a callback after a specified period of time, rather than having to manually manage timers. To enable this functionality, set <code>DEFERRED_EXEC_ENABLE = yes</code> in rules.mk.</p><h2 id="deferred-executor-callbacks" tabindex="-1">Deferred executor callbacks <a class="header-anchor" href="#deferred-executor-callbacks" aria-label="Permalink to &quot;Deferred executor callbacks&quot;"></a></h2><p>All <em>deferred executor callbacks</em> have a common function signature and look like:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint32_t</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> my_callback</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint32_t</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> trigger_time</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">cb_arg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* do something */</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> bool</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> repeat </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> my_deferred_functionality</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> repeat </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">?</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 500</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> :</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><p>The first argument <code>trigger_time</code> is the intended time of execution. If other delays prevent executing at the exact trigger time, this allows for &quot;catch-up&quot; or even skipping intervals, depending on the required behaviour.</p><p>The second argument <code>cb_arg</code> is the same argument passed into <code>defer_exec()</code> below, and can be used to access state information from the original call context.</p><p>The return value is the number of milliseconds to use if the function should be repeated -- if the callback returns <code>0</code> then it&#39;s automatically unregistered. In the example above, a hypothetical <code>my_deferred_functionality()</code> is invoked to determine if the callback needs to be repeated -- if it does, it reschedules for a <code>500</code> millisecond delay, otherwise it informs the deferred execution background task that it&#39;s done, by returning <code>0</code>.</p><div class="tip custom-block"><p class="custom-block-title">TIP</p><p>Note that the returned delay will be applied to the intended trigger time, not the time of callback invocation. This allows for generally consistent timing even in the face of occasional late execution.</p></div><h2 id="deferred-executor-registration" tabindex="-1">Deferred executor registration <a class="header-anchor" href="#deferred-executor-registration" aria-label="Permalink to &quot;Deferred executor registration&quot;"></a></h2><p>Once a callback has been defined, it can be scheduled using the following API:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">deferred_token my_token </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> defer_exec</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1500</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, my_callback, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NULL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div><p>The first argument is the number of milliseconds to wait until executing <code>my_callback</code> -- in the case above, <code>1500</code> milliseconds, or 1.5 seconds.</p><p>The third parameter is the <code>cb_arg</code> that gets passed to the callback at the point of execution. This value needs to be valid at the time the callback is invoked -- a local function value will be destroyed before the callback is executed and should not be used. If this is not required, <code>NULL</code> should be used.</p><p>The return value is a <code>deferred_token</code> that can consequently be used to cancel the deferred executor callback before it&#39;s invoked. If a failure occurs, the returned value will be <code>INVALID_DEFERRED_TOKEN</code>. Usually this will be as a result of supplying <code>0</code> to the delay, or a <code>NULL</code> for the callback. The other failure case is if there are too many deferred executions &quot;in flight&quot; -- this can be increased by changing the limit, described below.</p><h2 id="extending-a-deferred-execution" tabindex="-1">Extending a deferred execution <a class="header-anchor" href="#extending-a-deferred-execution" aria-label="Permalink to &quot;Extending a deferred execution&quot;"></a></h2><p>The <code>deferred_token</code> returned by <code>defer_exec()</code> can be used to extend a the duration a pending execution waits before it gets invoked:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--sh
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">extend_deferred_exec</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(my_token, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">800</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div><h2 id="cancelling-a-deferred-execution" tabindex="-1">Cancelling a deferred execution <a class="header-anchor" href="#cancelling-a-deferred-execution" aria-label="Permalink to &quot;Cancelling a deferred execution&quot;"></a></h2><p>The <code>deferred_token</code> returned by <code>defer_exec()</code> can be used to cancel a pending execution before it gets invoked:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// This will cancel my_token&#39;s future execution</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cancel_deferred_exec</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(my_token);</span></span></code></pre></div><p>Once a token has been canceled, it should be considered invalid. Reusing the same token is not supported.</p><h2 id="deferred-callback-limits" tabindex="-1">Deferred callback limits <a class="header-anchor" href="#deferred-callback-limits" aria-label="Permalink to &quot;Deferred callback limits&quot;"></a></h2><p>There are a maximum number of deferred callbacks that can be scheduled, controlled by the value of the define <code>MAX_DEFERRED_EXECUTORS</code>.</p><p>If registrations fail, then you can increase this value in your keyboard or keymap <code>config.h</code> file, for example to 16 instead of the default 8:</p><div class="language-c vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MAX_DEFERRED_EXECUTORS</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 16</span></span></code></pre></div><h1 id="advanced-topics" tabindex="-1">Advanced topics <a class="header-anchor" href="#advanced-topics" aria-label="Permalink to &quot;Advanced topics {#advanced-topics}&quot;"></a></h1><p>This page used to encompass a large set of features. We have moved many sections that used to be part of this page to their own pages. Everything below this point is simply a redirect so that people following old links on the web find what they&#39;re looking for.</p><h2 id="layer-change-code" tabindex="-1">Layer Change Code <a class="header-anchor" href="#layer-change-code" aria-label="Permalink to &quot;Layer Change Code {#layer-change-code}&quot;"></a></h2><p><a href="./feature_layers#layer-change-code">Layer change code</a></p><h2 id="persistent-configuration-eeprom" tabindex="-1">Persistent Configuration (EEPROM) <a class="header-anchor" href="#persistent-configuration-eeprom" aria-label="Permalink to &quot;Persistent Configuration (EEPROM) {#persistent-configuration-eeprom}&quot;"></a></h2><p><a href="./feature_eeprom">Persistent Configuration (EEPROM)</a></p></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-09de1c0f><!--[--><!--]--><div class="edit-info" data-v-09de1c0f><div class="edit-link" data-v-09de1c0f><a class="VPLink link vp-external-link-icon no-icon edit-link-button" href="https://github.com/qmk/qmk_firmware/edit/master/docs/custom_quantum_functions.md" target="_blank" rel="noreferrer" data-v-09de1c0f><!--[--><span class="vpi-square-pen edit-link-icon" data-v-09de1c0f></span> Edit this page<!--]--></a></div><!----></div><nav class="prev-next" data-v-09de1c0f><div class="pager" data-v-09de1c0f><a class="VPLink link pager-link prev" href="/cli_tab_complete" data-v-09de1c0f><!--[--><span class="desc" data-v-09de1c0f>Previous page</span><span class="title" data-v-09de1c0f>Tab Completion</span><!--]--></a></div><div class="pager" data-v-09de1c0f><a class="VPLink link pager-link next" href="/driver_installation_zadig" data-v-09de1c0f><!--[--><span class="desc" data-v-09de1c0f>Next page</span><span class="title" data-v-09de1c0f>Driver Installation with Zadig</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
<script>window.__VP_HASH_MAP__=JSON.parse("{\"changelog_20190830.md\":\"D5Gv0xM2\",\"changelog_20200229.md\":\"TBlkCyr5\",\"changelog_20200530.md\":\"TIsD4IJ6\",\"changelog_20200829.md\":\"DLRic7TT\",\"changelog_20201128.md\":\"Ba_rFlxl\",\"changelog_20210227.md\":\"DvqXERXt\",\"changelog_20210529.md\":\"D6J1RidB\",\"changelog_20210828.md\":\"C3lFgq5A\",\"changelog_20211127.md\":\"KawnpmKg\",\"changelog_20220226.md\":\"KLnmRn9F\",\"changelog_20220528.md\":\"Boue42eA\",\"changelog_20220827.md\":\"BmGjSteF\",\"changelog_20221126.md\":\"B7IFykhd\",\"changelog_20230226.md\":\"twCbPT9A\",\"changelog_20230528.md\":\"BzLLaFNh\",\"changelog_20230827.md\":\"BrhCShVH\",\"changelog_20231126.md\":\"DVLm2eGv\",\"changelog_20240225.md\":\"CXS3FgKU\",\"changelog_20240526.md\":\"BM7fxQBp\",\"changelog_20240825.md\":\"BDFY9wpG\",\"changelog_20241124.md\":\"Q_tHeHAa\",\"capabilities.md\":\"a_Nbgg0n\",\"capabilities_inc.md\":\"Cor-lDIx\",\"api_development_environment.md\":\"pCQRKF_M\",\"api_development_overview.md\":\"D5Zus3EP\",\"api_docs.md\":\"CCHPm9Ph\",\"api_overview.md\":\"Bo6l-c66\",\"arm_debugging.md\":\"DcrpfiyI\",\"breaking_changes.md\":\"y5xbzT8a\",\"breaking_changes_history.md\":\"DnerrifT\",\"breaking_changes_instructions.md\":\"y3u4ruQJ\",\"chibios_upgrade_instructions.md\":\"p6uZF4kP\",\"cli.md\":\"D3UYWDOB\",\"cli_commands.md\":\"DmaB9sac\",\"cli_configuration.md\":\"gftml-GJ\",\"cli_development.md\":\"CV5X2u0i\",\"cli_tab_complete.md\":\"BjhR5TFS\",\"coding_conventions_c.md\":\"DDXrsq6j\",\"coding_conventions_python.md\":\"BpeQIiJ8\",\"compatible_microcontrollers.md\":\"Bp5Cuz5M\",\"config_options.md\":\"C0ey_0zq\",\"configurator_architecture.md\":\"-1IamgaM\",\"configurator_default_keymaps.md\":\"Csbe8pIs\",\"configurator_step_by_step.md\":\"BQXI9udF\",\"configurator_troubleshooting.md\":\"A5MEdd6g\",\"contributing.md\":\"CJpSA-c9\",\"custom_matrix.md\":\"BBjT28gr\",\"custom_quantum_functions.md\":\"7Wtzp8l7\",\"data_driven_config.md\":\"fJsS9ZqH\",\"documentation_best_practices.md\":\"DG91aPmN\",\"documentation_templates.md\":\"C2-rIfQ7\",\"driver_installation_zadig.md\":\"quInZVrl\",\"drivers_adc.md\":\"DEzkLXsb\",\"drivers_apa102.md\":\"DZyCh2Hs\",\"drivers_audio.md\":\"D7D3osxI\",\"drivers_aw20216s.md\":\"CRGZz1xZ\",\"drivers_eeprom.md\":\"D2Ip1OAY\",\"drivers_flash.md\":\"BgL2PtmD\",\"drivers_gpio.md\":\"DMCKpyic\",\"drivers_i2c.md\":\"CZ78jxRE\",\"drivers_is31fl3218.md\":\"DgfDKi5r\",\"drivers_is31fl3236.md\":\"DO7FGjNM\",\"drivers_is31fl3729.md\":\"BmFD_YGz\",\"drivers_is31fl3731.md\":\"k9wXgexF\",\"drivers_is31fl3733.md\":\"CTovyg7g\",\"drivers_is31fl3736.md\":\"C-ibqmvt\",\"drivers_is31fl3737.md\":\"DuW4Ycqu\",\"drivers_is31fl3741.md\":\"Bd8tDEGh\",\"drivers_is31fl3742a.md\":\"o6103t5c\",\"drivers_is31fl3743a.md\":\"e2xEXYVf\",\"drivers_is31fl3745.md\":\"C0VypsOf\",\"drivers_is31fl3746a.md\":\"Dc_MBCTu\",\"drivers_serial.md\":\"CJRpA3ob\",\"drivers_snled27351.md\":\"DkoZ0nOF\",\"drivers_spi.md\":\"vFN5MRzl\",\"drivers_uart.md\":\"DfVpzltt\",\"drivers_ws2812.md\":\"Obzw-GTj\",\"easy_maker.md\":\"yfFLYEZu\",\"faq_build.md\":\"ClAW0aMe\",\"faq_debug.md\":\"DaRqKHet\",\"faq_general.md\":\"0Ixv74_I\",\"faq_keymap.md\":\"BLluC4NV\",\"faq_misc.md\":\"CVtsey6K\",\"feature_advanced_keycodes.md\":\"C2iBp6mf\",\"feature_converters.md\":\"rz_anpjy\",\"feature_debounce_type.md\":\"CYIyyhFB\",\"feature_eeprom.md\":\"Bhzy4RDv\",\"feature_layers.md\":\"B59e5DpO\",\"feature_layouts.md\":\"BK7vfZuH\",\"feature_macros.md\":\"4VvYwf-t\",\"feature_userspace.md\":\"CF52Rhjy\",\"features_audio.md\":\"xQ9VRAsq\",\"features_auto_shift.md\":\"DOcRLWqv\",\"features_autocorrect.md\":\"iXmGH9ee\",\"features_backlight.md\":\"Cvuqwm3F\",\"features_bootmagic.md\":\"BjDrYOUH\",\"features_caps_word.md\":\"CYfZhNN7\",\"features_combo.md\":\"v8KQE72a\",\"features_command.md\":\"ajsjx5Wc\",\"features_digitizer.md\":\"BouFmMBP\",\"features_dip_switch.md\":\"BphpeXE7\",\"features_dynamic_macros.md\":\"CnLDkQKC\",\"features_encoders.md\":\"CGtgEWxD\",\"features_grave_esc.md\":\"DXES6Ttb\",\"features_haptic_feedback.md\":\"COJ0M8E1\",\"features_hd4
</body>
</html>