<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span></code></pre></div><h2id="programming-the-behavior-of-any-keycode"tabindex="-1">Programming the Behavior of Any Keycode <aclass="header-anchor"href="#programming-the-behavior-of-any-keycode"aria-label="Permalink to "Programming the Behavior of Any Keycode {#programming-the-behavior-of-any-keycode}""></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><h3id="example-process-record-user-implementation"tabindex="-1">Example <code>process_record_user()</code> Implementation <aclass="header-anchor"href="#example-process-record-user-implementation"aria-label="Permalink to "Example `process_record_user()` Implementation""></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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> process_record_user</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint16_t</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> keycode</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">keyrecord_t</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">record</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Skip all further processing of this key</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Let QMK send the enter press/release events</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Process all other keycodes normally</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="process-record-function-documentation"tabindex="-1"><code>process_record_*</code> Function Documentation <aclass="header-anchor"href="#process-record-function-documentation"aria-label="Permalink to "`process_record_*` Function Documentation""></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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">keyrecord_t</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> record {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h1id="keyboard-initialization-code"tabindex="-1">Keyboard Initialization Code <aclass="header-anchor"href="#keyboard-initialization-code"aria-label="Permalink to "Keyboard Initialization Code""></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'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'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's startup process. This is where you'd want to put "customization" code, for the most part.</li></ul><divclass="warning custom-block"><pclass="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><h2id="keyboard-pre-initialization-code"tabindex="-1">Keyboard Pre Initialization code <aclass="header-anchor"href="#keyboard-pre-initialization-code"aria-label="Permalink to "Keyboard Pre Initialization code""></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't be used, as it'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><h3id="example-keyboard-pre-init-user-implementation"tabindex="-1">Example <code>keyboard_pre_init_user()</code> Implementation <aclass="header-anchor"href="#example-keyboard-pre-init-user-implementation"aria-label="Permalink to "Example `keyboard_pre_init_user()` Implementation""></a></h3><p>This example, at the keyboard level, sets up B0, B1, B2, B3, and B4 as LED pins.</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> keyboard_pre_init_user</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Call the keyboard pre init code.</span></span>
<spanclass="line"></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Set our LED pins as output</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="keyboard-pre-init-function-documentation"tabindex="-1"><code>keyboard_pre_init_*</code> Function Documentation <aclass="header-anchor"href="#keyboard-pre-init-function-documentation"aria-label="Permalink to "`keyboard_pre_init_*` Function Documentation""></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><h2id="matrix-initialization-code"tabindex="-1">Matrix Initialization Code <aclass="header-anchor"href="#matrix-initialization-code"aria-label="Permalink to "Matrix Initialization Code""></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't hardware related nor is dependant on where it's started.</p><h3id="matrix-init-function-documentation"tabindex="-1"><code>matrix_init_*</code> Function Documentation <aclass="header-anchor"href="#matrix-init-function-documentation"aria-label="Permalink to "`matrix_init_*` Function Documentation""></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><h3id="low-level-matrix-overrides"tabindex="-1">Low-level Matrix Overrides Function Documentation <aclass="header-anchor"href="#low-level-matrix-overrides"aria-label="Permalink to "Low-level Matrix Overrides Function Documentation {#low-level-matrix-overrides}""></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'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's override.</li></ul></li></ul><h2id="keyboard-post-initialization-code"tabindex="-1">Keyboard Post Initialization code <aclass="header-anchor"href="#keyboard-post-initialization-code"aria-label="Permalink to "Keyboard Post Initialization code""></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><h3id="example-keyboard-post-init-user-implementation"tabindex="-1">Example <code>keyboard_post_init_user()</code> Implementation <aclass="header-anchor"href="#example-keyboard-post-init-user-implementation
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Call the post init code.</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_enable_noeeprom</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // enables Rgb, without saving settings</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_sethsv_noeeprom</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">180</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">255</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">255</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // sets the color to teal/cyan without saving</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> rgblight_mode_noeeprom</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(RGBLIGHT_MODE_BREATHING </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // sets mode to Fast breathing without saving</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="keyboard-post-init-function-documentation"tabindex="-1"><code>keyboard_post_init_*</code> Function Documentation <aclass="header-anchor"href="#keyboard-post-init-function-documentation"aria-label="Permalink to "`keyboard_post_init_*` Function Documentation""></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><h1id="matrix-scanning-code"tabindex="-1">Matrix Scanning Code <aclass="header-anchor"href="#matrix-scanning-code"aria-label="Permalink to "Matrix Scanning Code""></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><h3id="example-matrix-scan-implementation"tabindex="-1">Example <code>matrix_scan_*</code> Implementation <aclass="header-anchor"href="#example-matrix-scan-implementation"aria-label="Permalink to "Example `matrix_scan_*` Implementation""></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 <ahref="https://github.com/qmk/qmk_firmware/issues/new"target="_blank"rel="noreferrer">open an issue</a> or <ahref="https://discord.gg/qmk"target="_blank"rel="noreferrer">chat with us on Discord</a>.</p><h3id="matrix-scan-function-documentation"tabindex="-1"><code>matrix_scan_*</code> Function Documentation <aclass="header-anchor"href="#matrix-scan-function-documentation"aria-label="Permalink to "`matrix_scan_*` Function Documentation""></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't typing.</p><h1id="keyboard-housekeeping"tabindex="-1">Keyboard housekeeping <aclass="header-anchor"href="#keyboard-housekeeping"aria-label="Permalink to "Keyboard housekeeping""></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'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><h3id="example-void-housekeeping-task-user-void-implementation"tabindex="-1">Example <code>void housekeeping_task_user(void)</code> implementation <aclass="header-anchor"href="#example-void-housekeeping-task-user-void-implementation"aria-label="Permalink to "Example `void housekeeping_task_user(void)` implementation""></a></h3><p>This example will show you how to use <code>void housekeeping_task_user(void)</code> to turn off <ahref="./features/rgblight">RGB Light</a>.
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RGBLIGHT_TIMEOUT</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 900000</span><spanstyle="--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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> uint32_t</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> key_timer;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // timer for last keyboard activity, use 32bit value and function to make longer idle time possible</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> refresh_rgb</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // refreshes the activity timer and RGB, invoke whenever any activity happens</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">static</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> check_rgb_timeout</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // checks if enough time has passed for RGB to timeout</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> is_rgb_timeout </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // store if RGB has timed out or not in a boolean</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> key_timer </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> timer_read32</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // store time of last refresh</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">!</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">is_rgb_timeout </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">&&</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> timer_elapsed32</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(key_timer) </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> RGBLIGHT_TIMEOUT)</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // check if RGB has already timeout and if enough time has passed</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Then, call the above functions from QMK's built in post processing functions like so */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Runs at the end of each scan loop, check if RGB timeout has occured or not */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Runs after each encoder tick, check if activity occurred */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h1id="keyboard-idling-wake-code"tabindex="-1">Keyboard Idling/Wake Code <aclass="header-anchor"href="#keyboard-idling-wake-code"aria-label="Permalink to "Keyboard Idling/Wake Code""></a></h1><p>If the board supports it, it can be "idled", 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><h3id="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 <aclass="header-anchor"href="#example-suspend-power-down-user-and-suspend-wakeup-init-user-implementation"aria-label="Permalink to "Example `suspend_power_down_user()` and `suspend_wakeup_init_user()` Implementation""></a></h3><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> suspend_power_down_user</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // code will run multiple times while keyboard is suspended</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // code will run on keyboard wakeup</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="keyboard-suspend-wake-function-documentation"tabindex="-1">Keyboard suspend/wake Function Documentation <aclass="header-anchor"href="#keyboard-suspend-wake-function-documentation"aria-label="Permalink to "Keyboard suspend/wake Function Documentation""></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><h1id="keyboard-shutdown-reboot-code"tabindex="-1">Keyboard Shutdown/Reboot Code <aclass="header-anchor"href="#keyboard-shutdown-reboot-code"aria-label="Permalink to "Keyboard Shutdown/Reboot Code {#keyboard-shutdown-reboot-code}""></a></h1><p>This function gets called whenever the firmware is reset, whether it'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><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>Bootmagic does not trigger <code>shutdown_*()</code> as it happens before most of the initialization process.</p></div><h3id="example-shutdown-kb-implementation"tabindex="-1">Example <code>shutdown_kb()</code> Implementation <aclass="header-anchor"href="#example-shutdown-kb-implementation"aria-label="Permalink to "Example `shutdown_kb()` Implementation""></a></h3><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> shutdown_kb</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> jump_to_bootloader</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="keyboard-shutdown-reboot-function-documentation"tabindex="-1">Keyboard shutdown/reboot Function Documentation <aclass="header-anchor"href="#keyboard-shutdown-reboot-function-documentation"aria-label="Permalink to "Keyboard shutdown/reboot Function Documentation""></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><h1id="deferred-execution"tabindex="-1">Deferred Execution <aclass="header-anchor"href="#deferred-execution"aria-label="Permalink to "Deferred Execution {#deferred-execution}""></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><h2id="deferred-executor-callbacks"tabindex="-1">Deferred executor callbacks <aclass="header-anchor"href="#deferred-executor-callbacks"aria-label="Permalink to "Deferred executor callbacks""></a></h2><p>All <em>deferred executor callbacks</em> have a common function signature and look like:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint32_t</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> my_callback</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint32_t</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> trigger_time</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">cb_arg</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* do something */</span></span>
<spanclass="line"><spanstyle="--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 "catch-up" 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'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's done, by returning <code>0</code>.</p><divclass="tip custom-block"><pclass="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><h2id="deferred-executor-registration"tabindex="-1">Deferred executor registration <aclass="header-anchor"href="#deferred-executor-registration"aria-label="Permalink to "Deferred executor registration""></a></h2><p>Once a callback has been defined, it can be scheduled using the following API:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">deferred_token my_token </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> defer_exec</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1500</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, my_callback, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NULL</span><spanstyle="--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'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 "in flight" -- this can be increased by changing the limit, described below.</p><h2id="extending-a-deferred-execution"tabindex="-1">Extending a deferred execution <aclass="header-anchor"href="#extending-a-deferred-execution"aria-label="Permalink to "Extending a deferred execution""></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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--sh
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">extend_deferred_exec</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(my_token, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">800</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div><h2id="cancelling-a-deferred-execution"tabindex="-1">Cancelling a deferred execution <aclass="header-anchor"href="#cancelling-a-deferred-execution"aria-label="Permalink to "Cancelling a deferred execution""></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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// This will cancel my_token's future execution</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cancel_deferred_exec</span><spanstyle="--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><h2id="deferred-callback-limits"tabindex="-1">Deferred callback limits <aclass="header-anchor"href="#deferred-callback-limits"aria-label="Permalink to "Deferred callback limits""></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><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MAX_DEFERRED_EXECUTORS</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 16</span></span></code></pre></div><h1id="advanced-topics"tabindex="-1">Advanced topics <aclass="header-anchor"href="#advanced-topics"aria-label="Permalink to "Advanced topics {#advanced-topics}""></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're looking for.</p><h2id="layer-change-code"tabindex="-1">Layer Change Code <aclass="header-anchor"href="#layer-change-code"aria-label="Permalink to "Layer Change Code {#layer-change-code}""></a></h2><p><ahref="./feature_layers#layer-change-code">Layer change code</a></p><h2id="persistent-configuration-eeprom"tabindex="-1">Persistent Configuration (EEPROM) <aclass="header-anchor"href="#persistent-configuration-eeprom"aria-label="Permalink to "Persistent Configuration (EEPROM) {#persistent-configuration-eeprom}""></a></h2><p><ahref="./feature_eeprom">Persistent Configuration (EEPROM)</a></p></div></div></main><footerclass="VPDocFooter"data-v-39a288b8data-v-09de1c0f><!--[--><!--]--><divclass="edit-info"data-v-09de1c0f><divclass="edit-link"data-v-09de1c0f><aclass="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><!--[--><spanclass="vpi-square-pen edit-link-icon"data-v-09de1c0f></span> Edit this page<!--]--></a></div><!----></div><navclass="prev-next"data-v-09de1c0f><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link prev"href="/cli_tab_complete"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Previous page</span><spanclass="title"data-v-09de1c0f>Tab Completion</span><!--]--></a></div><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link next"href="/driver_installation_zadig"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Next page</span><spanclass="title"data-v-09de1c0f>Driver Installation with Zadig</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>