unicode.md (16785B)
1 # Unicode {#unicode} 2 3 With a little help from your OS, practically any Unicode character can be input using your keyboard. 4 5 ## Caveats {#caveats} 6 7 There are some limitations to this feature. Because there is no "standard" method of Unicode input across all operating systems, each of them require their own setup process on both the host *and* in the firmware, which may involve installation of additional software. This also means Unicode input will not "just work" when the keyboard is plugged into another device. 8 9 ## Usage {#usage} 10 11 The core Unicode API can be used purely programmatically. However, there are also additional subsystems which build on top of it and come with keycodes to make things easier. See below for more details. 12 13 Add the following to your keymap's `rules.mk`: 14 15 ```make 16 UNICODE_COMMON = yes 17 ``` 18 19 ## Basic Configuration {#basic-configuration} 20 21 Add the following to your `config.h`: 22 23 |Define |Default |Description | 24 |------------------------|------------------|--------------------------------------------------------------------------------| 25 |`UNICODE_KEY_MAC` |`KC_LEFT_ALT` |The key to hold when beginning a Unicode sequence with the macOS input mode | 26 |`UNICODE_KEY_LNX` |`LCTL(LSFT(KC_U))`|The key to tap when beginning a Unicode sequence with the Linux input mode | 27 |`UNICODE_KEY_WINC` |`KC_RIGHT_ALT` |The key to hold when beginning a Unicode sequence with the WinCompose input mode| 28 |`UNICODE_SELECTED_MODES`|`-1` |A comma separated list of input modes for cycling through | 29 |`UNICODE_CYCLE_PERSIST` |`true` |Whether to persist the current Unicode input mode to EEPROM | 30 |`UNICODE_TYPE_DELAY` |`10` |The amount of time to wait, in milliseconds, between Unicode sequence keystrokes| 31 32 ### Audio Feedback {#audio-feedback} 33 34 If you have the [Audio](audio) feature enabled on your board, you can configure it to play sounds when the input mode is changed. 35 36 Add the following to your `config.h`: 37 38 |Define |Default|Description | 39 |-------------------|-------|-----------------------------------------------------------| 40 |`UNICODE_SONG_MAC` |*n/a* |The song to play when the macOS input mode is selected | 41 |`UNICODE_SONG_LNX` |*n/a* |The song to play when the Linux input mode is selected | 42 |`UNICODE_SONG_BSD` |*n/a* |The song to play when the BSD input mode is selected | 43 |`UNICODE_SONG_WIN` |*n/a* |The song to play when the Windows input mode is selected | 44 |`UNICODE_SONG_WINC`|*n/a* |The song to play when the WinCompose input mode is selected| 45 46 ## Input Subsystems {#input-subsystems} 47 48 Each of these subsystems have their own pros and cons in terms of flexibility and ease of use. Choose the one that best fits your needs. 49 50 ::::tabs 51 52 === Basic 53 54 This is the easiest to use, albeit somewhat limited. It supports code points up to `U+7FFF`, which covers characters for most modern languages (including East Asian), as well as many symbols, but does not include emoji. 55 56 To enable Basic Unicode, add the following to your `rules.mk`: 57 58 ```make 59 UNICODE_ENABLE = yes 60 ``` 61 62 You can then add `UC(c)` keycodes to your keymap, where *c* is the code point of the desired character (in hexadecimal - the `U+` prefix will not work). For example, `UC(0x40B)` will output [Ћ](https://unicode-table.com/en/040B/), and `UC(0x30C4)` will output [ツ](https://unicode-table.com/en/30C4). 63 64 === Unicode Map 65 66 Unicode Map supports all possible code points (up to `U+10FFFF`). Here, the code points are stored in a separate mapping table (which may contain at most 16,384 entries), instead of directly in the keymap. 67 68 To enable Unicode Map, add the following to your `rules.mk`: 69 70 ```make 71 UNICODEMAP_ENABLE = yes 72 ``` 73 74 Then, you will need to create a mapping table in your `keymap.c`, and (optionally) an enum for naming the array indices, like so: 75 76 ```c 77 enum unicode_names { 78 BANG, 79 IRONY, 80 SNEK 81 }; 82 83 const uint32_t PROGMEM unicode_map[] = { 84 [BANG] = 0x203D, // ‽ 85 [IRONY] = 0x2E2E, // ⸮ 86 [SNEK] = 0x1F40D, // 🐍 87 }; 88 ``` 89 90 Finally, add `UM(i)` keycodes to your keymap, where *i* is an index into the `unicode_map[]` array. If you defined the enum above, you can use those names instead, for example `UM(BANG)` or `UM(SNEK)`. 91 92 #### Lower and Upper Case Pairs {#unicodemap-pairs} 93 94 Some writing systems have lowercase and uppercase variants of each character, such as å and Å. To make inputting these characters easier, you can use the `UP(i, j)` keycode in your keymap, where *i* and *j* are the mapping table indices of the lowercase and uppercase characters, respectively. If you're holding down Shift or have Caps Lock turned on when you press the key, the uppercase character will be inserted; otherwise, the lowercase character will be inserted. 95 96 ```c 97 const uint32_t PROGMEM unicode_map[] = { 98 [AE_LOWER] = 0x00E6, // æ 99 [AE_UPPER] = 0x00C6, // Æ 100 }; 101 ``` 102 103 This is most useful when creating a keymap for an international layout with special characters. Instead of having to put the lower and upper case versions of a character on separate keys, you can have them both on the same key. This helps blend Unicode keys in with regular keycodes. 104 105 Due to keycode size constraints, *i* and *j* can each only refer to one of the first 128 characters in your `unicode_map`. In other words, 0 ≤ *i* ≤ 127 and 0 ≤ *j* ≤ 127. 106 107 === UCIS 108 109 As with Unicode Map, the UCIS method also supports all possible code points, and requires the use of a mapping table. However, it works much differently - Unicode characters are input by replacing a typed mnemonic. 110 111 To enable UCIS, add the following to your keymap's `rules.mk`: 112 113 ```make 114 UCIS_ENABLE = yes 115 ``` 116 117 Then, create a mapping table in your `keymap.c`: 118 119 ```c 120 const ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE( 121 UCIS_SYM("poop", 0x1F4A9), // 💩 122 UCIS_SYM("rofl", 0x1F923), // 🤣 123 UCIS_SYM("ukr", 0x1F1FA, 0x1F1E6), // 🇺🇦 124 UCIS_SYM("look", 0x0CA0, 0x005F, 0x0CA0) // ಠ_ಠ 125 ); 126 ``` 127 128 By default, each table entry may be up to three code points long. This can be changed by adding `#define UCIS_MAX_CODE_POINTS n` to your keymap's `config.h`. 129 130 To invoke UCIS input, the `ucis_start()` function must first be called (for example, in a custom "Unicode" keycode). Then, type the mnemonic for the mapping table entry (such as "rofl"), and hit Space or Enter. The "rofl" text will be backspaced and the emoji inserted. 131 132 :::: 133 134 ## Input Modes {#input-modes} 135 136 Unicode input works by typing a sequence of characters, similar to a macro. However, since this sequence depends on your OS, you will need to prepare both your host machine and QMK to recognise and send the correct Unicode input sequences respectively. 137 138 To set the list of enabled input modes, add the `UNICODE_SELECTED_MODES` define to your keymap's `config.h`, for example: 139 140 ```c 141 #define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX 142 // or 143 #define UNICODE_SELECTED_MODES UNICODE_MODE_MACOS, UNICODE_MODE_WINCOMPOSE 144 ``` 145 146 These modes can then be cycled through using the `UC_NEXT` and `UC_PREV` keycodes. You can also switch to any input mode, even if it is not specified in `UNICODE_SELECTED_MODES`, using their respective keycodes. 147 148 If your keyboard has working EEPROM, it will remember the last used input mode and continue using it on the next power up. This can be disabled by defining `UNICODE_CYCLE_PERSIST` to `false`. 149 150 :::::tabs 151 152 ==== macOS 153 154 **Mode Name:** `UNICODE_MODE_MACOS` 155 156 macOS has built-in support for Unicode input as its own input source. It supports all possible code points by way of surrogate pairs for code points above `U+FFFF`. 157 158 To enable, go to **System Preferences → Keyboard → Input Sources**, then add Unicode Hex Input to the list (under Other), and activate it from the input dropdown in the menu bar. Note that this may disable some Option-based shortcuts such as Option+Left and Option+Right. 159 160 ==== Linux (IBus) 161 162 **Mode Name:** `UNICODE_MODE_LINUX` 163 164 For Linux distros with IBus, Unicode input is enabled by default, supports all possible code points, and works almost anywhere. Without IBus, it works under GTK apps, but rarely anywhere else. 165 166 Users who would like support in non-GTK apps without IBus may need to resort to a more indirect method, such as creating a custom keyboard layout. 167 168 ==== Windows (WinCompose) 169 170 **Mode Name:** `UNICODE_MODE_WINCOMPOSE` 171 172 This mode requires a third-party tool called [WinCompose](https://github.com/samhocevar/wincompose). It supports all possible code points, and is the recommended input mode for Windows. 173 174 To enable, install the [latest release from GitHub](https://github.com/samhocevar/wincompose/releases/latest). Once installed, it will automatically run on startup. This works reliably under all versions of Windows supported by WinCompose. 175 176 ==== Windows (HexNumpad) 177 178 **Mode Name:** `UNICODE_MODE_WINDOWS` 179 180 ::: warning 181 This input mode is *not* the "Alt code" system. Alt codes are not Unicode; they instead follow [the Windows-1252 character set](https://en.wikipedia.org/wiki/Alt_code). 182 ::: 183 184 This is Windows' built-in hex numpad Unicode input mode. It only supports code points up to `U+FFFF`, and is not recommended due to reliability and compatibility issues. 185 186 To enable, run the following as an administrator, then reboot: 187 188 ``` 189 reg add "HKCU\Control Panel\Input Method" -v EnableHexNumpad -t REG_SZ -d 1 190 ``` 191 192 ==== Emacs 193 194 **Mode Name:** `UNICODE_MODE_EMACS` 195 196 Emacs supports code point input with the `insert-char` command. 197 198 ==== BSD 199 200 **Mode Name:** `UNICODE_MODE_BSD` 201 202 Not currently implemented. If you're a BSD user and want to contribute support for this input mode, please [feel free](../contributing)! 203 204 ::::: 205 206 ## Keycodes {#keycodes} 207 208 |Key |Aliases |Description | 209 |----------------------------|---------|----------------------------------------------------------------| 210 |`UC(c)` | |Send Unicode code point `c`, up to `0x7FFF` | 211 |`UM(i)` | |Send Unicode code point at index `i` in `unicode_map` | 212 |`UP(i, j)` | |Send Unicode code point at index `i`, or `j` if Shift/Caps is on| 213 |`QK_UNICODE_MODE_NEXT` |`UC_NEXT`|Cycle through selected input modes | 214 |`QK_UNICODE_MODE_PREVIOUS` |`UC_PREV`|Cycle through selected input modes in reverse | 215 |`QK_UNICODE_MODE_MACOS` |`UC_MAC` |Switch to macOS input | 216 |`QK_UNICODE_MODE_LINUX` |`UC_LINX`|Switch to Linux input | 217 |`QK_UNICODE_MODE_WINDOWS` |`UC_WIN` |Switch to Windows input | 218 |`QK_UNICODE_MODE_BSD` |`UC_BSD` |Switch to BSD input (not implemented) | 219 |`QK_UNICODE_MODE_WINCOMPOSE`|`UC_WINC`|Switch to Windows input using WinCompose | 220 |`QK_UNICODE_MODE_EMACS` |`UC_EMAC`|Switch to emacs (`C-x-8 RET`) | 221 222 ## API {#api} 223 224 ### `uint8_t get_unicode_input_mode(void)` {#api-get-unicode-input-mode} 225 226 Get the current Unicode input mode. 227 228 #### Return Value {#api-get-unicode-input-mode-return-value} 229 230 The currently active Unicode input mode. 231 232 --- 233 234 ### `void set_unicode_input_mode(uint8_t mode)` {#api-set-unicode-input-mode} 235 236 Set the Unicode input mode. 237 238 #### Arguments {#api-set-unicode-input-mode-arguments} 239 240 - `uint8_t mode` 241 The input mode to set. 242 243 --- 244 245 ### `void unicode_input_mode_step(void)` {#api-unicode-input-mode-step} 246 247 Change to the next Unicode input mode. 248 249 --- 250 251 ### `void unicode_input_mode_step_reverse(void)` {#api-unicode-input-mode-step-reverse} 252 253 Change to the previous Unicode input mode. 254 255 --- 256 257 ### `void unicode_input_mode_set_user(uint8_t input_mode)` {#api-unicode-input-mode-set-user} 258 259 User-level callback, invoked when the input mode is changed. 260 261 #### Arguments {#api-unicode-input-mode-set-user-arguments} 262 263 - `uint8_t input_mode` 264 The new input mode. 265 266 --- 267 268 ### `void unicode_input_mode_set_kb(uint8_t input_mode)` {#api-unicode-input-mode-set-kb} 269 270 Keyboard-level callback, invoked when the input mode is changed. 271 272 #### Arguments {#api-unicode-input-mode-set-kb-arguments} 273 274 - `uint8_t input_mode` 275 The new input mode. 276 277 --- 278 279 ### `void unicode_input_start(void)` {#api-unicode-input-start} 280 281 Begin the Unicode input sequence. The exact behavior depends on the currently selected input mode: 282 283 - **macOS**: Hold `UNICODE_KEY_MAC` 284 - **Linux**: Tap `UNICODE_KEY_LNX` 285 - **WinCompose**: Tap `UNICODE_KEY_WINC`, then U 286 - **HexNumpad**: Hold Left Alt, then tap Numpad + 287 - **Emacs**: Tap Ctrl+X, then 8, then Enter 288 289 This function is weakly defined, and can be overridden in user code. 290 291 --- 292 293 ### `void unicode_input_finish(void)` {#api-unicode-input-finish} 294 295 Complete the Unicode input sequence. The exact behavior depends on the currently selected input mode: 296 297 - **macOS**: Release `UNICODE_KEY_MAC` 298 - **Linux**: Tap Space 299 - **WinCompose**: Tap Enter 300 - **HexNumpad**: Release Left Alt 301 - **Emacs**: Tap Enter 302 303 This function is weakly defined, and can be overridden in user code. 304 305 --- 306 307 ### `void unicode_input_cancel(void)` {#api-unicode-input-cancel} 308 309 Cancel the Unicode input sequence. The exact behavior depends on the currently selected input mode: 310 311 - **macOS**: Release `UNICODE_KEY_MAC` 312 - **Linux**: Tap Escape 313 - **WinCompose**: Tap Escape 314 - **HexNumpad**: Release Left Alt 315 - **Emacs**: Tap Ctrl+G 316 317 This function is weakly defined, and can be overridden in user code. 318 319 --- 320 321 ### `void register_unicode(uint32_t code_point)` {#api-register-unicode} 322 323 Input a single Unicode character. A surrogate pair will be sent if required by the input mode. 324 325 #### Arguments {#api-register-unicode-arguments} 326 327 - `uint32_t code_point` 328 The code point of the character to send. 329 330 --- 331 332 ### `void send_unicode_string(const char *str)` {#api-send-unicode-string} 333 334 Send a string containing Unicode characters. 335 336 #### Arguments {#api-send-unicode-string-arguments} 337 338 - `const char *str` 339 The string to send. 340 341 --- 342 343 ### `uint8_t unicodemap_index(uint16_t keycode)` {#api-unicodemap-index} 344 345 Get the index into the `unicode_map` array for the given keycode, respecting shift state for pair keycodes. 346 347 #### Arguments {#api-unicodemap-index-arguments} 348 349 - `uint16_t keycode` 350 The Unicode Map keycode to get the index of. 351 352 #### Return Value {#api-unicodemap-index-return-value} 353 354 An index into the `unicode_map` array. 355 356 --- 357 358 ### `uint32_t unicodemap_get_code_point(uint8_t index)` {#api-unicodemap-get-code-point} 359 360 Get the code point for the given index in the `unicode_map` array. 361 362 #### Arguments {#unicodemap-get-code-point-arguments} 363 364 - `uint8_t index` 365 The index into the `unicode_map` array. 366 367 #### Return Value {#unicodemap-get-code-point-return-value} 368 369 A Unicode code point value. 370 371 --- 372 373 ### `void register_unicodemap(uint8_t index)` {#api-register-unicodemap} 374 375 Send the code point for the given index in the `unicode_map` array. 376 377 #### Arguments {#api-register-unicodemap-arguments} 378 379 - `uint8_t index` 380 The index into the `unicode_map` array. 381 382 --- 383 384 ### `void ucis_start(void)` {#api-ucis-start} 385 386 Begin the input sequence. 387 388 --- 389 390 ### `bool ucis_active(void)` {#api-ucis-active} 391 392 Whether UCIS is currently active. 393 394 #### Return Value {#api-ucis-active-return-value} 395 396 `true` if UCIS is active. 397 398 --- 399 400 ### `uint8_t ucis_count(void)` {#api-ucis-count} 401 402 Get the number of characters in the input sequence buffer. 403 404 #### Return Value {#api-ucis-count-return-value} 405 406 The current input sequence buffer length. 407 408 --- 409 410 ### `bool ucis_add(uint16_t keycode)` {#api-ucis-add} 411 412 Add the given keycode to the input sequence buffer. 413 414 #### Arguments {#api-ucis-add-arguments} 415 416 - `uint16_t keycode` 417 The keycode to add. Must be between `KC_A` and `KC_Z`, or `KC_1` and `KC_0`. 418 419 #### Return Value {#api-ucis-add-return-value} 420 421 `true` if the keycode was added. 422 423 --- 424 425 ### `bool ucis_remove_last(void)` {#api-ucis-remove-last} 426 427 Remove the last character from the input sequence buffer. 428 429 #### Return Value {#api-ucis-remove-last-return-value} 430 431 `true` if the sequence was not empty. 432 433 --- 434 435 ### `void ucis_finish(void)` {#api-ucis-finish} 436 437 Mark the input sequence as complete, and attempt to match. 438 439 --- 440 441 ### `void ucis_cancel(void)` {#api-ucis-cancel} 442 443 Cancel the input sequence. 444 445 --- 446 447 ### `void register_ucis(void)` {#api-register-ucis} 448 449 Send the code point(s) for the given UCIS index. 450 451 #### Arguments {#api-register-ucis-arguments} 452 453 - `uint8_t index` 454 The index into the UCIS symbol table.