This document describes how to create a kanata configuration file. The kanata configuration file will determine your keyboard behaviour upon running kanata.
The configuration file uses S-expression syntax from Lisps. If you are not familiar with any Lisp-like programming language, do not be too worried. This document will hopefully be a sufficient guide to help you customize your keyboard behaviour to your exact liking.
If you have any questions or confusions, feel free to file an issue or start a discussion. If you have ideas for how to improve this document or any other part of the project, please be welcome to make a pull request or file an issue.
- Table of contents
- Comments
- Required configuration entries
- Non-US keyboards
- Optional defcfg entries
- defcfg
- process-unmapped-keys
- danger-enable-cmd
- sequence-timeout
- sequence-input-mode
- Linux only: linux-dev
- log-layer-changes
- Linux only: linux-continue-if-no-devs-found
- Linux only: linux-unicode-u-code
- Linux only: linux-unicode-termination
- Windows only: windows-altgr
- Windows only: windows-interception-mouse-hwid
- Using multiple defcfg entries
- Aliases and variables
- Actions
- Global overrides
- Advanced/weird features
You can add comments to your configuration file. Comments are prefixed with two semicolons. E.g:
;; This is a comment in a kanata configuration file. ;; There is no special syntax for multi-line comments at this time. ;; Comments will be ignored and are intended for you to help understand your ;; own configuration when reading it later.
You can begin a multi-line comment block with #|
and end it with |#
:
#| This is a multi-line comment block |#
Your configuration file must have exactly one defsrc
entry. This defines the
order of keys that the deflayer
entries will operate on.
A defsrc
entry is composed of defsrc
followed by key names that are
separated by whitespace.
It should be noted that the defsrc
entry is treated as a long sequence; the
amount of whitespace (spaces, tabs, newlines) are not relevant. You may use
spaces, tabs, or newlines however you like to visually format defsrc
to your
liking.
The the primary source of all key names is the str_to_oscode function in the source code. Please feel free to file an issue if you’re unable to find the key you’re looking for.
An example defsrc
containing the US QWERTY keyboard keys as an
approximately 60% keyboard layout:
(defsrc grv 1 2 3 4 5 6 7 8 9 0 - = bspc tab q w e r t y u i o p [ ] \ caps a s d f g h j k l ; ' ret lsft z x c v b n m , . / rsft lctl lmet lalt spc ralt rmet rctl )
For non-US keyboards, see this section.
Your configuration file must have at least one deflayer
entry. This defines
how each physical key mapped in defsrc
behaves when kanata runs.
A deflayer
configuration entry is followed by the layer name then a list of
keys or actions. The usable key names are the same as in defsrc. Actions are
explained further on in this document. The whitespace story is the same as with
defsrc
. The order of keys/actions in deflayer
corresponds to the
physical key in the same sequence position defined in defsrc
.
The first layer defined in your configuration file will be the starting layer when kanata runs. Other layers can be temporarily activated or switched to using actions. There is currently a maximum of 25 layers allowed.
An example defsrc
and deflayer
that remaps QWERTY to the Dvorak layout
would be:
(defsrc grv 1 2 3 4 5 6 7 8 9 0 - = bspc tab q w e r t y u i o p [ ] \ caps a s d f g h j k l ; ' ret lsft z x c v b n m , . / rsft lctl lmet lalt spc ralt rmet rctl ) (deflayer dvorak grv 1 2 3 4 5 6 7 8 9 0 [ ] bspc tab ' , . p y f g c r l / = \ caps a o e u i d h t n s - ret lsft ; q j k x b m w v z rsft lctl lmet lalt spc ralt rmet rctl )
If you’re reading in order, you have now seen all of the required entries:
-
defsrc
-
deflayer
An example minimal configuration is:
(defsrc a b c) (deflayer start 1 2 3)
This will make kanata remap your a b c
keys to 1 2 3
. This is almost
certainly undesirable but is a valid configuration.
For non-US keyboard users, you may have some keys on your keyboard with characters
that are not allowed in defsrc
by default, at least according to the symbol
shown. You can use deflocalkeys
to define additional key names that can be
used in defsrc
, deflayer
and anywhere else in the configuration.
There are three variants of deflocalkeys:
-
deflocalkeys-win
-
deflocalkeys-wintercept
-
deflocalkeys-linux
Only one of each deflocalkeys-* variant is allowed. The variants that are not
applicable will be ignored, e.g. deflocalkeys-linux
and deflocalkeys-wintercept
are both ignored when using the default Windows kanata binary.
You can find configurations that others have made in this document. If you do not see your keyboard there and are not confident in using the available tools, please feel welcome to ask for help in a discussion or issue. Please contribute to the document if you are able!
Example:
(deflocalkeys-win ì 187 ) (deflocalkeys-wintercept ì 187 ) (deflocalkeys-linux ì 13 ) (defsrc grv 1 2 3 4 5 6 7 8 9 0 - ì bspc )
The number used for a custom key represents the converted value for an OsCode in base 10. This differs between Windows-hooks, Windows-interception, and Linux.
In Linux, evtest
will give the correct number for the physical key you press.
In Windows using the default hook mechanism, the non-interception version of the keyboard tester in the kanata repository will give the correct number. (prebuilt binary)
In Windows using Interception, the interception version of the keyboard tester will give the correct number. Between the hook and interception versions, some keys may agree but others may not; do be aware that they are not compatible!
Ideas for improving the user-friendliness of this system are welcome! As mentioned before, please ask for help in an issue or discussion if needed, and help with this document is very welcome so that future users can have an easier time 🙂.
Your configuration file may include a single defcfg
entry.
It can be empty but there are options that can change kanata’s behaviour that will be described in this section.
Example:
(defcfg)
Enabling this configuration makes kanata process keys that are not in defsrc. This is useful if you are only mapping a few keys in defsrc instead of most of the keys on your keyboard.
Without this, some actions like rpt
, tap-hold-release
, one-shot
,
will not work correctly for subsequent key presses that are not in defsrc.
This is disabled by default. The reason this is not enabled by default is because some keys may not work correctly if they are intercepted. For example, see the Windows only: windows-altgr option below.
Example:
(defcfg process-unmapped-keys yes )
This option can be used to enable the cmd
action in your configuration. The
cmd
action allows kanata to execute programs with arguments passed to them.
This requires using a kanata program that is compiled with the cmd
action
enabled. The reason for this is so that if you choose to, there is no way for
kanata to execute arbitrary programs even if you download some random
configuration from the internet.
This configuration is disabled by default and can be enabled by giving it the
value yes
.
Example:
(defcfg danger-enable-cmd yes )
This option customizes the key sequence timeout (unit: ms). Its default value is 1000. The purpose of this item is explained in Sequences.
Example:
(defcfg sequence-timeout 2000 )
This option customizes the key sequence input mode. Its default value when not
configured is hidden-suppressed
.
The options are:
-
visible-backspaced
: types sequence characters as they are inputted. The typed characters will be erased with backspaces for a valid sequence termination. -
hidden-suppressed
: hides sequence characters as they are typed. Does not output the hidden characters for an invalid sequence termination. -
hidden-delay-type
: hides sequence characters as they are typed. Outputs the hidden characters for an invalid sequence termination either after a timeout or after a non-sequence key is typed.
For visible-backspaced
and hidden-delay-type
, a sequence leader input will
be ignored if a sequence is already active. For historical reasons, and in case
it is desired behaviour, a sequence leader input using hidden-suppressed
will
reset the key sequence.
See Sequences for more about sequences.
Example:
(defcfg sequence-input-mode visible-backspaced )
By default, kanata will try to detect which input devices are keyboards and try
to intercept them all. However, you may specify exact keyboard devices from the
/dev/input
directories using the linux-dev
configuration.
Example:
(defcfg linux-dev /dev/input/by-path/platform-i8042-serio-0-event-kbd )
If you want to specify multiple keyboards, you can separate the paths with a
colon :
. Example:
(defcfg linux-dev /dev/input/dev1:/dev/input/dev2 )
Due to using the colon to separate devices, if you have a device with colons in its file name, you must escape those colons with backslashes:
(defcfg linux-dev /dev/input/path-to\:device )
By default, kanata will log layer changes. However, logging has some processing overhead. If you do not care for the logging, you can choose to disable it.
Example:
(defcfg log-layer-changes no )
By default, kanata will crash if no input devices are found. You can change
this behaviour by setting linux-continue-if-no-devs-found
.
Example:
(defcfg linux-continue-if-no-devs-found yes )
Unicode on Linux works by pressing Ctrl+Shift+U, typing the unicode hex value, then pressing Enter. However, if you do remapping in userspace, e.g. via xmodmap/xkb, the keycode "U" that kanata outputs may not become a keysym "u" after the userspace remapping. This will be likely if you use non-US, non-European keyboards on top of kanata. For unicode to work, kanata needs to use the keycode that outputs the keysym "u", which might not be the keycode "U".
You can use evtest
or kanata --debug
, set your userspace key remapping,
then press the key that outputs the keysym "u" to see which underlying keycode
is sent. Then you can use this configuration to change kanata’s behaviour.
Example:
(defcfg linux-unicode-u-code v )
Unicode on Linux terminates with the Enter key by default. This may not work in some applications. The termination is configurable with the following options:
-
enter
-
space
-
enter-space
-
space-enter
(defcfg linux-unicode-termination space )
There is an option for Windows to help mitigate the strange behaviour of AltGr (ralt) if you’re using that key in your defsrc. This is applicable for many non-US layouts. You can use one of the listed values to change what kanata does with the key:
-
cancel-lctl-release
-
This will remove the
lctl
press that is generated alonsideralt
-
-
add-lctl-release
-
This adds an
lctl
release whenralt
is released
-
Example:
(defcfg windows-altgr add-lctl-release )
For more context, see: #55.
Note
|
Even with these workarounds, putting lctl `+ralt` in your defsrc may not
work properly with other applications that also use keyboard interception.
Known application with issues: GWSL/VcXsrv
|
This defcfg item allows you to intercept mouse buttons for a specific mouse device. This only works with the Interception driver (the -wintercept variants of the binary).
The intended use case for this is for laptops such as a Thinkpad, which have mouse buttons that may be desirable to activate kanata actions with.
To know what numbers to put into the string, you can run the variant with this defcfg item defined with any numbers. Then when a button is first pressed on the mouse device, kanata will print its hwid in the log; you can then copy-paste that into this configuration entry. If this defcfg item is not defined, the log will not print.
Example:
(defcfg windows-interception-mouse-hwid "70, 0, 60, 0" )
The defcfg
entry is treated as a list with pairs of strings. For example:
(defcfg a 1 b 2)
This will be treated as configuration a
having value 1
and configuration
b
having value 2
.
An example defcfg containing all of the options is shown below. It should be noted options that are Linux-only or Windows-only will be ignored when used on a non-applicable operating system.
(defcfg process-unmapped-keys yes danger-enable-cmd yes sequence-timeout 2000 sequence-input-mode visible-backspaced log-layer-changes no linux-dev /dev/input/dev1:/dev/input/dev2 linux-continue-if-no-dev-found yes linux-unicode-u-code v linux-unicode-termination space windows-altgr add-lctl-release windows-interception-mouse-hwid "70, 0, 60, 0" )
Before learning about actions, it will be useful to first learn about aliases and variables.
Using the defalias
configuration entry, you can introduce a shortcut label
for an action.
Similar to how defcfg
works, defalias
reads pairs of items in a sequence
where the first item in the pair is the alias name and the second item is the
action it can be substituted for. However, unlike defcfg
, the second item
in defalias
may be a "list" as opposed to a single string like it was in
defcfg
.
A list is a sequence of strings separated by whitespace, surrounded by
parentheses. All of the configuration entries we’ve looked at so far are lists;
defalias
is where we’ll first see nested lists in this guide.
Example:
(defalias ;; tap for caps lock, hold for left control cap (tap-hold 200 200 caps lctl) )
This alias can be used in deflayer
as a substitute for the long action. The
alias name is prefixed with @
to signify that it’s an alias as opposed to a
normal key.
(deflayer example @cap a s d f )
You may have multiple defalias
entries and multiple aliases within a single
defalias
. Aliases may also refer to other aliases that were defined earlier
in the configuration file.
Example:
(defalias one (tap-hold 200 200 caps lctl)) (defalias two (tap-hold 200 200 esc lctl)) (defalias three C-A-del ;; Ctrl+Alt+Del four (tap-hold 200 200 @three ralt) )
You can choose to put actions without aliasing them right into deflayer
.
However, for long actions it is recommended not to do so to keep a nice visual
alignment. Visually aligning your deflayer
entries will hopefully make your
configuration file easier to read.
Example:
(deflayer example ;; this is equivalent to the previous deflayer example (tap-hold 200 200 caps lctl) a s d f )
Using the defvar
configuration entry,
you can introduce a shortcut label for an arbitrary string or list.
Unlike an alias, a variable does not need to be a valid standalone action.
In other words,
a variable can be used as components of actions.
The most common use case is to define common number strings
for actions such as tap-hold
, tap-dance
, and one-shot
.
Similar to how defalias
works,
defvar
reads pairs of items in a sequence
where the first item in the pair is the variable name
and the second item is a string or list.
Variables are allowed to refer to previously defined variables.
Variables can be used to substitute most values. Some notable exceptions are:
-
variables cannot be used in
defcfg
,defsrc
, ordeflocalkeys
-
variables cannot be used to substitute a layer name
-
variables cannot be used to substitute an action name
Variables are referred to by prefixing their name with $
.
Example:
(defvar tap-timeout 100 hold-timeout 200 tt $tap-timeout ht $hold-timeout ) (defalias th1 (tap-hold $tt $ht caps lctl) th2 (tap-hold $tt $ht spc lsft) )
The actions kanata provides are what make it truly customizable. This section explains the available actions.
You can put the lrld
action onto a key to live-reload your configuration
file. If kanata can’t parse the file, it will continue using the previous
configuration.
Example:
(deflayer has-live-reload lrld a s d f )
There are variants of lrld
: lrld-prev
and lrld-next
. These will cycle
through different configuration files that you specify on kanata’s startup.
The first configuration file specified will be the one loaded on startup.
The prev/next variants can be used with shortened names of lrpv
and lrnx
as
well.
Example:
(deflayer has-live-reloads lrld lrpv lrnx )
Example specifying multiple config files in the command line:
kanata -c startup.cfg -c 2nd.cfg -c 3rd.cfg
The action rpt
repeats the most recently typed key. Holding down this key
will not repeatedly send the key. The intended use case is to be able to use a
different finger or even thumb key to repeat a typed key, as opposed to
double-tapping a key.
Example:
(deflayer has-repeat rpt a s d f )
This action allows you to switch to another "base" layer. This is permanent
until a layer-switch
to another layer is activated. The concept of a base
layer makes more sense when looking at the next action: layer-while-held
.
This action accepts a single subsequent string which must be a layer name
defined in a deflayer
entry.
Example:
(defalias dvk (layer-switch dvorak))
This action allows you to temporarily change to another layer while the key remains held. When the key is released, you go back to the currently active "base" layer.
This action accepts a single subsequent string which must be a layer name
defined in a deflayer
entry.
Example:
(defalias nav (layer-while-held navigation))
You may also use layer-toggle
in place of layer-while-held
; they behave
exactly the same. The layer-toggle
name is slightly shorter but is a bit
inaccurate with regards to its meaning.
If you use a single underscore for a key _
then it acts as a "transparent"
key in a deflayer
. The behaviour depends if _
is on a base layer or a
while-held layer. When _
is pressed on the active base layer, the key will
default to the corresponding defsrc
key. If _
is pressed on the active
while-held layer, the base layer’s behaviour will activate.
Example:
(defsrc a b c ) (deflayer remap-only-c-to-d _ _ d )
You may use the action XX
as a "no operation" key, meaning pressing the key
will do nothing. This might be desirable in place of a transparent key on a
layer that is not fully mapped so that a key that is intentionally not mapped
will do nothing as opposed to typing a letter.
Example:
(deflayer contains-no-op XX a s d f )
The unicode
action accepts a single unicode character. The character will
not be repeatedly typed if you hold the key down.
You may use a unicode character as an alias if desired.
Note
|
The unicode action may not be correctly accepted by the active application. |
Note
|
If using Linux, make sure to look at the unicode behaviour customization in defcfg. |
(defalias sml (unicode 😀) 🙁 (unicode 🙁) ) (deflayer has-happy-sad @sml @🙁 a s d f )
You may want to remap a key to automatically be pressed in combination with modifiers such as Control or Shift. You can achieve this by prefixing the normal key name with one or more of:
-
C-
: Left Control -
A-
: Left Alt -
S-
: Left Shift -
M-
: Left Meta, a.k.a. Windows, GUI, Command, Super -
RA-
orAG
: Right Alt, a.k.a. AltGr
These modifiers may be combined together if desired.
Example:
(defalias ;; Ctrl+C: send SIGINT to a Linux terminal program int C-c ;; Win+Tab: open Windows' Task View tsk M-tab ;; Ctrl+Shift+(C|V): copy or paste from certain terminal programs cpy C-S-c pst C-S-v )
You can release a held key or layer via these actions:
-
release-key
: release a key, acceptsdefsrc
compatible names -
release-layer
: release a while-held layer
An example practical use case for release-key
is seen in the multi
section
directly below.
There is currently no known practical use case for
release-layer
, but it exists nonetheless.
The multi
action executes multiple keys or actions in order but also
simultaneously. It accepts one or more actions.
This action may result in unexpected or incorrect behaviour when creating a complicated combination of actions. If you find incorrect behaviour, please feel free to file an issue.
An example use case is to press the "Alt" key while also activating another layer.
In the example below, holding the physical "Alt" key will result in a held
layer being activated while also holding "Alt" itself. The held layer operates
nearly the same as the standard keyboard, so for example the sequence (hold
Alt)+(Tab+Tab+Tab) will work as expected. This is in contrast to having a layer
where tab
is mapped to A-tab
, which results in repeated press+release of
the two keys and has different behaviour than expected. Some special keys will
release the "Alt" key and do some other action that requires "Alt" to be
released. In other words, the "Alt" key serves a dual purpose of still
fulfilling the "Alt" key role for some button presses (e.g. Tab), but also as a
new layer for keys that aren’t typically used with "Alt" to have added useful
functionality.
(defalias atl (multi alt (layer-while-held alted-with-exceptions)) lft (multi (release-key alt) left) ;; release alt if held and also press left rgt (multi (release-key alt) rght) ;; release alt if held and also press rght ) (defsrc alt a s d f ) (deflayer base @atl _ _ _ _ ) (deflayer alted-with-exceptions _ _ _ @lft @rgt )
You can click the left, middle, and right buttons using kanata actions, do vertical/horizontal scrolling, and move the mouse.
The mouse button actions are:
-
mlft
: left mouse button -
mmid
: middle mouse button -
mrgt
: right mouse button -
mfwd
: forward mouse button -
mbck
: backward mouse button
The mouse button will be held while the key mapped to it is held.
If there are multiple mouse click actions within a single multi action, e.g.
(multi mrgt mlft)
then all the buttons except the last will be clicked then unclicked. The last button will remain held until key release. In the example above, pressing then releasing the key mapped to this action will result in the following event sequence:
-
press key mapped to
multi
-
click right mouse button
-
unclick right mouse button
-
click left mouse button
-
release key mapped to
multi
-
release left mouse button
There are variants of the standard mouse buttons which "tap" the button. Rather than holding the button while the key is held, a mouse click will be immediately followed by the release. Nothing happens when the key is released. The actions are as follows:
-
mltp
: tap left mouse button -
mmtp
: tap middle mouse button -
mrtp
: tap right mouse button -
mftp
: tap forward mouse button -
mbtp
: tap bacward mouse button
The mouse wheel actions are:
-
mwheel-up
: vertical scroll up -
mwheel-down
: vertical scroll down -
mwheel-left
: horizontal scroll left -
mwheel-right
: horizontal scroll right
All of these actions accept two number strings. The first is the interval (unit: ms) between scroll actions. The second number is the distance (unit: arbitrary). In both Windows and Linux, 120 distance units is equivalent to a notch movement on a physical wheel. You can play with the parameters to see what feels correct to you. Both numbers must be in the range [1,65535].
Note
|
In Linux, not all desktop
environments support the REL_WHEEL_HI_RES event, so kanata just doesn’t use
it. Instead, a scroll happens when 120 or more distance units are accumulated.
This may result in poor scrolling experience so in Linux it is recommended to
use a distance value that is a multiple of 120.
|
The mouse movement actions are:
-
movemouse-up
-
movemouse-down
-
movemouse-left
-
movemouse-right
Similar to the mouse wheel actions, all of these actions accept two number strings. The first is the interval (unit: ms) between movement actions and the second number is the distance (unit: pixels) of each movement.
The following are variants of the above mouse movements that apply linear mouse acceleration from the minimum distance to the maximum distance as the mapped key is held.
-
movemouse-accel-up
-
movemouse-accel-down
-
movemouse-accel-left
-
movemouse-accel-right
All these actions accept four number strings. The first number is the interval (unit: ms) between movement actions. The second number is the time it takes (unit: ms) to linearly ramp up from the minimum distance to the maximum distance. The third and fourth numbers are the minimum and maximum distances (unit: pixels) of each movement.
(defalias mwu (mwheel-up 50 120) mwd (mwheel-down 50 120) mwl (mwheel-left 50 120) mwr (mwheel-right 50 120) ms↑ (movemouse-up 1 1) ms← (movemouse-left 1 1) ms↓ (movemouse-down 1 1) ms→ (movemouse-right 1 1) ma↑ (movemouse-accel-up 1 1000 1 5) ma← (movemouse-accel-left 1 1000 1 5) ma↓ (movemouse-accel-down 1 1000 1 5) ma→ (movemouse-accel-right 1 1000 1 5) ) (deflayer mouse _ @mwu @mwd @mwl @mwr _ _ _ _ _ @ma↑ _ _ _ _ pgup bck _ fwd _ _ _ _ @ma← @ma↓ @ma→ _ _ _ pgdn mlft _ mrgt mmid _ mbck mfwd _ @ms↑ _ _ _ _ mltp _ mrtp mmtp _ mbtp mftp @ms← @ms↓ @ms→ _ _ _ _ _ _ _ )
The tap-dance
action allows repeated tapping of a key to result in
different actions. It is followed by a timeout (unit: ms) and a list
of keys or actions. Each time the key is pressed, its timeout will reset. The
action will be chosen if one of the following events occur:
-
the timeout expires
-
a different key is pressed
-
the key is repeated up to the final action
You may put normal keys or other actions in tap-dance
.
Example:
(defalias ;; 1 tap : "A" key ;; 2 taps: Control+C ;; 3 taps: Switch to another layer ;; 4 taps: Escape key td (tap-dance 200 (a C-c (layer-switch l2) esc)) )
There is a variant of tap-dance
with the name tap-dance-eager
. The variant
is parsed identically but the difference is that it will activate every
action in the sequence as the taps progress.
In the example below, repeated taps will, in order:
-
type
a
-
erase the
a
and typebb
-
erase the
bb
and typeccc
(defalias td2 (tap-dance-eager 500 ( (macro a) ;; use macro to prevent auto-repeat of the key (macro bspc b b) (macro bspc bspc c c c) )) )
The one-shot
action is similar to "sticky keys", if you know what that is.
This activates an action or key until either the timeout expires or a different
key is used. The one-shot
action must be followed by a timeout (unit:
ms) and another key or action.
Some of the intended use cases are:
-
press a modifier for exactly one following key press
-
switch to another layer for exactly one following key press
If a one-shot
key is held then it will act as the regular key. E.g. holding
a key assigned with @os1
in the example below will keep Left Shift held for
every key, not just one, as long as it’s still physically pressed.
Pressing multiple one-shot
keys in a row within the timeout will combine
the actions of those keys and reset the timeout to the value of the most
recently pressed one-shot
key.
There are four variants of the one-shot
action:
-
one-shot-press
: end on the first press of another key -
one-shot-release
: end on the first release of another key -
one-shot-press-pcancel
: end on the first press of another key or on re-press of another active one-shot key -
one-shot-release-pcancel
: end on the first release of another key or on re-press of another active one-shot key
It is important to note that the first activation of a one-shot key determines the behaviour with regards to the 4 variants for all subsequent one-shot key activations, even if a following one-shot key has a different configuration than the initial key pressed.
The default name one-shot
corresponds to one-shot-press
.
Example:
(defalias os1 (one-shot 500 (layer-while-held another-layer)) os2 (one-shot-press 2000 lsft) os3 (one-shot-release 2000 lctl) os4 (one-shot-press-pcancel 2000 lalt) os5 (one-shot-release-pcancel 2000 lmet) )
The tap-hold
action allows you to have one action/key for a "tap" and a
different action/key for a "hold". A tap is a rapid press then release of the
key whereas a hold is a long press.
The action takes 4 parameters in the listed order:
-
tap timeout (unit: ms)
-
hold timeout (unit: ms)
-
tap action
-
hold action
The tap timeout is the number of milliseconds within which a rapid press+release+press of a key will result in the tap action being held instead of the hold action activating.
The hold timeout is the number of milliseconds after which the hold action will activate.
There are two additional variants of tap-hold
:
-
tap-hold-press
-
If there is a press of a different key, the hold action is activated even if the hold timeout hasn’t expired yet
-
-
tap-hold-release
-
If there is a press+release of a different key, the hold action is activated even if the hold timeout hasn’t expired yet
-
These variants may be useful if you want more responsive tap-hold keys, but you should be wary of activating the hold action unintentionally.
Example:
(defalias anm (tap-hold 200 200 a @num) ;; tap: a hold: numbers layer oar (tap-hold-press 200 200 o @arr) ;; tap: o hold: arrows layer ech (tap-hold-release 200 200 e @chr) ;; tap: e hold: chords layer )
There are further additional variants of tap-hold-press
and tap-hold-release
:
-
tap-hold-press-timeout
-
tap-hold-release-timeout
These variants take a 5th parameter, in addition to the same 4 as the other
variants. The 5th parameter is another action, which will activate if the hold
timeout expires as opposed to being triggered by other key actions, whereas the
non -timeout
variants will activate the hold action in both cases.
-
tap-hold-release-keys
This variant takes a 5th parameter which is a list of keys that trigger an
early tap when they are pressed while the tap-hold-release-keys
action is
waiting.
Example:
(defalias ;; tap: o hold: arrows layer timeout: backspace oat (tap-hold-press-timeout 200 200 o @arr bspc) ;; tap: e hold: chords layer timeout: esc ect (tap-hold-release-timeout 200 200 e @chr esc) ;; tap: u hold: misc layer early tap if any of: (a o e) are pressed umk (tap-hold-release-keys 200 200 u @msc (a o e)) )
The macro
action will tap a sequence of keys with optional
delays. This is different from multi
because in the multi
action,
all keys are held, whereas in macro
, keys are pressed then released.
This means that with macro
you can have some letters capitalized and others
not. This is not possible with multi
.
The macro
action accepts one or more keys, some actions, chords, and delays
(unit: ms). It also accepts a list prefixed with output chord
modifiers where the list is subject to the aforementioned restrictions. The
number keys will be parsed as delays, so they must be aliased to be used in a macro.
Up to 4 macros can be active at the same time.
The actions supported in macro
are:
Example:
(defalias : S-; 8 8 0 0 🙃 (unicode 🙃) ;; Type "http://localhost:8080" lch (macro h t t p @: / / 100 l o c a l h o s t @: @8 @0 @8 @0) ;; Type "I am HAPPY my FrIeNd 🙃" hpy (macro S-i spc a m spc S-(h a p p y) spc m y S-f r S-i e S-n d spc @🙃) ;; alt-tab(x3) and alt-shift-tab(x3) with macro tfd (macro A-(tab 200 tab 200 tab)) tbk (macro A-S-(tab 200 tab 200 tab)) )
There is a variant of the macro
action that will cancel all active macros
upon releasing the key: macro-release-cancel
. It is parsed identically to
the non-cancelling version. An example use case for this action is holding down
a key to get different outputs, similar to tap-dance but one can see which keys
are being outputted.
E.g. in the example below, when holding the key, first 1
is typed, then
replaced by !
after 500ms, and finally that is replaced by @
after another
500ms. However, if the key is released, the last character typed will remain
and the rest of the macro does not run.
(defalias 1 1 ;; macro-release-cancel to output different characters with visual feedback ;; after holding for different amounts of time. 1!@ (macro-release-cancel @1 500 bspc S-1 500 bspc S-2) )
The dynamic-macro actions allow for recording and playing key presses. The dynamic macro records physical key presses, as opposed to kanata’s outputs. This allows the dynamic macro to replicate any action, but it means that if the macro starts and ends on different layers, then the macro might not be properly repeatable.
The action dynamic-macro-record
accepts one number (0-65535), which represents
the macro ID. Activating this action will begin recording physical key inputs.
If dynamic-macro-record
with the same ID is pressed again, the recording will
end and be saved. If dynamic-macro-record
with a different ID is pressed then
the current recording will end and be saved, then a new recording with the new
ID will begin.
The action dynamic-macro-record-stop
will stop and save any active recording.
The action dynamic-macro-play
accepts one number (0-65535), which represents
the macro ID. Activating this action will play the saved recording of physical
keys from a previous dynamic-macro-record
with the same macro ID, if it exists.
One can nest dynamic macros within each other, e.g. activate
(dynamic-macro-play 1)
while recording with (dynamic-macro-record 0)
.
However, dynamic macros cannot recurse; e.g. activating (dynamic-macro-play 0)
while recording with (dynamic-macro-record 0)
will be ignored.
Example:
(defalias dr0 (dynamic-macro-record 0) dr1 (dynamic-macro-record 1) dr2 (dynamic-macro-record 2) dp0 (dynamic-macro-play 0) dp1 (dynamic-macro-play 1) dp2 (dynamic-macro-play 2) dms dynamic-macro-record-stop )
The fork action accepts two actions and a key list. The first (left) action will activate by default. The second (right) action will activate if any of the keys in the third parameter (right-trigger-keys) are currently active.
Example:
(defalias frk (fork k @special (lalt ralt)) )
The cmd
action executes a program with arguments. It accepts one or more
strings. The first string is the program that will be run and the following
strings are arguments to that program. The arguments are provided to the
program in the order written in the config file.
Note
|
The command is executed directly and not via a shell, so you cannot make
use of environment variables, e.g. ~ or $HOME in Linux will not be
substituted with your home directory.
|
Example:
(defalias cm1 (cmd rm -fr /tmp/testing) ;; You can use bash -c and then a quoted string to execute arbitrary text in ;; bash. All text within double-quotes is treated as a single string. cm2 (cmd bash -c "echo hello world") )
There is a variant of cmd
: cmd-output-keys
. This variant reads the output
of the executed program and reads it as an S-expression, similarly to the
macro action. However — unlike macro — only keys, chords, and
chorded lists are supported. Delays and other actions are not supported.
(defalias ;; bash: type date-time as YYYY-MM-DD HH:MM pdb (cmd-output-keys bash -c "date +'%F %R' | sed 's/./& /g' | sed 's/:/S-;/g' | sed 's/\(.\{20\}\)\(.*\)/\(\1 spc \2\)/'") ;; powershell: type date-time as YYYY-MM-DD HH:MM pdp (cmd-output-keys powershell.exe "echo '(' (((Get-Date -Format 'yyyy-MM-dd HH:mm').toCharArray() -join ' ').insert(20, ' spc ') -replace ':','S-;') ')'") )
The arbitrary-code
action allows sending an arbitrary number to kanata’s
output mechanism. The press is sent when pressed, and the release sent when
released. This action can be useful for testing keys that are not yet named or
mapped in kanata. Please contribute findings with names and mappings, either in
a GitHub issue or as a pull request!
Warning
|
This is not cross platform! |
Warning
|
When using the Interception driver, this action is still sent over SendInput. |
(defalias ab1 (arbitrary-code 700) )
The defoverrides
optional configuration item allows you to create global
key overrides, irrespective of what actions are used to generate those keys.
It accepts pairs of lists:
-
the input key list that gets replaced
-
the output key list to replace the input keys with
Both input and output lists accept 0 or more modifier keys (e.g. lctl, rsft) and exactly 1 non-modifier key (e.g. 1, bspc).
Only zero or one defoverrides
is allowed in a configuration file.
Example:
;; Swap numbers and their symbols with respect to shift (defoverrides (1) (lsft 1) (2) (lsft 2) ;; repeat for all remaining numbers (lsft 1) (1) (lsft 2) (2) ;; repeat for all remaining numbers )
You can define up to 256 fake keys. These keys are not directly mapped to any physical key presses and can only be activated via these actions:
-
(on-press-fakekey <fake key name> <key action>)
: Activate a fake key action when pressing the key mapped to this action. -
(on-release-fakekey <fake key name> <key action>)
: Activate a fake key action when releasing the key mapped to this action.
A fake key can be defined in a deffakekeys
configuration entry. Configuring
this entry is similar to defalias
, but you cannot make use of aliases
inside of deffakekeys
to shorten an action. You can however refer to
previously defined fake keys.
The aforementioned <key action>
can be one of three values:
-
press
: Press the fake key. It will not be released until another action triggers a release or tap. -
release
: Release the fake key. If it’s not already pressed, this does nothing. -
tap
: Press and release the fake key. If it’s already pressed, this only releases it.
Example:
(deffakekeys ctl lctl sft lsft met lmet alt lalt ;; Press all modifiers pal (multi (on-press-fakekey ctl press) (on-press-fakekey sft press) (on-press-fakekey met press) (on-press-fakekey alt press) ) ;; Release all modifiers ral (multi (on-press-fakekey ctl release) (on-press-fakekey sft release) (on-press-fakekey met release) (on-press-fakekey alt release) ) ) (defalias psf (on-press-fakekey sft press) rsf (on-press-fakekey sft release) pal (on-press-fakekey pal tap) ral (on-press-fakekey ral tap) ) (deflayer use-fake-keys @psf @rsf @pal @ral a s d f )
If you find that an application isn’t registering keypresses correctly with
multi
because the sequence activates too quickly, you can try using fake
key actions alongside the delay actions below.
-
on-press-fakekey-delay
-
on-release-fakekey-delay
Do note that processing a fakekey-delay and even a sequence of delays will delay any other inputs from being processed until the fakekey-delays are all complete, so use with care.
Note
|
You will likely want to use macro instead of fake keys with delays now
that macro supports more actions.
|
(defalias stm (multi ;; Shift -> middle mouse with a delay (on-press-fakekey lsft press) (on-press-fakekey-delay 200) (on-press-fakekey mmid press) (on-release-fakekey mmid release) (on-release-fakekey-delay 200) (on-release-fakekey lsft release) ) )
For more context, you can read the issue that sparked the creation of fake keys.
Something notable about fake keys is that they don’t always interrupt the state
of an active tap-dance-eager
. If a macro
action is assigned to a fake
key, this won’t interrupt a tap dance. However, most other action types,
notably a "normal" key action like rsft
will still interrupt a tap dance.
The sldr
action makes kanata go into "sequence" mode. The action name is
short for "sequence leader". This comes from Vim which has the concept of a configurable
sequence leader key. When in sequence mode, keys are not typed but are saved
until one of the following happens:
-
A key is typed that does not match any sequence
-
sequence-timeout
milliseconds elapses since the most recent key press
Sequences are configured similarly to deffakekeys
. The first parameter of a
pair must be a defined fake key name. The second parameter is a list of keys
that will activate a fake key tap when typed in the defined order. More
precisely, the action triggered is:
(on-press-fakekey <fake key name> tap)
Example:
(defseq git-status (g s t)) (deffakekeys git-status (macro g i t spc s t a t u s)) (defalias rcl (tap-hold-release 200 200 sldr rctl))
For more context, you can read the design and motivation of sequences.
Not to be confused with output chords, chord
actions allow you to perform various actions based on which specific combination
of input keys are pressed together. Such an unordered combination of keys
is called a "chord". Each chord can perform a different action, allowing you
to bind up to 2^n - 1
different actions to just n
keys.
Input chords are configured similarly to defalias
with two extra parameters
at the beginning of each defchords
group: the name of the group and a
timeout value after which a chord triggers if it isn’t triggered by a key release
or press of a non-chord key before the timeout expires.
(defsrc a b c) (deflayer default @cha @chb @chc ) (defalias cha (chord example a) chb (chord example b) chc (chord example c) ) (defchords example 500 (a ) a ( b ) b (a c) C-v (a b c) @three )
The first item of each pair specifies the keys that make up a given chord.
The second item of each pair is the action to be executed when the given chord
is pressed and may be any regular or advanced action, including aliases. It
currently cannot however contain another chord
action.
Note that unlike with defseq
, these keys do not directly correspond to real
keys and are merely arbitrary labels that make sense within the context of the
chord.
They are mapped to real keys in layers by configuring the key in the layer to
map to a (chord name key)
action where name
is the name of the chords
group (above example
) and key
is one of these arbitrary labels.
It is perfectly valid to nest these chord
actions that enter "chording mode"
within other actions like tap-dance
and that will work as one would expect.
However, this only applies to the first key used to enter "chording mode".
Once "chording mode" is active, all other keys will be directly handled by
"chording mode" with no regard for wrapper actions; e.g. if a key is pressed
and it maps to a tap-hold with a chord as the hold action within, that chord
key will immediately activate instead of the key needing to be held for the
timeout period.
This is not currently configurable without modifying the source code, but if
you’re willing and/or capable, there is a tap-hold behaviour that is currently
not exposed. Using this behaviour, one can be very particular about when and how
tap vs. hold will activate by using extra information. The available
information that can be used is exactly which keys have been pressed or
released as well as the timing in milliseconds of those key presses.
The action tap-hold-release-keys
makes use of some of this capability, but
doesn’t make full use of the power of this functionality.
For more context, you can read the motivation for custom tap-hold behaviour.