Skyler Grey | 967d67e | 2022-10-31 00:04:18 +0000 | [diff] [blame] | 1 | #| -------------------------------------------------------------------------- |
| 2 | |
| 3 | KMonad: Guided tour |
| 4 | |
| 5 | Welcome to the KMonad configuration tutorial. This document aims to explain: |
| 6 | 1. The configuration syntax |
| 7 | 2. The required information |
| 8 | 3. All possible configuration options |
| 9 | |
| 10 | This document should be a runnable configuration, so with some tweaks to the |
| 11 | `defcfg` block (see below) you should be able to try out all the examples |
| 12 | interactively. |
| 13 | |
| 14 | -------------------------------------------------------------------------- |# |
| 15 | |
| 16 | |
| 17 | #| -------------------------------------------------------------------------- |
| 18 | |
| 19 | Basic syntax: comments and parentheses |
| 20 | |
| 21 | KMonad's configuration language is styled on various lisps, like scheme or |
| 22 | Common Lisp. In a lisp, every statement is entered between '(' and ')'s. If |
| 23 | you are more used to Fortan style languages (python, ruby, C, Java, etc.), the |
| 24 | change is quite straightforward: the function name moves into the parentheses, |
| 25 | and you don't use commas to separate arguments. I.e. |
| 26 | |
| 27 | This: my_function(a, 3, "Alakazam") |
| 28 | Becomes: (my_function a 3 "Alakazam") |
| 29 | |
| 30 | The reason for this is because Lisp-style languages are very easy to parse and |
| 31 | write syntax-highlighters for. |
| 32 | |
| 33 | We also provide standard Lisp syntax for comments: |
| 34 | - block comments between: #| and its reverse |
| 35 | - line comments following: ;; |
| 36 | |
| 37 | Unlike standard lisp, a single ; does not denote a comment, but instead the |
| 38 | keycode for semicolon. |
| 39 | |
| 40 | Also, as you might have noticed, whitespace is possible anywhere. |
| 41 | |
| 42 | To check for syntax errors while editing, invoke kmonad with the -d option. |
| 43 | -------------------------------------------------------------------------- |# |
| 44 | |
| 45 | |
| 46 | #| -------------------------------------------------------------------------- |
| 47 | Necessary: the `defcfg` block |
| 48 | |
| 49 | There are a few bits of information that are required to be present in a |
| 50 | KMonad configuration file. One of these is the existence of exactly 1 `defcfg` |
| 51 | statement. This statement is used to customize various configuration settings. |
| 52 | Many of these settings have default values, but a minimal definition must |
| 53 | include at least an 'input' field and an 'output' field. These describe how |
| 54 | KMonad captures its inputs and how it emits its outputs. |
| 55 | |
| 56 | First, let's go over the optional, non-OS specific settings. Currently there is |
| 57 | only 2: |
| 58 | |
| 59 | - fallthrough: `true` or `false`, defaults to `false` |
| 60 | |
| 61 | KMonad catches input events and tries to match them to various handlers. If |
| 62 | it cannot match an event to any handler (for example, if it isn't included |
| 63 | in the `defsrc` block, or if it is, but the current keymap does not map any |
| 64 | buttons to it), then the event gets quietly ignored. If `fallthrough` is set |
| 65 | to `true`, any unhandled events simply get reemitted. |
| 66 | |
| 67 | - allow-cmd: `true` or `false`, defaults to `false` |
| 68 | |
| 69 | If this is set to `false`, any action that runs a shell-command will simply |
| 70 | log to `stdout` without ever running (log-level info). Don't ever enable |
| 71 | this on a configuration that you do not trust, because: |
| 72 | |
| 73 | (cmd-button "rm -rf ~/*") |
| 74 | |
| 75 | is a thing. For more information on the `cmd-button' function, see the |
| 76 | section on Command buttons below. |
| 77 | |
| 78 | There are also some optional OS specific settings that we support: |
| 79 | |
| 80 | - `cmp-seq': KEY, defaults to `RightAlt' (Linux X11 specific) |
| 81 | |
| 82 | This sets your compose key for Unicode input. For more information, as well |
| 83 | as a workaround to also make this work on windows, see the section on |
| 84 | Compose-key sequences below. |
| 85 | |
| 86 | - `cmp-seq-delay': NUMBER (in milliseconds) |
| 87 | |
| 88 | This sets a delay between each pressed key in a compose-key sequence. Some |
| 89 | environments may have troubles recognizing the key sequence if it's pressed |
| 90 | too rapidly; if you experience any problems in this direction, you can try |
| 91 | setting this value to `5' or `10' and see if that helps. |
| 92 | |
| 93 | Secondly, let's go over how to specify the `input` and `output` fields of a |
| 94 | `defcfg` block. This differs between OS'es, and so do the capabilities of |
| 95 | these interfaces. |
| 96 | |
| 97 | |
| 98 | -- Linux ------ |
| 99 | |
| 100 | In Linux we deal with input by performing an ioctl-grab on a specific |
| 101 | device-file. This allows us to hook KMonad on the input of exactly 1 keyboard, |
| 102 | and allows you to run multiple instances of KMonad for different keyboards. We |
| 103 | make an input using: |
| 104 | (device-file "/dev/input/by-id/my-keyboard-kbd") |
| 105 | |
| 106 | NOTE: Any valid path to a device-file will work, but it is recommended to use |
| 107 | the 'by-id' directory, since these names will not change if you replug the |
| 108 | device. |
| 109 | |
| 110 | We deal with output by creating a 'uinput' device. This requires that the |
| 111 | 'uinput' kernel module is loaded. The easiest way to ensure this is by calling |
| 112 | 'sudo modprobe uinput'. We create a uinput device using: |
| 113 | (uinput-sink "name" "optional post-init command") |
| 114 | |
| 115 | |
| 116 | -- Windows ---- |
| 117 | |
| 118 | In Windows we do not get such fine-grained control. We use a low-level |
| 119 | keyboard hook to intercept all non-injected keyboard events. There is |
| 120 | currently an open issue to improve the C-bindings used to capture windows |
| 121 | keyevents, and if you have a better way to approach this issue, help is deeply |
| 122 | appreciated. You specify a windows input using: |
| 123 | (low-level-hook) |
| 124 | |
| 125 | Similarly, the output in Windows lacks the fine-grained control. We use the |
| 126 | SendEvent API to emit key events directly to Windows. Since these are |
| 127 | 'artificial' events we won't end up catching them again by the |
| 128 | `low-level-hook`. It is very likely that KMonad does not play well with other |
| 129 | programs that capture keyboard input like AHK. You specify windows output using: |
| 130 | (send-event-sink) |
| 131 | |
| 132 | |
| 133 | -- Mac OS ----- |
| 134 | |
| 135 | For Mac questions I suggest filing an issue and tagging @thoelze1, he wrote |
| 136 | the MacOS API. However, input using: |
| 137 | (iokit-name "optional product string") |
| 138 | |
| 139 | By default this should grab all keyboards, however if a product string is |
| 140 | provided, KMonad will only capture those devices that match the provided |
| 141 | product string. If you would like to provide a product string, you can run |
| 142 | `make; ./list-keyboards' in c_src/mac to list the product strings of all |
| 143 | connected keyboards. |
| 144 | |
| 145 | You initialize output on MacOS using: |
| 146 | (kext) |
| 147 | |
| 148 | -------------------------------------------------------------------------- |# |
| 149 | |
| 150 | (defcfg |
| 151 | ;; For Linux |
| 152 | input (device-file "/dev/input/by-id/usb-04d9_daskeyboard-event-kbd") |
| 153 | output (uinput-sink "My KMonad output" |
| 154 | ;; To understand the importance of the following line, see the section on |
| 155 | ;; Compose-key sequences at the near-bottom of this file. |
| 156 | "/run/current-system/sw/bin/sleep 1 && /run/current-system/sw/bin/setxkbmap -option compose:ralt") |
| 157 | cmp-seq ralt ;; Set the compose key to `RightAlt' |
| 158 | cmp-seq-delay 5 ;; 5ms delay between each compose-key sequence press |
| 159 | |
| 160 | ;; For Windows |
| 161 | ;; input (low-level-hook) |
| 162 | ;; output (send-event-sink) |
| 163 | |
| 164 | ;; For MacOS |
| 165 | ;; input (iokit-name "my-keyboard-product-string") |
| 166 | ;; output (kext) |
| 167 | |
| 168 | ;; Comment this if you want unhandled events not to be emitted |
| 169 | fallthrough true |
| 170 | |
| 171 | ;; Set this to false to disable any command-execution in KMonad |
| 172 | allow-cmd true |
| 173 | ) |
| 174 | |
| 175 | |
| 176 | #| -------------------------------------------------------------------------- |
| 177 | Necessary: the `defsrc` block |
| 178 | |
| 179 | It is difficult to explain the `defsrc` block without immediately going into |
| 180 | `deflayer` blocks as well. Essentially, KMonad maps input-events to various |
| 181 | internal actions, many of which generate output events. The `defsrc` block |
| 182 | explains the layout on which we specify our `deflayer`s down the line. |
| 183 | |
| 184 | It is important to realize that the `defsrc` block doesn't *necessarily* have |
| 185 | to coincide with your actual input keyboard. You can specify a full 100% |
| 186 | `defsrc` block, but only use a 40% keyboard. This will mean that every |
| 187 | `deflayer` you specify will also have to match your 100% `defsrc`, and that |
| 188 | your actual keyboard would be physically unable to trigger about 60% of your |
| 189 | keymap, but it would be perfectly valid syntax. |
| 190 | |
| 191 | The dual of this (and more useful) is that it is also perfectly valid to only |
| 192 | specify that part of your keyboard in `defsrc` that you want to remap. If you |
| 193 | use a 100% keyboard, but don't want to remap the numpad at all you can simply |
| 194 | leave the numpad out of your `defsrc`, and it should work just fine. In that |
| 195 | particular case you probably want to set `fallthrough` to `true` in your |
| 196 | `defcfg` block though. |
| 197 | |
| 198 | In the future we would like to provide support for multiple, named `defsrc` |
| 199 | blocks, so that it becomes easier to specify various layers for just the |
| 200 | numpad, for example, but at the moment any more or less than 1 `defsrc` block |
| 201 | will result in an error. |
| 202 | |
| 203 | The layouting in the `defsrc` block is completely free, whitespace simply gets |
| 204 | ignored. We strive to provide a name for every keycode that is no longer than |
| 205 | 4 characters, so we find that laying out your keymap in columns of 5 works out |
| 206 | quite nicely (although wider columns will allow for more informative aliases, |
| 207 | see below). |
| 208 | |
| 209 | Most keycodes should be obvious. If you are unsure, check |
| 210 | './src/KMonad/Keyboard/Keycode.hs'. Every Keycode has a name corresponding to |
| 211 | its Keycode name, but all lower-case and with the 'Key' prefix removed. There |
| 212 | are also various aliases for Keycodes starting around line 350. If you are |
| 213 | trying to bind a key and there is not a 4-letter alias, please file an issue, |
| 214 | or better yet, a pull-request, and it will be added promptly. |
| 215 | |
| 216 | Also, you can consult './keymap/template/' for various input templates to use |
| 217 | directly or to look up keycodes by position. Here we use the input-template |
| 218 | for 'us_ansi_60.kbd' |
| 219 | |
| 220 | -------------------------------------------------------------------------- |# |
| 221 | |
| 222 | (defsrc |
| 223 | grv 1 2 3 4 5 6 7 8 9 0 - = bspc |
| 224 | tab q w e r t y u i o p [ ] \ |
| 225 | caps a s d f g h j k l ; ' ret |
| 226 | lsft z x c v b n m , . / rsft |
| 227 | lctl lmet lalt spc ralt rmet cmp rctl |
| 228 | ) |
| 229 | |
| 230 | |
| 231 | #| -------------------------------------------------------------------------- |
| 232 | Optional : `defalias` statements |
| 233 | |
| 234 | KMonad will let you specify some very specific, crazy buttons. These |
| 235 | definitions can get pretty long, though, and would make `deflayer` blocks |
| 236 | nearly impossible to read. Therefore we provide the ability to alias names to |
| 237 | these buttons, to keep the actual `deflayer` statements orderly. |
| 238 | |
| 239 | A `defalias` can contain any number of aliases, and it can refer backwards or |
| 240 | forwards to layers without issue. The only sequencing that needs to be kept in |
| 241 | mind is that a `defalias` cannot refer forward to another `defalias` that is |
| 242 | not yet defined. |
| 243 | |
| 244 | Here we define a few aliases, but we will define more later. Notice that we |
| 245 | try to only use 3 letter names for aliases. If that is not enough to be clear, |
| 246 | consider widening all columns to 6 or 7 characters (or be content with a messy |
| 247 | config). |
| 248 | |
| 249 | -------------------------------------------------------------------------- |# |
| 250 | |
| 251 | (defalias |
| 252 | num (layer-toggle numbers) ;; Bind num to a button that switches to a layer |
| 253 | kil C-A-del ;; Bind kil to a button that Ctrl-Alt-deletes |
| 254 | ) |
| 255 | |
| 256 | |
| 257 | #| NOTE: The above code could just as easily have been written as: |
| 258 | (defalias num (layer-toggle numbers)) |
| 259 | (defalias kil C-A-del) |
| 260 | |# |
| 261 | |
| 262 | |
| 263 | #| -------------------------------------------------------------------------- |
| 264 | Necessary: at least 1 `deflayer` block |
| 265 | |
| 266 | As explained in the `defsrc` section, a `deflayer` will define a button for |
| 267 | each corresponding entry in the `defsrc` definition. A `deflayer` statement |
| 268 | consists of the `deflayer` keyword, followed by the name used to identify this |
| 269 | layer, followed by N 'statements-that-evaluate-to-a-button', where N is |
| 270 | exactly how many entries are defined in the `defsrc` statement. |
| 271 | |
| 272 | It is also important to mention that the 'keymap' in KMonad is modelled as a |
| 273 | stack of layers (just like in QMK). When an event is registered we look in the |
| 274 | top-most layer for a handler. If we don't find one we try the next layer, and |
| 275 | then the next. |
| 276 | |
| 277 | Exactly what 'evaluates-to-a-button' will be expanded on in more detail below. |
| 278 | There are very many different specialist buttons in KMonad that we will touch |
| 279 | upon. However, for now, these 4 are a good place to begin: |
| 280 | |
| 281 | 1. Any keycode evaluates to a button that, on press, emits the press of that |
| 282 | keycode, and on release, emits the release of that keycode. Just a 'normal' |
| 283 | button. The exception is '\', which gets used as an escape character. Use |
| 284 | '\\' instead. Other characters that need to be escaped to match the literal |
| 285 | character are '(', ')', and '_'. |
| 286 | |
| 287 | 2. An @-prefixed name evaluates to an alias lookup. We named two buttons in |
| 288 | the `defalias` block above, we could now refer to these buttons using |
| 289 | `@num` and `@kil`. This is also why we only use alias-names no longer than |
| 290 | 3 characters in this tutorial. Also, note that we are already referencing |
| 291 | some aliases that have not yet been defined, this is not an issue. |
| 292 | |
| 293 | 3. The '_' character evaluates to transparent. I.e. no handler for that |
| 294 | key-event in this layer, causing this event to be handed down the layer |
| 295 | stack to perhaps be handled by the next layer. |
| 296 | |
| 297 | 4. The 'XX' character evaluates to blocked. I.e. no action bound to that |
| 298 | key-event in this layer, but do actually catch event, preventing any |
| 299 | underlying layer from handling it. |
| 300 | |
| 301 | Finally, it is important to note that the *first* `deflayer` statement in a |
| 302 | KMonad config will be the layer that is active when KMonad starts up. |
| 303 | |
| 304 | -------------------------------------------------------------------------- |# |
| 305 | |
| 306 | |
| 307 | (deflayer qwerty |
| 308 | grv 1 2 3 4 5 6 7 8 9 0 - = bspc |
| 309 | tab q w e r t y u i o p [ ] \ |
| 310 | caps a s d f g h j k l ; ' ret |
| 311 | lsft z x c v b n m , . / rsft |
| 312 | lctl @num lalt spc ralt rmet @sym @tst |
| 313 | ) |
| 314 | |
| 315 | |
| 316 | #| -------------------------------------------------------------------------- |
| 317 | Optional: as many layers as you please |
| 318 | |
| 319 | We had already defined `num` as referring to a `(layer-toggle numbers)`. We |
| 320 | will get into layer-manipulation soon, but first, let's just create a second |
| 321 | layer that overlays a numpad under our right-hand. |
| 322 | |
| 323 | To easily specify layers it is highly recommended to create an empty |
| 324 | `deflayer` statement as a comment at the top of your config, so you can simply |
| 325 | copy-paste this template. There are also various empty layer templates |
| 326 | available in the './keymap/template' directory. |
| 327 | |
| 328 | -------------------------------------------------------------------------- |# |
| 329 | |
| 330 | (deflayer numbers |
| 331 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 332 | _ _ _ _ _ XX / 7 8 9 - _ _ _ |
| 333 | _ _ _ _ _ XX * 4 5 6 + _ _ |
| 334 | _ _ \( \) . XX 0 1 2 3 _ _ |
| 335 | _ _ _ _ _ _ _ _ |
| 336 | ) |
| 337 | |
| 338 | |
| 339 | #| -------------------------------------------------------------------------- |
| 340 | Optional: modded buttons |
| 341 | |
| 342 | Let's start by exploring the various special buttons that are supported by |
| 343 | KMonad by looking at 'modded' buttons, that is to say, buttons that activate |
| 344 | some kind of 'mod', then perform some button, and finally release that 'mod' |
| 345 | again. |
| 346 | |
| 347 | We have already seen an example of this style of button, our `kil` button is |
| 348 | one such button. Let's look at it in more detail: |
| 349 | C-A-del |
| 350 | |
| 351 | This looks like a simple declarative statement, but it's helpful to realize |
| 352 | that is simply syntactic sugar around 2 function calls. This statement is |
| 353 | equivalent to: |
| 354 | (around ctl (around alt del)) |
| 355 | |
| 356 | This highlights a core design principle in KMonad: we try to provide very |
| 357 | simple buttons, and then we provide rules and functions for combining them |
| 358 | into new buttons. Although note: still very much a work in progress. |
| 359 | |
| 360 | So, looking at this statement: |
| 361 | (around foo bar) |
| 362 | |
| 363 | Here, `around` is a function that takes two buttons and creates a new button. |
| 364 | This new button will, on a press, first press foo, then press bar, and on a |
| 365 | release first release bar, and then foo. Once created, this new button can be |
| 366 | passed to anything in KMonad that expects a button. |
| 367 | |
| 368 | We have already seen other examples of modded buttons, \(, \), *, and +. There |
| 369 | are no Keycodes for these buttons in KMonad, but they are buttons. They simply |
| 370 | evaluate to `(around lsft x)`. All shifted numbers have their corresponding |
| 371 | characters, the same is true for all capitals, and < > : ~ " | { } \_ + and ?. |
| 372 | |
| 373 | To wrap up 'modded-buttons', let's look back at C-A-del. We have 8 variants: |
| 374 | C- : (around lctl X) |
| 375 | A- : (around lalt X) |
| 376 | M- : (around lmet X) |
| 377 | S- : (around lsft X) |
| 378 | |
| 379 | Then RC-, RA-, RM-, and RS- behave exactly the same, except using the |
| 380 | right-modifier. |
| 381 | |
| 382 | These can be combined however you please: |
| 383 | C-A-M-S-x ;; Perfectly valid |
| 384 | C-% ;; Perfectly valid: same as C-S-5 |
| 385 | C-RC-RA-A-M-S-RS-m ;; Sure, but why would you? |
| 386 | |
| 387 | Also, note that although we provide special syntax for certain modifiers, |
| 388 | these buttons are in no way 'special' in KMonad. There is no concept of 'modifier'. |
| 389 | (around a (around b c)) ;; Perfectly valid |
| 390 | |
| 391 | -------------------------------------------------------------------------- |# |
| 392 | |
| 393 | (defalias |
| 394 | |
| 395 | ;; Something useful |
| 396 | cpy C-c |
| 397 | pst C-v |
| 398 | cut C-x |
| 399 | |
| 400 | ;; Something silly |
| 401 | md1 (around a (around b c)) ;; abc |
| 402 | md2 (around a (around lsft b)) ;; aB |
| 403 | md3 C-A-M-S-l |
| 404 | md4 (around % b) ;; BEWARE: %B, not %b, do you see why? |
| 405 | ) |
| 406 | |
| 407 | #| -------------------------------------------------------------------------- |
| 408 | Optional: sticky keys |
| 409 | |
| 410 | KMonad also support so called "sticky keys". These are keys that will |
| 411 | behave as if they were pressed after just tapping them. This behaviour |
| 412 | wears off after the next button is pressed, which makes them ideal for |
| 413 | things like a quick control or shift. For example, tapping a sticky and |
| 414 | then pressing `abc' will result in `Abc'. |
| 415 | |
| 416 | You can create these keys with the `sticky-key' keyword: |
| 417 | |
| 418 | (defalias |
| 419 | slc (sticky-key 500 lctl)) |
| 420 | |
| 421 | The number after `sticky-key' is the timeout you want, in milliseconds. If |
| 422 | a key is tapped and that time has passed, it won't act like it's pressed |
| 423 | down when we receive the next keypress. |
| 424 | |
| 425 | It is also possible to combine sticky keys. For example, to |
| 426 | get a sticky shift+control you can do |
| 427 | |
| 428 | (defalias |
| 429 | ssc (around |
| 430 | (sticky-key 500 lsft) |
| 431 | (sticky-key 500 lctl))) |
| 432 | |
| 433 | -------------------------------------------------------------------------- |# |
| 434 | |
| 435 | ;; Let's make both shift keys sticky |
| 436 | (defalias |
| 437 | sl (sticky-key 300 lsft) |
| 438 | sr (sticky-key 300 rsft)) |
| 439 | |
| 440 | |
| 441 | ;; Now we define the 'tst' button as opening and closing a bunch of layers at |
| 442 | ;; the same time. If you understand why this works, you're starting to grok |
| 443 | ;; KMonad. |
| 444 | ;; |
| 445 | ;; Explanation: we define a bunch of testing-layers with buttons to illustrate |
| 446 | ;; the various options in KMonad. Each of these layers makes sure to have its |
| 447 | ;; buttons not overlap with the buttons from the other layers, and specifies all |
| 448 | ;; its other buttons as transparent. When we use the nested `around` statement, |
| 449 | ;; whenever we push the button linked to '@tst' (check `qwerty` layer, we bind |
| 450 | ;; it to `rctl`), any button we press when holding `rctl` will be pressed in the |
| 451 | ;; context of those 4 layers overlayed on the stack. When we release `rctl`, all |
| 452 | ;; these layers will be popped again. |
| 453 | (defalias tst (around (layer-toggle macro-test) |
| 454 | (around (layer-toggle layer-test) |
| 455 | (around (layer-toggle around-next-test) |
| 456 | (around (layer-toggle command-test) |
| 457 | (layer-toggle modded-test)))))) |
| 458 | |
| 459 | (deflayer modded-test |
| 460 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 461 | _ _ @md4 _ _ _ _ _ _ _ _ _ _ _ |
| 462 | _ _ @md1 @md2 @md3 _ _ _ _ _ _ _ _ |
| 463 | _ _ @cut @cpy @pst _ _ _ _ _ _ _ |
| 464 | _ _ _ _ _ _ _ _ |
| 465 | ) |
| 466 | |
| 467 | #| -------------------------------------------------------------------------- |
| 468 | Optional: tap-macros |
| 469 | |
| 470 | Let's look at a button we haven't seen yet, tap-macros. |
| 471 | |
| 472 | `tap-macro` is a function that takes an arbitrary number of buttons and |
| 473 | returns a new button. When this new button is pressed it rapidly taps all its |
| 474 | stored buttons in quick succesion except for its last button, which it only |
| 475 | presses. This last button gets released when the `tap-macro` gets released. |
| 476 | |
| 477 | There are two ways to define a `tap-macro`, using the `tap-macro` function |
| 478 | directly, or through the #() syntactic sugar. Both evaluate to exactly the |
| 479 | same button. |
| 480 | |
| 481 | (tap-macro K M o n a d) |
| 482 | #(K M o n a d) |
| 483 | |
| 484 | If you are going to use a `tap-macro` to perform a sequence of actions inside |
| 485 | some program you probably want to include short pauses between inputs to give |
| 486 | the program time to register all the key-presses. Therefore we also provide |
| 487 | the 'pause' function, which simply pauses processing for a certain amount of |
| 488 | milliseconds. Pauses can be created like this: |
| 489 | |
| 490 | (pause 20) |
| 491 | P20 |
| 492 | |
| 493 | You can also pause between each key stroke by specifying the `:delay' keyword, |
| 494 | as well as a time in ms, at the end of a `tap-macro': |
| 495 | |
| 496 | (tap-macro K M o n a d :delay 5) |
| 497 | #(K M o n a d :delay 5) |
| 498 | |
| 499 | The above would be equivalent to e.g. |
| 500 | |
| 501 | (tap-macro K P5 M P5 o P5 n P5 a P5 d) |
| 502 | |
| 503 | The `tap-macro-release` is like `tap-macro`, except that it |
| 504 | waits to press the last button when the `tap-macro-release` |
| 505 | gets released. It might be useful when combined with a |
| 506 | footswitch that sends keybooard scan codes. |
| 507 | |
| 508 | (tap-macro-release i K M o n a d esc) |
| 509 | |
| 510 | WARNING: DO NOT STORE YOUR PASSWORDS IN PLAIN TEXT OR IN YOUR KEYBOARD |
| 511 | |
| 512 | I know it might be tempting to store your password as a macro, but there are 2 |
| 513 | huge risks: |
| 514 | 1. You accidentally leak your config and expose your password |
| 515 | 2. Anyone who knows about the button can get clear-text representation of your |
| 516 | password with any text editor, shell, or text-input field. |
| 517 | |
| 518 | Support for triggering shell commands directly from KMonad is described in the |
| 519 | command buttons section below. |
| 520 | |
| 521 | This concludes this public service announcement. |
| 522 | |
| 523 | -------------------------------------------------------------------------- |# |
| 524 | |
| 525 | (defalias |
| 526 | mc1 #(K M o n a d) |
| 527 | mc2 #(C-c P50 A-tab P50 C-v) ;; Careful, this might do something |
| 528 | mc3 #(P200 h P150 4 P100 > < P50 > < P20 0 r z 1 ! 1 ! !) |
| 529 | mc4 (tap-macro a (pause 50) @md2 (pause 50) c) |
| 530 | mc5 (tap-macro-release esc esc esc) |
| 531 | mc6 #(@mc3 spc @mc3 spc @mc3) |
| 532 | ) |
| 533 | |
| 534 | (deflayer macro-test |
| 535 | _ @mc1 @mc2 @mc3 @mc4 @mc5 @mc6 _ _ _ _ _ _ _ |
| 536 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 537 | _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 538 | _ _ _ _ _ _ _ _ _ _ _ _ |
| 539 | _ _ _ _ _ _ _ _ |
| 540 | ) |
| 541 | |
| 542 | |
| 543 | #| -------------------------------------------------------------------------- |
| 544 | Optional: layer manipulation |
| 545 | |
| 546 | You have already seen the basics of layer-manipulation. The `layer-toggle` |
| 547 | button. This button adds a layer to the top of KMonad's layer stack when |
| 548 | pressed, and removes it again when released. There are a number of other ways |
| 549 | to manipulate the layer stack, some safer than others. Let's go through all of |
| 550 | them from safest to least safe: |
| 551 | |
| 552 | `layer-toggle` works as described before, 2 things to note: |
| 553 | 1. If you are confused or worried about pressing a key, changing layers, and |
| 554 | then releasing a key and this causing issues: don't be. KMonad handles |
| 555 | presses and releases in very different ways. Presses get passed directly to |
| 556 | the stacked keymap as previously described. When a KMonad button has its |
| 557 | press-action triggered, it then registers a callback that will catch its |
| 558 | own release before we ever touch the keymap. This guarantees that the |
| 559 | button triggered by the press of X *will be* the button whose release is |
| 560 | triggered by the release of X (the release of X might trigger other things |
| 561 | as well, but that is besides the point.) |
| 562 | 2. If `layer-toggle` can only ever add and then necessarily remove 1 layer |
| 563 | from the stack, then it will never cause a permanent change, and is |
| 564 | perfectly safe. |
| 565 | |
| 566 | `layer-delay`, once pressed, temporarily switches to some layer for some |
| 567 | milliseconds. Just like `layer-toggle` this will never permanently mess-up the |
| 568 | layer stack. This button was initially implemented to provide some |
| 569 | 'leader-key' style behavior. Although I think in the future better solutions |
| 570 | will be available. For now this will temporarily add a layer to the top of the |
| 571 | stack: |
| 572 | (layer-delay 500 my-layer) |
| 573 | |
| 574 | `layer-next`, once pressed, primes KMonad to handle the next press from some |
| 575 | arbitrary layer. This aims to fill the same usecase as `layer-delay`: the |
| 576 | beginnings of 'leader-key' style behavior. I think this whole button will get |
| 577 | deleted soon, because the more general `around-next` now exists (see below) |
| 578 | and this is nothing more than: |
| 579 | (around-next (layer-toggle layer-name)) |
| 580 | Until then though, use `layer-next` like this: |
| 581 | (layer-next layer-name) |
| 582 | |
| 583 | `layer-switch`: change the base-layer of KMonad. As described at the top of |
| 584 | this document, the first `deflayer` statement is the layer that is active when |
| 585 | KMonad starts. Since `layer-toggle` can only ever add on and remove from the |
| 586 | top of that, it can never change the base-layer. The following button will |
| 587 | unregister the bottom-most layer of the keymap, and replace it with another |
| 588 | layer. |
| 589 | (layer-switch my-layer) |
| 590 | |
| 591 | This is where things start getting potentially dangerous (i.e. get KMonad into |
| 592 | an unusuable state until a restart has occured). It is perfectly possible to |
| 593 | switch into a layer that you can never get out of. Or worse, you could |
| 594 | theoretically have a layer full of only `XX`s and switch into that, rendering |
| 595 | your keyboard unuseable until you somehow manage to kill KMonad (without using |
| 596 | your keyboard). |
| 597 | |
| 598 | However, when handled well, `layer-switch` is very useful, letting you switch |
| 599 | between 'modes' for your keyboard. I have a tiny keyboard with a weird keymap, |
| 600 | but I switch into a simple 'qwerty' keymap shifted 1 button to the right for |
| 601 | gaming. Just make sure that any 'mode' you switch into has a button that |
| 602 | allows you to switch back out of the 'mode' (or content yourself restarting |
| 603 | KMonad somehow). |
| 604 | |
| 605 | `layer-add` and `layer-rem`. This is where you can very quickly cause yourself |
| 606 | a big headache. Originally I didn't expose these operations, but someone |
| 607 | wanted to use them, and I am not one to deny someone else a chainsaw. As the |
| 608 | names might give away: |
| 609 | (layer-add name) ;; Add a layer to the top of the stack |
| 610 | (layer-rem name) ;; Remove a layer by name (noop if no such layer) |
| 611 | |
| 612 | To use `layer-add` and `layer-rem` well, you should take a moment to think |
| 613 | about how to create a layout that will prevent you from getting into |
| 614 | situations where you enter a key-configuration you cannot get out of again. |
| 615 | These two operations together, however, are very useful for activating a |
| 616 | permanent overlay for a while. This technique is illustrated in the tap-hold |
| 617 | overlay a bit further down. |
| 618 | |
| 619 | |
| 620 | -------------------------------------------------------------------------- |# |
| 621 | |
| 622 | (defalias |
| 623 | |
| 624 | yah (layer-toggle asking-for-trouble) ;; Completely safe |
| 625 | nah (layer-add asking-for-trouble) ;; Completely unsafe |
| 626 | |
| 627 | ld1 (layer-delay 500 numbers) ;; One way to get a leader-key |
| 628 | ld2 (layer-next numbers) ;; Another way to get a leader key |
| 629 | |
| 630 | ;; NOTE, this is safe because both `qwerty` and `colemak` contain the `@tst` |
| 631 | ;; button which will get us to the `layer-test` layer, which itself contains |
| 632 | ;; both `@qwe` and `@col`. |
| 633 | qwe (layer-switch qwerty) ;; Set qwerty as the base layer |
| 634 | col (layer-switch colemak) ;; Set colemak as the base layer |
| 635 | ) |
| 636 | (deflayer layer-test |
| 637 | @qwe _ _ _ _ _ _ _ _ _ _ @add _ @nah |
| 638 | @col _ _ _ _ _ _ _ _ _ _ _ _ @yah |
| 639 | _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 640 | _ _ _ _ _ _ _ _ _ @ld1 @ld2 _ |
| 641 | _ _ _ _ _ _ _ _ |
| 642 | ) |
| 643 | |
| 644 | ;; Exactly like qwerty, but with the letters switched around |
| 645 | (deflayer colemak |
| 646 | grv 1 2 3 4 5 6 7 8 9 0 - = bspc |
| 647 | tab q w f p g j l u y ; [ ] \ |
| 648 | @xcp a r s t d h n e i o ' ret |
| 649 | @sl z x c v b k m , . / @sr |
| 650 | lctl @num lalt spc ralt rmet @sym @tst |
| 651 | ) |
| 652 | |
| 653 | (defalias lol #(: - D)) |
| 654 | |
| 655 | ;; Contrived example |
| 656 | (deflayer asking-for-trouble |
| 657 | @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol |
| 658 | @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol |
| 659 | @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol |
| 660 | @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol @lol |
| 661 | @lol @lol @lol @lol @lol @lol @lol @lol |
| 662 | ) |
| 663 | |
| 664 | ;; One way to safely use layer-add and layer-rem: the button bound to layer-add |
| 665 | ;; is the same button bound to layer-rem in the layer that `add` adds to the |
| 666 | ;; stack. I.e., it becomes impossible to add or remove multiple copies of a |
| 667 | ;; layer. |
| 668 | (defalias |
| 669 | add (layer-add multi-overlay) ;; multi-overlay is defined in the next |
| 670 | rem (layer-rem multi-overlay) ;; section below this |
| 671 | ) |
| 672 | |
| 673 | #| -------------------------------------------------------------------------- |
| 674 | Optional: Multi-use buttons |
| 675 | |
| 676 | Perhaps one of the most useful features of KMonad, where a lot of work has |
| 677 | gone into, but also an area with many buttons that are ever so slightly |
| 678 | different. The naming and structuring of these buttons might change sometime |
| 679 | soon, but for now, this is what there is. |
| 680 | |
| 681 | For the next section being able to talk about examples is going to be handy, |
| 682 | so consider the following scenario and mini-language that will be the same |
| 683 | between scenarios. |
| 684 | - We have some button `foo` that will be different between scenarios |
| 685 | - `foo` is bound to 'Esc' on the input keyboard |
| 686 | - the letters a s d f are bound to themselves |
| 687 | - Px signifies the press of button x on the keyboard |
| 688 | - Rx signifies the release of said button |
| 689 | - Tx signifies the sequential and near instantaneous press and release of x |
| 690 | - 100 signifies 100ms pass |
| 691 | |
| 692 | So for example: |
| 693 | Tesc Ta: |
| 694 | tap of 'Esc' (triggering `foo`), tap of 'a' triggering `a` |
| 695 | Pesc 100 Ta Tb Resc: |
| 696 | press of 'Esc', 100ms pause, tap of 'a', tap of 'b', release of 'Esc' |
| 697 | |
| 698 | The `tap-next` button takes 2 buttons, one for tapping, one for holding, and |
| 699 | combines them into a single button. When pressed, if the next event is its own |
| 700 | release, we tap the 'tapping' button. In all other cases we first press the |
| 701 | 'holding' button then we handle the event. Then when the `tap-next` gets |
| 702 | released, we release the 'holding' button. |
| 703 | |
| 704 | So, using our mini-language, we set foo to: |
| 705 | (tap-next x lsft) |
| 706 | Then: |
| 707 | Tesc -> x |
| 708 | Tesc Ta -> xa |
| 709 | Pesc Ta Resc -> A |
| 710 | Pesc Ta Tr Resc -> AR |
| 711 | |
| 712 | The `tap-hold` button is very similar to `tap-next` (a theme, trust me). The |
| 713 | difference lies in how the decision is made whether to tap or hold. A |
| 714 | `tap-hold` waits for a particular timeout, if the `tap-hold` is released |
| 715 | anywhere before that moment we execute a tap immediately. If the timeout |
| 716 | occurs and the `tap-hold` is still held, we switch to holding mode. |
| 717 | |
| 718 | The additional feature of a `tap-hold` is that it pauses event-processing |
| 719 | until it makes its decision and then rolls back processing when the decision |
| 720 | has been made. |
| 721 | |
| 722 | So, again with the mini-language, we set foo to: |
| 723 | (tap-hold 200 x lsft) ;; Like tap-next, but with a 200ms timeout |
| 724 | Then: |
| 725 | Tesc -> x |
| 726 | Tesc Ta -> xa |
| 727 | Pesc 300 a -> A (the moment you press a) |
| 728 | Pesc a 300 -> A (after 200 ms) |
| 729 | Pesc a 100 Resc -> xa (both happening immediately on Resc) |
| 730 | |
| 731 | The `tap-hold-next` button is a combination of the previous 2. Essentially, |
| 732 | think of it as a `tap-next` button, but it also switches to held after a |
| 733 | period of time. This is useful, because if you have a (tap-next ret ctl) for |
| 734 | example, and you press it thinking you want to press C-v, but then you change |
| 735 | your mind, you now cannot release the button without triggering a 'ret', that |
| 736 | you then have to backspace. With the `tap-hold-next` button, you simply |
| 737 | outwait the delay, and you're good. I see no benefit of `tap-next` over |
| 738 | `tap-hold-next` with a decent timeout value. |
| 739 | |
| 740 | You can use the `:timeout-button` keyword to specify a button other than the |
| 741 | hold button which should be held when the timeout expires. For example, we |
| 742 | can construct a button which types one x when tapped, multiple x's when held, |
| 743 | and yet still acts as shift when another button is pressed before the timeout |
| 744 | expires. So, using the minilanguage and foo as: |
| 745 | (tap-hold-next 200 x lsft :timeout-button x) |
| 746 | Then: |
| 747 | Tesc -> Tx |
| 748 | Pesc 100 a -> A (the moment you press a) |
| 749 | Pesc 5000 Resc -> xxxxxxx (some number of auto-repeated x's) |
| 750 | |
| 751 | Note that KMonad does not itself auto-repeat the key. In this last example, |
| 752 | KMonad emits 200 Px 4800 Rx, and the operating system's auto-repeat feature, |
| 753 | if any, emits multiple x's because it sees that the x key is held for 4800 ms. |
| 754 | |
| 755 | The `tap-next-release` is like `tap-next`, except it decides whether to tap or |
| 756 | hold based on the next release of a key that was *not* pressed before us. This |
| 757 | also performs rollback like `tap-hold`. So, using the minilanguage and foo as: |
| 758 | (tap-next-release x lsft) |
| 759 | Then: |
| 760 | Tesc Ta -> xa |
| 761 | Pa Pesc Ra Resc -> ax (because 'a' was already pressed when we started, so |
| 762 | foo decides it is tapping) |
| 763 | Pesc Ta Resc -> A (because a was pressed *and* released after we started, |
| 764 | so foo decides it is holding) |
| 765 | |
| 766 | These increasingly stranger buttons are, I think, coming from the stubborn |
| 767 | drive of some of my more eccentric (and I mean that in the most positive way) |
| 768 | users to make typing with modifiers on the home-row more comfortable. |
| 769 | Especially layouts that encourage a lot of rolling motions are nicer to use |
| 770 | with the `release` style buttons. |
| 771 | |
| 772 | The `tap-hold-next-release` (notice a trend?) is just like `tap-next-release`, |
| 773 | but it comes with an additional timeout that, just like `tap-hold-next` will |
| 774 | jump into holding-mode after a timeout. |
| 775 | |
| 776 | I honestly think that `tap-hold-next-release`, although it seems the most |
| 777 | complicated, probably is the most comfortable to use. But I've put all of them |
| 778 | in a testing layer down below, so give them a go and see what is nice. |
| 779 | |
| 780 | -------------------------------------------------------------------------- |# |
| 781 | |
| 782 | |
| 783 | (defalias |
| 784 | xtn (tap-next x lsft) ;; Shift that does 'x' on tap |
| 785 | xth (tap-hold 400 x lsft) ;; Long delay for easier testing |
| 786 | thn (tap-hold-next 400 x lsft) |
| 787 | tnr (tap-next-release x lsft) |
| 788 | tnh (tap-hold-next-release 2000 x lsft) |
| 789 | |
| 790 | ;; Used it the colemak layer |
| 791 | xcp (tap-hold-next 400 esc ctl) |
| 792 | ) |
| 793 | |
| 794 | ;; Some of the buttons used here are defined in the next section |
| 795 | (deflayer multi-overlay |
| 796 | @mt _ _ _ _ _ _ _ _ _ _ _ @rem _ |
| 797 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 798 | @thn _ _ _ _ _ _ _ _ _ _ _ _ |
| 799 | @xtn _ _ _ _ _ _ _ _ _ _ @xth |
| 800 | @tnr _ _ _ _ _ _ @tnh |
| 801 | ) |
| 802 | |
| 803 | |
| 804 | #| -------------------------------------------------------------------------- |
| 805 | Optional: Multi-tap |
| 806 | |
| 807 | Besides the tap-hold style buttons there is another multi-use button (with. |
| 808 | only 1 variant, at the moment). The `multi-tap`. |
| 809 | |
| 810 | A `multi-tap` codes for different buttons depending on how often it is tapped. |
| 811 | It is defined by a series of delays and buttons, followed by a last button |
| 812 | without delay. As long as you tap the `multi-tap` within the delay specified, |
| 813 | it will jump to the next button. Once the delay is exceeded the selected |
| 814 | button is pressed. If the last button in the list is reached, it is |
| 815 | immediately pressed. When another key is pressed down while we're tapping, |
| 816 | `multi-tap' also immediately exits and taps the current button. |
| 817 | |
| 818 | Note that you can actually hold the button, so in the below example, going: |
| 819 | tap-tap-hold (wait 300ms) will get you a pressed c, until you release again. |
| 820 | |
| 821 | -------------------------------------------------------------------------- |# |
| 822 | |
| 823 | (defalias |
| 824 | mt (multi-tap 300 a 300 b 300 c 300 d e)) |
| 825 | |
| 826 | |
| 827 | #| -------------------------------------------------------------------------- |
| 828 | Optional: Around-next |
| 829 | |
| 830 | The `around-next` function creates a button that primes KMonad to perform the |
| 831 | next button-press inside some context. This could be the context of 'having |
| 832 | Shift pressed' or 'being inside some layer' or, less usefully, 'having d |
| 833 | pressed'. It is a more general and powerful version of `layer-next`. |
| 834 | |
| 835 | There is also an `around-next-timeout` button that does the same thing as |
| 836 | `around-next`, except that if some other button press is not detected within |
| 837 | some timeout, some other button is tapped. This can be used to create a |
| 838 | leader-key that simply times out (by passing a non-button), or a key that can |
| 839 | still function as a normal key, but also as a leader key when used slowly. |
| 840 | |
| 841 | I think expansion of this button-style is probably the future of leader-key, |
| 842 | hydra-style functionality support in KMonad. |
| 843 | |
| 844 | -------------------------------------------------------------------------- |# |
| 845 | |
| 846 | (defalias |
| 847 | ns (around-next sft) ;; Shift the next press |
| 848 | nnm (around-next @num) ;; Perform next press in numbers layer |
| 849 | ntm (around-next-timeout 500 sft XX) |
| 850 | |
| 851 | |
| 852 | ) |
| 853 | |
| 854 | (deflayer around-next-test |
| 855 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 856 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 857 | @ns _ _ _ _ _ _ _ _ _ _ _ _ |
| 858 | @nnm _ _ _ _ _ _ _ _ _ _ _ |
| 859 | @ntm _ _ _ _ _ _ _ |
| 860 | ) |
| 861 | |
| 862 | #| -------------------------------------------------------------------------- |
| 863 | Optional: Compose-key sequences |
| 864 | |
| 865 | Compose-key sequences are series of button-presses that your operating system |
| 866 | will interpret as the insertion of a special character, like accented |
| 867 | characters, or various special-languages. In that sense, they are just |
| 868 | syntactic sugar for keyboard macros. |
| 869 | |
| 870 | To get this to work on Linux you will need to set your compose-key with a tool |
| 871 | like `setxkbmap', as well as tell kmonad that information. See the `defcfg' |
| 872 | block at the top of this file for a working example. Note that you need to |
| 873 | wait ever so slightly for the keyboard to register with linux before the |
| 874 | command gets executed, that's why the `sleep 1`. Also, note that all the |
| 875 | `/run/current-system' stuff is because the author uses NixOS. Just find a |
| 876 | shell-command that will: |
| 877 | |
| 878 | 1. Sleep a moment |
| 879 | 2. Set the compose-key to your desired key |
| 880 | |
| 881 | Please be aware that what `setxkbmap' calls the `menu' key is not actually the |
| 882 | `menu' key! If you want to use the often suggested |
| 883 | |
| 884 | setxkbmap -option compose:menu |
| 885 | |
| 886 | you will have to set your compose key within kmonad to `compose' and not |
| 887 | `menu'. |
| 888 | |
| 889 | After this, this should work out of the box under Linux. Windows does not |
| 890 | recognize the same compose-key sequences, but WinCompose will make most of the |
| 891 | sequences line up with KMonad: http://wincompose.info/ |
| 892 | This has not in any way been tested on Mac. |
| 893 | |
| 894 | In addition to hard-coded symbols, we also provide 'uncompleted' macros. Since |
| 895 | a compose-key sequence is literally just a series of keystrokes, we can omit |
| 896 | the last one, and enter the sequence for 'add an umlaut' and let the user then |
| 897 | press some letter to add this umlaut to. These are created using the `+"` |
| 898 | syntax. |
| 899 | |
| 900 | -------------------------------------------------------------------------- |# |
| 901 | |
| 902 | (defalias |
| 903 | sym (layer-toggle symbols) |
| 904 | |
| 905 | ) |
| 906 | |
| 907 | (deflayer symbols |
| 908 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 909 | _ ä é © _ _ _ _ _ _ _ _ _ _ |
| 910 | _ +' +~ +` +^ _ _ _ _ _ _ _ _ |
| 911 | _ +" +, _ _ _ _ _ _ _ _ _ |
| 912 | _ _ _ _ _ _ _ _) |
| 913 | |
| 914 | |
| 915 | #| -------------------------------------------------------------------------- |
| 916 | Optional: Command buttons |
| 917 | |
| 918 | Currently we also provide the ability to launch arbitrary shell-commands from |
| 919 | inside kmonad. These commands are simply handed off to the command-shell |
| 920 | without any further checking or waiting. |
| 921 | |
| 922 | NOTE: currently only tested on Linux, but should work on any platform, as long |
| 923 | as the command is valid for that platform. |
| 924 | |
| 925 | The `cmd-button' function takes two arguments, the second one of which is |
| 926 | optional. These represent the commands to be executed on pressing and |
| 927 | releasing the button respectively. |
| 928 | |
| 929 | BEWARE: never run anyone's configuration without looking at it. You wouldn't |
| 930 | want to push: |
| 931 | |
| 932 | (cmd-button "rm -rf ~/*") ;; Delete all this user's data |
| 933 | |
| 934 | |
| 935 | -------------------------------------------------------------------------- |# |
| 936 | |
| 937 | (defalias |
| 938 | dat (cmd-button "date >> /tmp/kmonad_example.txt") ;; Append date to tmpfile |
| 939 | pth (cmd-button "echo $PATH > /tmp/kmonad_path.txt") ;; Write out PATH |
| 940 | ;; `dat' on press and `pth' on release |
| 941 | bth (cmd-button "date >> /tmp/kmonad_example.txt" |
| 942 | "echo $PATH > /tmp/kmonad_path.txt") |
| 943 | ) |
| 944 | |
| 945 | (deflayer command-test |
| 946 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 947 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 948 | _ _ _ _ _ _ _ _ _ _ _ _ _ |
| 949 | _ _ _ _ _ _ _ _ _ @dat @pth _ |
| 950 | _ _ _ _ _ _ _ _ |
| 951 | ) |