keyboard stuff
1# Debugging FAQ
2
3This page details various common questions people have about troubleshooting their keyboards.
4
5## Debugging {#debugging}
6
7Your keyboard will output debug information if you have `CONSOLE_ENABLE = yes` in your `rules.mk`. By default the output is very limited, but you can turn on debug mode to increase the amount of debug output. Use the `DB_TOGG` keycode in your keymap, use the [Command](features/command) feature to enable debug mode, or add the following code to your keymap.
8
9```c
10void keyboard_post_init_user(void) {
11 // Customise these values to desired behaviour
12 debug_enable=true;
13 debug_matrix=true;
14 //debug_keyboard=true;
15 //debug_mouse=true;
16}
17```
18
19## Debugging Tools
20
21Various tools are available to debug your keyboard.
22
23### Debugging With QMK Toolbox
24
25For compatible platforms, [QMK Toolbox](https://github.com/qmk/qmk_toolbox) can be used to display debug messages from your keyboard.
26
27### Debugging with QMK CLI
28
29Prefer a terminal based solution? The [QMK CLI console command](cli_commands#qmk-console) can be used to display debug messages from your keyboard.
30
31### Debugging With hid_listen
32
33Something stand-alone? [hid_listen](https://www.pjrc.com/teensy/hid_listen.html), provided by PJRC, can also be used to display debug messages. Prebuilt binaries for Windows,Linux,and MacOS are available.
34
35## Sending Your Own Debug Messages {#debug-api}
36
37Sometimes it's useful to print debug messages from within your [custom code](custom_quantum_functions). Doing so is pretty simple. Start by including `print.h` at the top of your file:
38
39```c
40#include "print.h"
41```
42
43After that you can use a few different print functions:
44
45* `print("string")`: Print a simple string.
46* `uprintf("%s string", var)`: Print a formatted string
47* `dprint("string")` Print a simple string, but only when debug mode is enabled
48* `dprintf("%s string", var)`: Print a formatted string, but only when debug mode is enabled
49
50## Debug Examples
51
52Below is a collection of real world debugging examples. For additional information, refer to [Debugging/Troubleshooting QMK](faq_debug).
53
54### Which matrix position is this keypress?
55
56When porting, or when attempting to diagnose pcb issues, it can be useful to know if a keypress is scanned correctly. To enable logging for this scenario, add the following code to your keymaps `keymap.c`
57
58```c
59bool process_record_user(uint16_t keycode, keyrecord_t *record) {
60 // If console is enabled, it will print the matrix position and status of each key pressed
61#ifdef CONSOLE_ENABLE
62 uprintf("KL: kc: 0x%04X, col: %2u, row: %2u, pressed: %u, time: %5u, int: %u, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count);
63#endif
64 return true;
65}
66```
67
68Example output
69```
70Waiting for device:.......
71Listening:
72KL: kc: 169, col: 0, row: 0, pressed: 1, time: 15505, int: 0, count: 0
73KL: kc: 169, col: 0, row: 0, pressed: 0, time: 15510, int: 0, count: 0
74KL: kc: 174, col: 1, row: 0, pressed: 1, time: 15703, int: 0, count: 0
75KL: kc: 174, col: 1, row: 0, pressed: 0, time: 15843, int: 0, count: 0
76KL: kc: 172, col: 2, row: 0, pressed: 1, time: 16303, int: 0, count: 0
77KL: kc: 172, col: 2, row: 0, pressed: 0, time: 16411, int: 0, count: 0
78```
79
80### Which keycode is this keypress?
81
82Keycodes are logged in the example above as numerical codes, which may be difficult to interpret. For more readable logging, add `KEYCODE_STRING_ENABLE = yes` in your `rules.mk` and use `get_keycode_string(kc)`. For example:
83
84```c
85uprintf("kc: %s\n", get_keycode_string(keycode));
86```
87
88This logs the keycode as a human-readable string like "`LT(2,KC_D)`" rather than a numerical code like "`0x4207`." See the [Keycode String](unit_testing#keycode-string) section of the Unit Testing page for more information.
89
90
91### How long did it take to scan for a keypress?
92
93When testing performance issues, it can be useful to know the frequency at which the switch matrix is being scanned. To enable logging for this scenario, add the following code to your keymaps `config.h`
94
95```c
96#define DEBUG_MATRIX_SCAN_RATE
97```
98
99Example output
100```
101 > matrix scan frequency: 315
102 > matrix scan frequency: 313
103 > matrix scan frequency: 316
104 > matrix scan frequency: 316
105 > matrix scan frequency: 316
106 > matrix scan frequency: 316
107```
108
109## `hid_listen` Can't Recognize Device
110When debug console of your device is not ready you will see like this:
111
112```
113Waiting for device:.........
114```
115
116Once the device is plugged in then *hid_listen* finds it you will get this message:
117
118```
119Waiting for new device:.........................
120Listening:
121```
122
123If you can't get this 'Listening:' message try building with `CONSOLE_ENABLE=yes` in [Makefile]
124
125You may need privileges to access the device an OS like Linux. Try `sudo hid_listen`.
126
127On many Linux distros you can avoid having to run hid_listen as root
128by creating a file called `/etc/udev/rules.d/70-hid-listen.rules` with
129the following content:
130
131```
132SUBSYSTEM=="hidraw", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="def1", TAG+="uaccess", RUN{builtin}+="uaccess"
133```
134
135Replace abcd and def1 with your keyboard's vendor and product id,
136letters must be lowercase. The `RUN{builtin}+="uaccess"` part is only
137needed for older distros.
138
139
140## Can't Get Message on Console
141Check:
142- *hid_listen* finds your device. See above.
143- Enable debug by pressing **Magic**+d. See [Magic Commands](https://github.com/tmk/tmk_keyboard/wiki#magic-commands-for-debug).
144- Set `debug_enable=true`. See [Debugging](#debugging)
145- Try using `print` function instead of debug print. See **common/print.h**.
146- Disconnect other devices with console function. See [Issue #97](https://github.com/tmk/tmk_keyboard/issues/97).
147- Ensure all strings end with a newline character (`\n`). QMK Toolbox prints console output on a per-line basis.