kanata 1.11.0

Multi-layer keyboard customization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
= Kanata Configuration Guide
:last-update-label!:
ifndef::env-github[]
:toc: left
endif::[]
:stylesheet: config-stylesheet.css

This document describes how to create a kanata configuration file.
The kanata configuration file will determine your keyboard behaviour upon running kanata.

== How to read the guide

ifdef::env-github[]
See the triple bullet-lines at the upper right
to open or close a Table of Contents sidebar.

You may want to view the guide rendered to HTML.
https://jtroo.github.io/config.html[Link to guide].
endif::[]

The **Reference** sections are shorter
and are intended for reviewing how precisely to configure different sections.
The **Description** sections are longer
and contain more details such as advice, motivation, and examples.

The configuration guide you are reading
may have content not applicable to the version you are using.
See below for links to specific guide versions.

ifdef::env-github[]
* https://github.com/jtroo/kanata/blob/v1.10.0/docs/config.adoc[v1.10.0]
* https://github.com/jtroo/kanata/blob/v1.9.0/docs/config.adoc[v1.9.0]
* https://github.com/jtroo/kanata/blob/v1.8.1/docs/config.adoc[v1.8.1]
* https://github.com/jtroo/kanata/blob/v1.8.0/docs/config.adoc[v1.8.0]
* https://github.com/jtroo/kanata/blob/v1.7.0/docs/config.adoc[v1.7.0]
endif::[]
ifndef::env-github[]
* link:/config-1.10.0.html[v1.10.0]
* link:/config-1.9.0.html[v1.9.0]
* link:/config-1.8.1.html[v1.8.1]
* link:/config-1.8.0.html[v1.8.0]
* link:/config-1.7.0.html[v1.7.0]
endif::[]

== Preamble

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.

Useful terminology to learn early:
[cols="1,5"]
|===
| string
| A sequence of characters.
Optionally surrounded by quotes.
Examples: `backspace`, `"string with spaces and 1 number"`.

| list
| A sequence of strings or nested lists within round brackets.
List items are separated by any amount of whitespace characters,
or by round brackets.
Examples: `(lrld-num 1)`, `(tap-dance 200 (f1(unicode 😀)f2(unicode 🙂)))`.
|===

If you have any questions, confusions, suggestions, etc., feel free to
https://github.com/jtroo/kanata/discussions/new/choose[start a discussion]
or https://github.com/jtroo/kanata/issues/new/choose[file an issue].
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.

== Forcefully exit kanata [[force-exit]]

Though this isn't configuration-related,
it may be important for you to know that pressing and holding all of the
three following keys together at the same time will cause kanata to exit:

- Left Control
- Space
- Escape

This mechanism works on the key input **before** any remappings done by kanata.

[[comments]]
== Comments

You can add comments to your configuration file. Comments are prefixed with two
semicolons. E.g:

[source]
----
;; This is a comment in a kanata configuration file.
;; 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 `+|#+`:

[source]
----
#|
This is
a multi-line comment block
|#
----

[[required-configuration-entries]]
== Required configuration entries

[[defsrc]]
=== defsrc

**Reference**

Your configuration file must have exactly one `defsrc` list.
This defines the order of keys that the `+deflayer+` entries will operate on.

.Syntax:
[source]
----
(defsrc $key1 $key2 ... $keyN)
----

[cols="1,6"]
|===
| `$key`
| The name of a key. This can be a default key name or one defined in <<deflocalkeys>>.
When physically pressing this input key, the action defined
at the same order position on the active layer will activate.
|===

**Description**

The `defsrc` configuration entry defines which of your key inputs
will be processed by kanata and how the keys map to defined layers.
Keys excluded from `defsrc` will not be processed by Kanata
unless you have `process-unmapped-keys yes` in <<introduction-defcfg,defcfg>>.

Keys not processed by Kanata has implications on various actions.
For example:

- Pressing an excluded key will type a letter
while a prior `tap-hold` decision is still pending,
resulting in potentially incorrect results.
- Excluded keys do not trigger early activation
in actions such as `tap-hold-press` or `tap-dance`
- Excluded keys cannot be read by `fork` or `switch` logic.

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 primary source of all key names are the
`str_to_oscode` and `default_mappings` functions in
https://github.com/jtroo/kanata/blob/main/parser/src/keys/mod.rs[the source].
Please feel welcome 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:

.Example:
[source]
----
(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
)
----

Note that some keyboards have a Menu key instead of a right Meta key.
In this case you can use `menu` instead of `rmet`.

For non-US keyboards, see <<non-us-keyboards,the non-US keyboards section>>.

[[deflayer]]
=== deflayer

**Reference**

Your configuration file must have at least one `+deflayer+` entry. This defines
how each physical key mapped in `+defsrc+` behaves when kanata runs.

.Syntax:
[source]
----
(deflayer $layer-name $action1 $action2 ... $actionN)
----

[cols="1,5"]
|===
| `$layer-name`
| A string representing the layer name.
This name is used to reference this layer in other actions.

| `$action`
| The action that activates while this layer is active
when the corresponding `defsrc` input key is pressed.
|===

**Description**

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.

An example `defsrc` and `deflayer` that remaps QWERTY to the Dvorak layout
would be:

.Example:
[source]
----
(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
)
----

A <<windows-only-tray-icon,Windows tray menu build>> also allows specifying
layer icons in `+deflayer+` and `+deflayermap+` to show in the tray menu on layer activation,
see https://github.com/jtroo/kanata/blob/main/cfg_samples/tray-icon/tray-icon.kbd[example config]

==== deflayermap

**Reference**

An alternative method for defining a layer exists: `deflayermap`.
This method maps inputs to actions by defining input-output pairs,
ignoring `defsrc` entirely.

You will likely want to either enable <<process-unmapped-keys>>
or define most of your keyboard keys within <<defsrc>> when using `deflayermap`.
Otherwise many actions do not behave as intended.
See one of the links for more context.

.Syntax:
[source]
----
(deflayermap ($layer-name)
 $input1 $action1
 $input2 $action2
 ...
 $inputN $actionN)
----

[cols="1,5"]
|===
| `$layer-name`
| A string representing the layer name.
This name is used to reference this layer in other actions.

| `$input`
| The input key mapped to the corresponding output.

| `$action`
| The action that activates while this layer is active
when the corresponding input key is pressed.
|===

**Description**


The `deflayermap` variant has the advantage of terser configuration
when only a few keys on a layer need to be mapped.
When practicing a new configuration, the standard `deflayer` has an advantage
of looking more like a physical keyboard layout,
which may be helpful to some.

Within `deflayermap`, the very first item must be the layer name.
The layer name must be in parentheses unlike with `deflayer`.
After the layer name, the layer is configured via pairs of items:

* input key
* output action

An example complete configuration that maps Caps Lock to Escape is:

[source]
----
;; defsrc is still necessary
(defsrc)
(deflayermap (base-layer)
  caps esc)
----

The input key takes the same role as `defsrc` keys.
The output action takes the role that items in the normal `deflayer` have.

As special input names,
you can use one of `&#95;`, `&#95;&#95;`, or `&#95;&#95;&#95;` to map all
the keys that are not explicitly mapped in the layer,
e.g. in the example above, these affect keys other than `caps`.

[cols="1,6"]
|===
| `&#95;`
| Map all unmapped keys in this layer that are defined in `defsrc`.

| `&#95;&#95;`
| Map all unmapped keys in this layer that are not defined in `defsrc`.

| `&#95;&#95;&#95;`
| Map all unmapped keys in this layer.
|===

If a key is not mapped explicitly or through these wildcards, it will be implicitly mapped to a <<transparent-key,transparent key>>.

[[review-of-required-configuration-entries]]
=== Review of required configuration entries

If you're reading in order, you have now seen all of the required entries:

* `+defsrc+`
* `+deflayer+`

[[minimal-config]]
An example minimal configuration is:

[source]
----
(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.

NOTE: Please have a read through link:https://github.com/jtroo/kanata/blob/main/docs/platform-known-issues.adoc[the known platform issues]
because they may have implications on what you should include/exclude in `defsrc`.
The Windows LLHOOK I/O mechanism has the most issues by far.

[[key-names]]
== Key names for defsrc and deflayermap

The source of truth for all default key names are the functions
`str_to_oscode` and `add_default_str_osc_mappings`
in the link:https://github.com/jtroo/kanata/blob/main/parser/src/keys/mod.rs[keys/mod.rs file].

https://www.toptal.com/developers/keycode[This online tool]
will also work for most keys to tell you the key name.
It will be shown as the `event.code` field in the web page
after you press the key.

[[non-us-keyboards]]
== Non-US keyboards

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
on the physical keys.
The two sections below can help you understand how to remap all your keys.

=== Browser event.code

Ensure kanata and other key remapping programs are **not** running.
Then you can use https://www.toptal.com/developers/keycode[this online tool]
and press the key.
The `event.code` field tells you the key name to use in Kanata.
Alternatively, you can read through
https://www.w3.org/TR/uievents-code/[this reference].
Due to the lengthy key names,
you may want to use `deflayermap` if remapping using these key names.

IMPORTANT: On Windows, you should use either `kanata_winIOv2.exe`
or Interception when using key names according to the browser `event.code`.
The default `kanata.exe` does not do mappings according to the browser `event.code`
key names.

[[deflocalkeys]]
=== deflocalkeys

**Reference**

You can use `deflocalkeys` to define additional key names that can be
used in `defsrc`, `deflayer`, and anywhere else in the configuration.

.Syntax:
[source]
----
(deflocalkeys-$variant
  $key-name1 $key-number1
  $key-name2 $key-number2
  ...
  $key-nameN $key-numberN)
----

[cols="1,5"]
|===
| `$variant`
| One of: `win winiov2 wintercept linux macos`

| `$key-name`
| A key name of your choice that can be used in the rest of the configuration.

| `$key-number`
| A key number that varies based on the kanata variant you are using.
|===

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.exe` binary.

**Description**

The `deflocalkeys` configurations are not strictly necessary.
Their purpose is to help you match your physical keyboard's appearance
to your kanata configuration,
in the hopes it will be more readable and less confusing.
In the underlying hardware, all keyboard positions send the same scan codes
according to their position, regardless of what is printed on the key cap.
The scan code names are typically referred to by the corresponding US layout name.
It is the job of the operating system to translate the same scan code
to the correct outputs according to the configured locale and layout.

You can find configurations that others have made in
https://github.com/jtroo/kanata/blob/main/docs/locales.adoc[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!

There are five variants of deflocalkeys:

- `deflocalkeys-win`
- `deflocalkeys-winiov2`
- `deflocalkeys-wintercept`
- `deflocalkeys-linux`
- `deflocalkeys-macos`

.Example:
[source]
----
(deflocalkeys-win
  ì 187
)

(deflocalkeys-winiov2
  ì 187
)

(deflocalkeys-wintercept
  ì 187
)

(deflocalkeys-linux
  ì 13
)

(deflocalkeys-macos
  ì 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.

Running kanata with the `--debug` flag lets you read the correct number,
shown in parenthesis of `code` in the `KeyEvent` log lines.

It also possible to use native tools, as described below.

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
in the `code: <number>` section.
(https://github.com/jtroo/kanata/releases/tag/win-keycode-tester-v0.3.0[prebuilt binary])

In Windows uning `winIOv2`, the winIOv2 executable variant
will give the correct number in the `code: <number>` section.

In Windows using Interception, the interception version of the keyboard tester
will give the correct number i the `num: <number>` section.
Between the hook and interception versions, some
keys may agree but others may not; do be aware that they are **not** compatible!

However, Interception and winIOv2 should generally agree with each other.

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 https://github.com/jtroo/kanata/blob/main/docs/locales.adoc[this document]
is very welcome so that future users can have an easier time 🙂.

[[introduction-defcfg]]
== Introduction to defcfg

Your configuration file may include a single `defcfg` entry.
The `defcfg` can be empty or omitted.
There are options that change kanata's behaviour,
but this introduction will introduce
only the most prevalent entry: `process-unmapped-keys`.
All other options can be found later in the <<optional-defcfg-options>> section.

.Example of an empty defcfg:
[source]
----
(defcfg)
----

[[process-unmapped-keys]]
=== process-unmapped-keys

The `process-unmapped-keys` option in `defcfg` is probably the most
generally impactful option.
Enabling this configuration makes kanata process keys
that are not defined in `defsrc`.
This might be useful
if you are only mapping a few keys in defsrc
instead of most of the keys on your keyboard.

By default, keys excluded from `defsrc` will not work in various scenarios.
Some examples:

- The early hold for prior `+tap-hold-press+` actions will not
- Prior `+one-shot+` actions will not be released
- `fork` and `switch` logic will not see the key

This option is disabled by default.
The reason this is not enabled by default
is because some keys may not work correctly if they are intercepted.
A known issue being AltGr/ralt/Right Alt; see <<windows-only-windows-altgr>>.

.Example:
[source]
----
(defcfg process-unmapped-keys yes)
(defcfg process-unmapped-keys (all-except lctl ralt))
----

== Aliases and variables[[aliases-and-vars]]

Before learning about actions,
it will be useful to first learn about aliases and variables.

[[aliases]]
=== Aliases

**Reference**

Using the `defalias` configuration entry, you can introduce a shortcut label
for an action.

.Syntax:
[source]
----
(defalias
  $alias-name1 $action1
  $alias-name2 $action2
  ...
  $alias-nameN $actionN)
----

[cols="1,5"]
|===
| `$alias-name`
| The chosen shortcut label for the action.
This shortcut label can be used in the rest of
the configuration by prefixing it with the `@`
character.

| `$action`
| The ouput action used wherever the alias name is referenced.
|===

**Description**

The `defalias` entry 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.

A list is a sequence of strings
or nested lists 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:
[source]
----
(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.

[source]
----
(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:
[source]
----
(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:
[source]
----
(deflayer example
  ;; this is equivalent to the previous deflayer example
  (tap-hold 200 200 caps lctl) a s d f
)
----

[[variables]]
=== Variables

**Reference**

Using the `defvar` configuration entry,
you can introduce a shortcut label for an arbitrary string or list.

.Syntax:
[source]
----
(defvar
  $var-name1 $var-value1
  $var-name2 $var-value2
  ...
  $var-nameN $var-valueN)
----

[cols="1,5"]
|===
| `$var-name`
| The chosen shortcut label for the string or list.
This shortcut label can be used in the rest of the
configuration by prefixing it with `$`.

| `$var-value`
| An arbitrary string or list that will be substituted
wherever the variable is used.

|===

**Description**

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`, or `deflocalkeys`
- variables cannot be used to substitute an action name

Variables are referred to by prefixing their name with `$`.

.Example:
[source]
----
(defvar
  tap-repress-timeout   100
  hold-timeout  200
  tt $tap-repress-timeout
  ht $hold-timeout
)

(defalias
  th1 (tap-hold $tt $ht caps lctl)
  th2 (tap-hold $tt $ht spc  lsft)
)
----

[[concat-in-defvar]]
==== concat in defvar

Within the second item of `defvar`,
a list that begins with the special keyword `concat` will concatenate all
subsequent items in the list together into a single string value.
Without using `concat`, lists are saved as-is.

.Example:
[source]
----
(defvar
  rootpath "/home/myuser/mysubdir"
  ;; $otherpath will be the string: /home/myuser/mysubdir/helloworld
  otherpath (concat $rootpath "/helloworld")
)
----

[[actions]]
== Actions

The actions kanata provides are what make it truly customizable.
This section explains the available actions.

[[live-reload]]
=== Live reload

**Reference**

Live reload variants:

[cols="1,5"]
|===
| `lrld`
| String action that live-reloads the currently-used configuration file.

| `lrld-next`
| String action that live-reloads the configuration file specified
consecutively later in the command line order.
Cycles to the first-specified file
if currently using the last file specified.

| `lrld-prev`
| String action that live-reloads the configuration file specified
consecutively earlier in the command line order.
Cycles to the last-specified file
if currently using the first file specified.

| `(lrld-num $n)`
| List action that live-reloads the n'th file
as specified in the command line order.
The first file specified is `n=1`.
|===

Live reload does not read or apply changes to device-related configurations.
Examples of device-related configurations:
`linux-dev`, `macos-dev-names-include`, `linux-use-trackpoint-property`,
`windows-only-windows-interception-keyboard-hwids`.

**Description**

You can put the `+lrld+` action onto a key to live reload your configuration file.
If kanata can't parse the file,
the previous configuration will continue to be used.
When live reload is activated,
the active kanata layer will be the first `deflayer` defined in the configuration.

.Example:
[source]
----
(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.

Another variant is the list action `lrld-num`.
This reloads the configuration file specified by the number,
according to the order that the configuration file arguments
are passed into kanata's startup command.

.Example:
[source]
----
(deflayer has-live-reloads
  lrld lrpv lrnx (lrld-num 3)
)
----

Example specifying multiple config files in the command line:

[source]
----
kanata -c startup.cfg -c 2nd.cfg -c 3rd.cfg
----

Given the above startup command,
activating `(lrld-num 2)` would reload the `2nd.cfg` file.


[[layer-switch]]
=== layer-switch

**Reference**

A list action that changes the active base layer.

.Syntax:
[source]
----
(layer-switch $layer-name)
----

[cols="1,5"]
|===
| `$layer-name`
| Layer name to switch to.
|===


**Description**

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:
[source]
----
(defalias dvk (layer-switch dvorak))
----

[[layer-while-held]]
=== layer-while-held

**Reference**

A list action that changes the active layer while the key is held.

.Syntax:
[source]
----
(layer-while-held $layer-name)
----

[cols="1,5"]
|===
| `$layer-name`
| Layer name to activate while key is held.
|===

**Description**

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:
[source]
----
(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.

[[transparent-key]]
=== Transparent key

**Reference**

[cols="1,5"]
|===
| `+_+`
| String action that activates the action of the layer "underneath" the active one.
|===

**Description**

Kanata maintains a layer stack consisting in order of:

* a stack of temporary layers, where each `layer-while-held` adds one layer on top
* the base layer, manipulated by `layer-switch`
* if `delegate-to-first-layer` is enabled: the first layer defined by `deflayer` or `deflayermap`
* `defsrc`

A single underscore `+_+` acts as a "transparent" key in the current layer.
It will invoke the action assigned to the same key position on the next layer in the stack.

NOTE: If `delegate-to-first-layer` is enabled and the base layer is currently set to the first defined layer, a transparent key on the base layer will fall through directly to `defsrc`.

[[use-defsrc]]
=== use-defsrc

**Reference**

[cols="1,6"]
|===
| `use-defsrc`
| String action that outputs the corresponding `defsrc` input key.
|===

**Description**

A similar concept to transparent key is the `+use-defsrc+` action.
When activated, the underlying `defsrc` key will be the output action.

.Example:
[source]
----
(defsrc a b c d)
(defalias src use-defsrc)
(deflayer remap-only-c-to-d
  _ _ d @src)
----

[[no-op]]
=== No-op

**Reference**

[cols="1,6"]
|===
| `XX`
| String action that will output nothing.
|===

**Description**

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.

Alternatively you can use `+✗+` `+∅+` `+•+` to mean no-op.

.Example:
[source]
----
(deflayer contains-no-ops
  XX ✗ ∅ •)
----

[[unicode]]
=== Unicode

**Reference**

List action that outputs a single unicode codepoint.
The unicode codepoint will not be repeatedly typed if you hold the key down.

.Syntax:
[source]
----
(unicode $unicode-codepoint)
----

[cols="1,4"]
|===
| `$unicode-codepoint`
| One unicode codepoint.
Be warned that many emojis/glyphs/graphemes
are composed of multiple codepoints.
|===

**Description**

NOTE: The <<clipboard-actions>> may output unicode characters more consistently.

The `+unicode+` (or `+🔣+`) action accepts a single unicode character (but not
a composed character, so 🤲, but not 🤲🏿),
or a single unicode number prefixed with `U+`.
For example, both of these actions are the same:
- `(unicode 🚆)`
- `(unicode U+1F686)`

If you want to output a glyph that is composed of multiple codepoints,
you can use <<macro>> with multiple `unicode` actions.

You may use a unicode character as an alias if desired or in its simplified form `+🔣😀+`
(vs the usual `+(🔣 😀)+`).

NOTE: The unicode action may not be correctly accepted by the active
application.

NOTE: If using Linux, make sure to look at the
<<linux-only-linux-unicode-u-code,unicode behaviour customization>> in defcfg.
Furthermore, on Wayland the unicode mechanism may be broken entirely.
On Wayland you can install then use the `wtype` program,
using <<cmd, cmd>> to execute it.
For example: `(cmd wtype á)`

.Example:
[source]
----
(defalias
  sml (unicode 😀)
  😀 (🔣 😀)
  🙁 (unicode 🙁)
)
(deflayer has-happy-sad
  @sml @🙁 @😀 🔣😀 d f
)
----

If you want output parentheses `+( )+` via unicode you can quote them.

.Example with parentheses
[source]
----
(defalias
  lp (unicode "(")
  rp (unicode ")")
)
----

If you want to output double quotes via unicode
you need a special quoting syntax.

.Example use of double-quote within a string
[source]
----
(defalias
  dq (unicode r#"""#)
)
----

[[output-chordscombos]]
=== Output chords/combos

**Reference**

Prefixing a known key name with the following strings
will output the key alongside the specified modifier.
Multiple prefixes can be combined to add more modifiers
to the same key output.
Duplicate prefixes are not allowed.

[cols="1,6"]
|===
| `+C-+`
| Left Control

| `+RC-+`
| Right Control

| `+A-+`
| Left Alt

| `+RA-+`
| Right Alt, also known as AltGr

| `+AG-+`
| Also means Right Alt/AltGr

| `+S-+`
| Left Shift

| `+RS-+`
| Right Shift

| `+M-+`
| Left Meta

| `+RM-+`
| Right Meta
|===

A special behaviour of output chords is that if another key is pressed,
all of the chord keys will be released
before the newly pressed key action activates.
The modifier keys are often not desired for subsequent actions
and without this behaviour,
rapid typing can result in undesired modified key presses.
If you want keys to remain pressed, use <<multi>> instead.

**Description**

You may want to remap a key to automatically be pressed in combination with
modifiers such as Control or Shift.
Output chords are a way for you to achieve this.

Output chords are typically used do one-off actions such as:

- type a symbol, e.g. `S-1` to output `!` for the US layout.
- type an accented character,
e.g. `RA-a` to output `á` for the US international layout.
- do a special action like `C-c` to send `SIGTERM` in the terminal

It should be noted that output chords are not usable in all configuration items.
If you get an unknown key error where you expected an output chord to be usable,
you must split the output chord into its component keys.
For example, `+(unmod C-l)+` is an error;
instead you should use `+(unmod lctl l)+`.

The output chord prefix strings are:

* `+C-+`: Left Control (also `+‹⎈+` `+‹⌃+` or without the `+‹+` side indicator)
* `+RC-+`: Right Control (also `+⎈›+` `+⌃›+`)
* `+A-+`: Left Alt (also `+‹⎇+` `+‹⌥+` or without the `+‹+` side indicator))
* `+RA-+`: Right Alt, a.k.a. AltGr (also `+AG+` `+⎇›+` `+⌥›+`)
* `+S-+`: Left Shift (also `+‹⇧+` or without the `+‹+` side indicator))
* `+RS-+`: Right Shift (also `+⇧›+`)
* `+M-+`: Left Meta, a.k.a. Windows, GUI, Command, Super (also `+‹⌘+` `+‹❖+` `+‹◆+` or without the `+‹+` side indicator))
* `+RM-+`: Right Meta (also `+⌘›+` `+❖›+` `+◆›+`)

.Example:
[source]
----
(defalias
  ;; Type exclamation mark (US layout)
  ex! S-1
  ;; 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
)
----

[[repeat-key]]
=== Repeat key

**Reference**

[cols="1,5"]
|===
| `rpt`
| String action that outputs the single most-recently typed key.

| `rpt-any`
| String action that outputs the most-recently outputted action.
|===

**Description**

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:
[source]
----
(deflayer has-repeat
  rpt a s d f
)
----

The `rpt` action only repeats the last key output.
For example, it won't output a chord like `ctrl+c`
if the previous key pressed was `C-c`.
The `rpt` action will only output `c` in this case.

There is a variant `rpt-any`
which will repeat any previous action
and would output `ctrl+c` in the example case.

----
(deflayer has-repeat-any
  rpt-any a s d f
)
----

[[release-a-key-or-layer]]
=== Release a key or layer

**Reference**

[cols="1,2"]
|===
| `(release-key $key)`
| List action that releases the defined key from output actions.
Notably this does not act on key inputs.

| `(release-layer $layer-name)`
| List action that releases `layer-while-held` activations
for the given layer name.
|===

**Description**

You can release a held key or layer via these actions:

* `release-key` or `key↑`: release a key, accepts `defsrc` compatible names
* `release-layer` or `layer↑`: release a while-held layer

A lower-level detail of these actions is that they operate on output states
as opposed to virtually releasing an input key.
This does have some practical significance.
For example, if the action `(macro-repeat a 50)` were on the `a` key,
activating `(release-key a)` will not stop the repeating macro.

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.

[[multi]]
=== multi

**Reference**

Activate multiple actions in sequence.

.Syntax:
[source]
----
(multi $action1 $action2 ... $actionN)
----

[cols="1,3"]
|===
| `$action`
| An output action.
|===

**Description**

The `+multi+` action executes multiple keys or actions in order but also
simultaneously. It accepts one or more actions.

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.

[source]
----
(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
)
----

WARNING: This action can sometimes behave in surprising ways
with regards to simultaneity and order of actions.
For example, an action like `(multi sldr ')` will not behave as expected.
Due to implementation details, `sldr` will activate after the `'`
even though it is listed before.
This example could instead be written as `(macro sldr 10 ')`,
and that would work as intended.
It is recommended to avoid `multi` if it can be replaced
with a different action like `macro` or an output chord.

==== reverse-release-order

**Reference**

String item that can be used inside of `(multi ...)`
to reverse the release order of any keys that were pressed
as part of `multi`.

.Syntax:
[source]
----
(multi ... reverse-release-order)
----

**Description**

Within `multi` you can use include `reverse-release-order`
to do what the action states: reverse the typical release order from
if you have multiple keys in multi.

For example, pressing then releasing a key with the action:
`(multi a b c)` would press a b c in the stated order
and then release a b c in the stated order.
Changing it to `(multi a b c reverse-release-order)`
would press a b c in the stated order
and then release c b a in the stated order.

.Example:
[source]
----
(defalias
  S-a-reversed (multi lsft a reverse-release-order)
)
----

[[mouse-actions]]
=== Mouse actions

You can click the left, middle, and right buttons using kanata actions, do
vertical/horizontal scrolling, and move the mouse.

[[mouse-buttons]]
==== Mouse buttons

**Reference**

You can activate mouse actions with the string actions below.

[cols="1,5"]
|===
| `mlft`
| Hold left mouse button.

| `mmid`
| Hold middle mouse button.

| `mrgt`
| Hold right mouse button.

| `mfwd`
| Hold forward mouse button.

| `mbck`
| Hold backward mouse button.

| `mltp`
| Tap left mouse button.

| `mmtp`
| Tap middle mouse button.

| `mrtp`
| Tap right mouse button.

| `mftp`
| Tap forward mouse button.

| `mbtp`
| Tap backward mouse button.
|===

In Linux and Windows,
the hold actions can be used within `defsrc` and `deflayermap`
to remap mouse buttons like keyboard keys.

NOTE:
On Windows, the Kanata process must be restarted
for it to begin or to stop handling mouse events;
changing defsrc then live-reloading will not
begin handling mouse events
if defsrc previously did not have any mouse events in defsrc.

**Description**

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.
Only on Linux and Windows,
the above actions are also usable in `defsrc`
to enable remapping specified mouse actions in your layers,
like you would with keyboard keys.

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

[[mouse-wheel]]
==== Mouse wheel

**Reference**

The `mwheel-*` actions allow you to emulate a mouse wheel.
Holding the action will repeatedly scroll
according to the action configuration.

.Syntax:
[source]
----
(mwheel-$variant $interval $distance)
----

[cols="1,4"]
|===
| `$variant`
| One of `up down left right` representing the scroll direction to use.

| `$interval`
| Number of milliseconds between scroll actions.

| `$distance`
| Distance to travel per activation.
The number `120` represents a complete notch on
standard resolution mice and in some environments,
120 or a multiple of it should be what is used.
|===

You may use these key names within `defsrc`
to remap scroll events as if they were keys,
corresponding to up, down, left, right respectively:
`mwu`, `mwd`, `mwl`, `mwr`.

The remapping of mouse events is only effective on Linux and Windows.

NOTE:
On Windows, the Kanata process must be restarted
for it to begin or to stop handling mouse events;
changing defsrc then live-reloading will not
begin handling mouse events
if defsrc previously did not have any mouse events in defsrc.

**Description**

The mouse wheel actions are:

* `mwheel-up` or `🖱☸↑`: vertical scroll up
* `mwheel-down` or `🖱☸↓`: vertical scroll down
* `mwheel-left` or `🖱☸←`: horizontal scroll left
* `mwheel-right` or `🖱☸→`: 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 Linux and Windows, 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.
If this is the case for yours,
it will likely be a better experience to use a distance value that is a multiple of 120.

On Linux and Windows, you can also choose to read from a mouse device.
When doing so, using the `mwu`, `mwd`, `mwl`, `mwr` key names in `defsrc`
allow you to remap the mouse scroll up/down/left/right actions like you would
with keyboard keys.

NOTE: If you are using a high-resolution mouse in Linux,
only a full "notch" of the scroll wheel will activate the action.

NOTE: If you are using a high-resolution mouse with Interception,
you will probably get way more events than you intended.

[[mouse-wheel-inertial]]
===== Mouse wheel: inertial variants

**Reference**

The `mwheel-accel-*` actions allow you to emulate a mouse wheel,
with "inertial" properties,
meaning scroll speed accelerates while active and decelerates when not.
You can adjust factors of acceleration and deceleration.

.Syntax:
[source]
----
(mwheel-accel-$variant $initial-velocity $maximum-velocity $acceleration-multiplier $deceleration-multiplier)
----

[cols="2,3"]
|===
| `$variant`
| One of `up down left right` representing the scroll direction to use.

| `$initial-velocity`
| Starting scroll speed, in arbitrary units.
Suggested starting point: `3`.

| `$maximum-velocity`
| Starting scroll speed, in arbitrary units.
Suggested starting point: `1200`.

| `$acceleration-multiplier`
| Determines acceleration rate of scroll speed.
Suggested starting point: `1.15`.

| `deceleration-multiplier`
| Determines deceleration rate of scroll speed.
Suggested starting point: `0.93`.
Setting to 0 will make scroll stop abruptly.
|===

**Description**

An alternative scrolling action can be used via `mwheel-accel-*`.
This action has "inertial" behaviour which you may prefer.

The recommended starting point for the parameters are as below:

.Example configuration:
[source]
----
(defvar
  mw-initial-v 3
  mw-maximum-v 1200
  mw-accel 1.15
  mw-decel 0.93)
(defalias
  mwu (mwheel-accel-up   $mw-initial-v $mw-maximum-v $mw-accel $mw-decel)
  mwd (mwheel-accel-down $mw-initial-v $mw-maximum-v $mw-accel $mw-decel))
----

[[mouse-movement]]
==== Mouse movement

**Reference**

The `movemouse-*` actions allow you to move the mouse cursor.
Holding the action will repeatedly move the cursor
according to the configuration.

.Syntax:
[source]
----
(movemouse-$variant $interval $distance)
----

[cols="1,4"]
|===
| `$variant`
| One of `up down left right` representing the direction to move.

| `$interval`
| Number of milliseconds between move activations.

| `$distance`
| Distance to travel per activation in unit of pixels.
|===

There is a move mouse variant that increases distance per activation
at a constant rate until a maximum is reached.

.Syntax:
[source]
----
(movemouse-accel-$variant $interval $acceleration-time $min $max)
----

[cols="1,4"]
|===
| `$variant`
| One of `up down left right` representing the direction to move.

| `$interval`
| Number of milliseconds between move activations.

| `$acceleration-time`
| Number of milliseconds until max distance per activation is reached.

| `$min`
| Initial distance to travel per activation in unit of pixels.

| `$max`
| Maximum distance to travel per activation in unit of pixels.
|===

**Description**

The mouse movement actions are:

* `movemouse-up` or `🖱↑`
* `movemouse-down` or `🖱↓`
* `movemouse-left` or `🖱←`
* `movemouse-right` or `🖱→`

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` or `🖱accel↑`
* `movemouse-accel-down` or `🖱accel↓`
* `movemouse-accel-left` or `🖱accel←`
* `movemouse-accel-right` or `🖱accel→`

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.

There is a toggable defcfg option related to `movemouse-accel` - <<movemouse-inherit-accel-state>>. You might want to enable it, especially if you're coming from QMK.

[[set-mouse]]
==== Set absolute mouse position

The action `setmouse` or `set🖱` sets the absolute mouse position.

WARNING: This is not supported on Linux.
For an interesting keyboard-centric mouse solution in Linux,
try looking at
https://github.com/rvaiya/warpd[warpd].

This list action takes two parameters which are `x` and `y` positions
of the absolute movement.

The coordinate system is platform-specific:

* **macOS**: Pixel coordinates. `0,0` is the top-left of the main display,
  maximum values are your screen resolution (e.g., `1920,1080`).
* **Windows**: Normalized coordinates from `0,0` (top-left)
  to `65535,65535` (bottom-right). Multiple monitors are treated as one virtual desktop.

Experimentation will be needed to find the correct values for your setup.

[[mouse-speed]]
==== Modify the speed of mouse movements

The action `movemouse-speed` or `🖱speed` modifies the speed at which `movemouse` and
`movemouse-accel` function at runtime. It does this by expanding or shrinking
`min_distance` and `max_distance` while the action key is pressed.

This action accepts one number (unit: percentage) by which the
mouse movements will be accelerated.

WARNING: Due to the nature of pixels being whole numbers, some values such as
33 may not result in an exact third of the distance.

.Example:
[source]
----
(defalias
  fst (movemouse-speed 200)
  slw (movemouse-speed 50)
)
----

[[mouse-all-actions-example]]
==== Mouse all actions example

[source]
----
(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)

  sm (setmouse 32228 32228)

  fst (movemouse-speed 200)
)

(deflayer mouse
  _    @mwu @mwd @mwl @mwr _    _    _    _    _    @ma↑ _    _    _
  _    pgup bck  _    fwd  _    _    _    _    @ma← @ma↓ @ma→ _    _
  _    pgdn mlft _    mrgt mmid _    mbck mfwd _    @ms↑ _    _
  @fst _    mltp _    mrtp mmtp _    mbtp mftp @ms← @ms↓ @ms→
  _    _    _              _              _    _    _
)
----

[[tap-dance]]
=== tap-dance

**Reference**

The `tap-dance` action allows performing different actions
based on number of consecutive taps of the same key.

.Syntax:
[source]
----
(tap-dance $timeout $action-list)
----

[cols="1,4"]
|===
| `$timeout`
| Number of milliseconds after which the tap-dance ends.

| `$action-list`
| A list of actions that can be selected, ordered by number of taps.
|===

The `tap-dance-eager` variant will eagerly perform actions.
Use of `macro` and `bspc` can help to backtrack for the 2nd tap onwards.

.Syntax:
[source]
----
(tap-dance-eager $timeout $action-list)
----

**Description**

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:
[source]
----
(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:

1. type `a`
2. erase the `a` and type `bb`
3. erase the `bb` and type `ccc`

[source]
----
(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)
  ))
)
----

[[one-shot]]
=== one-shot

**Reference**

Activate keys or layers for a time without keeping the input key held,
for one subsequent key.
Activating other one-shot actions,
while one or more are already active,
will reset the timeout,
and overlap the one-shot actions.


.Syntax:
[source]
----
($one-shot-variant $timeout $action)
----

Values for `$variant`:
[cols="1,3"]
|===
| `one-shot-press`
| End on the first press of another key.
This is also the variant selected by the name `one-shot`.

| `+one-shot-release+`
| End on the first release of a newly pressed key.

| `+one-shot-press-pcancel+`
| End on the first press of another key
  or on re-press of this key, or of another active one-shot key

| `+one-shot-release-pcancel+`
| End on the first release of a newly pressed key
  or on re-press of this key, or of another active one-shot key.
|===

Other items:
[cols="1,3"]
|===
| `$timeout`
| Number of milliseconds after which
if not deactivated due to user input,
one-shot will deactivate on its own.

| `$action`
| Layer action, key, or output chord.
|===

**Description**

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 `+@os2+` 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+` or `+one-shot↓+`:
  end on the first press of another key
- `+one-shot-release+` or `+one-shot↑+`:
  end on the first release of another key
- `+one-shot-press-pcancel+` or `+one-shot↓⤫+`:
  end on the first press of another key
  or on re-press of another active one-shot key
- `+one-shot-release-pcancel+` or `+one-shot↑⤫+`:
  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+`.

NOTE: When using one-shot with keys that will trigger defoverrides,
you will likely want to adjust <<override-release-on-activation>> to yes in `defcfg`.

.Example:
[source]
----
(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)
)
----

[[one-shot-pause-processing]]
==== one-shot-pause-processing

**Reference**

Pause `one-shot` processing of new input keypresses for a time,
to allow actions that are not intended to consume `one-shot`
to take place.

.Syntax:
[source]
----
(one-shot-pause-processing $time)
----

[cols="1,5"]
|===
| `time`
| Number of milliseconds to ignore processing.
Something notable is that one virtual key press or releas
(tap is a separate press and subsequent release)
will take 1ms to process.
If using virtual keys this number must be larger
than the number of virtual key events that are taking place.
|===

**Description**

The `one-shot-pause-processing` list action allows you to pause the
key press processing of one-shot activations.
An example of when this is useful the following sequence:

- Activate a layer-while-held
- Activate a one-shot action on that layer
- Release the layer-while-held key, which has an `(on-release ...)` action
  associated with it.
- The on-release action is not intended to consume one-shot activations

In the scenario above, by default the on-release activation
would trigger deactivation of one-shot;
thus the pause processing action must be used to stop this from happening.

[[tap-hold]]
=== tap-hold

WARNING: The `tap-hold` action and all variants can behave unexpectedly on Linux
with respect to repeat of antecedent key presses.
The full context is in https://github.com/jtroo/kanata/discussions/422[discussion #422].
In brief, the workaround is to use `tap-hold` inside of <<multi,multi>>,
combined with another key action that behaves as a no-op like `f24`. +
Example: `(multi f24 (tap-hold ...))`.
If multiple `tap-hold` actions may be pressed subsequently,
all using the `f24` workaround,
you may need to release the `f24` within the same `multi`
to avoid repeats from one double-tapped `tap-hold` action
followed by another, different `tap-hold` action.
Example:
`(defvirtualkeys relf24 (release-key f24)) ... (multi f24 (tap-hold ...) (macro 5 (on-press tap-vkey relf24)))`

**Reference**

The `tap-hold` action lets you activate different actions
depending it a key is tapped or held.

.Syntax:
[source]
----
(tap-hold $tap-repress-timeout $hold-timeout $tap-action $hold-action)
----

[cols="1,4"]
|===
| `$tap-repress-timeout`
| Number of milliseconds for the window that a tap into re-press with hold
results in the `$tap-action` being held.

| `$hold-timeout`
| Number of milliseconds after which the `$hold-action` activates.
Releasing the key before this elapses
results in `$tap-action` activating.

| `$tap-action`
| Action to activate when the input is determined to be a "tap".

| `$hold-action`
| Action to activate when the input is determined to be a "hold".
|===

.Variants:
----
(tap-hold-press $tap-repress-timeout $hold-timeout $tap-action $hold-action)
(tap-hold-release $tap-repress-timeout $hold-timeout $tap-action $hold-action)
(tap-hold-press-timeout $tap-repress-timeout $hold-timeout $tap-action $hold-action $timeout-action)
(tap-hold-release-timeout $tap-repress-timeout $hold-timeout $tap-action $hold-action $timeout-action [?reset-timeout-on-press])
(tap-hold-release-keys $tap-repress-timeout $hold-timeout $tap-action $hold-action $tap-keys)
(tap-hold-release-tap-keys-release $tap-repress-timeout $hold-timeout $tap-action $hold-action $tap-trigger-keys-on-press $tap-trigger-keys-on-press-then-release)
(tap-hold-except-keys $tap-repress-timeout $hold-timeout $tap-action $hold-action $tap-keys)
(tap-hold-tap-keys $tap-repress-timeout $hold-timeout $tap-action $hold-action $tap-keys)
----

[cols="1,2"]
|===
| `tap-hold-press`
| Activate `$hold-action` early if held and another input key is pressed.

| `tap-hold-release`
| Activate `$hold-action` early if held and another input key is pressed and released.

| `tap-hold-press-timeout`
| Activate `$hold-action` if held and another input key is pressed.
If the defined timeout elapses, `$timeout-action` will activate.

| `tap-hold-release-timeout`
| Activate `$hold-action` early if held and another input key is pressed and released.
If the defined timeout elapses, `$timeout-action` will activate.
Optionally include `reset-timeout-on-press` to reset timeout duration on a new press,
giving more time for a subsequent release to activate hold instead of timeout.

| `tap-hold-release-keys`
| Activate `$hold-action` early if held and another input key is pressed and released.
The `$tap-keys` parameter is a list of key names.
Activates `$tap-action` early if a key within `$tap-keys` is pressed before hold activates.

| `tap-hold-release-tap-keys-release`
| Activate `$hold-action` early if held and another input key is pressed and released.
The `$tap-keys-...` parameters are lists of key names.
Activate `$tap-action` early
if a key within `$tap-trigger-keys-on-press`
is pressed before hold activates
Activate `$tap-action` early
if a key within `$tap-trigger-keys-on-press-then-release`
is pressed then released before hold activates.

| `tap-hold-except-keys`
| The `$tap-keys` parameter is a list of key names.
Activates $tap-action if a key within $tap-keys is pressed
or if the action key is released before hold timeout.
No key is ever output until the action key is released
or another key is pressed,
which differs from the default `tap-hold` behaviour.

| `tap-hold-tap-keys`
| The `$tap-keys` parameter is a list of key names.
Activates `$tap-action` early if a key within `$tap-keys` is pressed before hold activates.
Unlike `tap-hold-release-keys`, does NOT activate `$hold-action` early
when other keys are pressed and released.
Waits for full `$hold-timeout` before activating `$hold-action`.
This is useful for home row mods where fast typing should not trigger modifiers.
|===
**Description**

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.

NOTE: for more discussion on tap-hold,
you may want to have a look at this GitHub discussion:
https://github.com/jtroo/kanata/discussions/1455[link to discussion].

The action takes 4 parameters in the listed order:

. tap repress timeout (unit: ms)
. hold timeout (unit: ms)
. tap action
. hold action

The tap repress 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.

.Tap repress timeout in more detail
[%collapsible,indent=4]
====
The way a `tap-hold` action works with respect to the tap repress timeout
is often unclear to newcomers.
To make it concrete, the output event sequence of the `tap-hold` action
`(tap-hold $tap-repress-timeout 200 a lctl)`
for varying values of `$tap-repress-timeout`
with a fixed input event sequence will be described.

The input event sequence is:

- press
- 50 ms elapses
- release
- 50 ms elapses
- press
- 300 ms elapses
- release

With `(defvar $tap-repress-timeout 0)`, the output event sequence is:

- 50 ms elapses
- press `a`
- release `a`
- 250 ms elapses
- press `lctl`
- 100 ms elapses
- release `lctl`

The above output sequence is the same for all `$tap-repress-timeout` values
between and including `0` and `99`.

For a value of `100` or greater for `$tap-repress-timeout`,
the output event sequence is instead:

- 50 ms elapses
- press `a`
- release `a`
- 50 ms elapses
- press `a`
- 300 ms elapses
- release `a`
====

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+` or `+tap⬓↓+`
** 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+` or `+tap⬓↑+`
** 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:
[source]
----
(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` or `tap⬓↓timeout`
- `tap-hold-release-timeout` or `tap⬓↑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.

The `release` variant also accepts an optional 6th argument,
`reset-timeout-on-press` which if included changes the timeout behaviour.
This flag will make the timeout duration reset if a new key is pressed.
This may result in longer timeouts,
but can help with more consistent hold activations;
because it may be challenging to release a key in time
to activate the hold action instead of the timeout action.

- `tap-hold-release-keys` or `tap⬓↑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.
Otherwise this behaves as `tap-hold-release`.

The keys in the 5th parameter correspond to the physical input keys,
or in other words the key that corresponds to `defsrc`.
This is in contrast to the `fork` and `switch` actions
which operates on outputted keys, or in other words the outputs
that are in `deflayer`, `defalias`, etc. for the corresponding `defsrc` key.

.Example:
[source]
----
(defalias
  ;; 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))
)
----

- `tap-hold-release-tap-keys-release`

This variant behaves nearly the same as `tap-hold-release-keys`,
but has another condition with respect to the eager tap.
It accepts a second list that activates the eager tap
if the any key listed within is pressed then released;
as opposed to only on a press.

.Example:
[source]
----
(defalias
  ;; tap: u    hold: misc layer      early tap if any of:
  ;;                                 (z x c v) are pressed, OR
  ;;                                 (a s d f) are pressed THEN released
  umk2 (tap-hold-release-tap-keys-release 200 200 u @msc (z x c v) (a s d f))
)
----

- `tap-hold-except-keys` or `tap-hold⤫keys`

This variant takes a 5th parameter which is a list of keys
that always trigger a tap
when they are pressed while the `tap-hold-except-keys` action is waiting.
No key is ever output until there is either a release of the key or any other
key is pressed. This differs from `tap-hold` behaviour.

The keys in the 5th parameter correspond to the physical input keys,
or in other words the key that corresponds to `defsrc`.
This is in contrast to the `fork` and `switch` actions
which operates on outputted keys, or in other words the outputs
that are in `deflayer`, `defalias`, etc. for the corresponding `defsrc` key.

.Example:
[source]
----
(defalias
  ;; tap: u    hold: misc layer      always tap if any of: (a o e) are pressed
  umk (tap-hold-except-keys 200 200 u @msc (a o e))
)
----

- `tap-hold-tap-keys` or `tap⬓tapkeys`

This variant takes a 5th parameter which is a list of keys
that trigger an early tap when they are pressed.
Unlike `tap-hold-release-keys`, pressing and releasing other keys
does NOT activate hold early - the full hold timeout is always waited.
This is useful for home row mods where fast typing should not trigger modifiers.

The keys in the 5th parameter correspond to the physical input keys,
or in other words the key that corresponds to `defsrc`.

.Example:
[source]
----
(defalias
  ;; tap: a    hold: lsft    early tap if any of: (s d f) are pressed
  ;; other keys do NOT trigger early hold
  ath (tap-hold-tap-keys 200 200 a lsft (s d f))
)
----

[[macro]]
=== macro

**Reference**

The macro action taps the configured sequence of keys or actions.
Numbers can be used to delay the sequence by the defined number of milliseconds.

.Syntax:
[source]
----
(macro $macro-action1 $macro-action2 ... $macro-actionN)
----

[cols="1,4"]
|===
| `$macro-action`
| A delay, key, action within the subset allowed within macros,
or an output-chord-prefixed list of more macro-actions.
|===

.Variants:
----
(macro-release-cancel ...)
(macro-cancel-on-press ...)
(macro-release-cancel-and-cancel-on-press ...)
(macro-repeat ...)
(macro-repeat-$cancel-variant ...)
----
[cols="1,2"]
|===
| `macro-release-cancel`
| Cancel all active macros if the key is released.

| `macro-cancel-on-press`
| Cancel all active macros if a different key is pressed.

| `macro-release-cancel-and-cancel-on-press`
| Cancel all active macros if either the key is released or a different key is pressed.

| `macro-repeat`
| Repeat the macro while held.

| `macro-repeat-$cancel-variant`
| Repeat the macro while held.
Cancels the final repeat according the behaviour of one of the variants:
`release-cancel`, `cancel-on-press`, `release-cancel-and-cancel-on-press`.
|===

**Description**

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-chordscombos,output chord>>
modifiers where the list is subject to the aforementioned restrictions.

IMPORTANT: The number keys `0-9` will be parsed as millisecond delays
whereas in other contexts they would be parsed as key names.
To use the numbered keys they must be aliased
or otherwise use the key names `Digit0-Digit9`.

Up to 4 macros can be active at the same time.

The actions supported in `+macro+` are:

* <<cmd, cmd>>
* <<unicode, unicode>>
* <<mouse-actions,mouse actions>>
* <<repeat-key,repeat>>
* <<live-reload,live reload>>
* <<virtual-keys,virtual key actions>>
* <<sequences,sequence leader>>
* <<arbitrary-code,arbitrary keycode>>
* <<dynamic-macro,dynamic macro>>
* <<unmod,unmod>>

NOTE: Some of these actions may need short delays between.
For example, `(macro a (unmod b) 5 (unmod c) d))`
needs the delay of `5` to work correctly.

.Example:
[source]
----
(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))
)
----

[[macro-release-cancel]]
==== macro-release-cancel

The `macro-release-cancel` variant of the `+macro+` action
will cancel all active macros
upon releasing the key.
Shorter unicode variant: `+macro↑⤫+`.
This variant 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.

[source]
----
(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)
)
----

[[macro-cancel-on-press]]
==== macro-cancel-on-press

The `macro-cancel-on-press` variant of the `macro action`
enables a cancellation trigger for all active macros including itself,
which is activated when a physical press of any other key happens.
The trigger is enabled while the macro is in progress.

[source]
----
(defalias
  1 1
  1!@ (macro-cancel-on-press @1 500 bspc S-1 500 bspc S-2)
)
----

[[macro-release-cancel-and-cancel-on-press]]
==== macro-release-cancel-and-cancel-on-press

The `macro-release-cancel-and-cancel-on-press` variant
combines the cancel behaviours
of both the release-cancel and cancel-on-press.

[source]
----
(defalias
  1 1
  1!@ (macro-release-cancel-and-cancel-on-press @1 500 bspc S-1 500 bspc S-2)
)
----


[[macro-repeat]]
==== macro-repeat

There are further `macro-repeat` variants of the three `macro` actions described previously.
These variants repeat while held.
The repeat will only occur once all macros have completed,
including the held macro key.
If multiple repeating macros are being held simulaneously,
only the most recently pressed macro will be repeated.

[source]
----
(defalias
  mr1 (macro-repeat mltp)
  mr2 (macro-repeat-release-cancel mltp)
  mr3 (macro-repeat-cancel-on-press mltp)
  mr4 (macro-repeat-release-cancel-and-cancel-on-press mltp)
)
----

[[dynamic-macro]]
=== dynamic-macro

**Reference**

Record and replay key inputs.

.Syntax:
[source]
----
(dynamic-macro-record $id)
(dynamic-macro-play   $id)
(dynamic-macro-record-stop)
(dynamic-macro-record-stop-truncate $count)
----

[cols="1,3"]
|===
| `dynamic-macro-record`
| Record a dynamicro macro which will be saved with the defined `$id`.

| `dynamic-macro-play`
| Play back a macro saved with the defined `$id`.

| `dynamic-macro-record-stop`
| Stop and save a macro recording.
This can also be achieved by recording a new macro
or re-pressing record with the same `$id`.

| `dynamic-macro-record-stop-truncate`
| Stop and save a macro recording while truncating `$count` events
from the end of the recording.
This can be useful if the record/stop button is on a different layer.
|===

**Description**

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.
There is a variant of this:
`dynamic-macro-record-stop-truncate`
This is a list action that takes a single parameter:
the number of key actions to remove at the end of a dynamic macro.
This variant is useful if the macro stop button is on a different layer.

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:
[source]
----
(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
  dst (dynamic-macro-record-stop-truncate 1)
)
----

[[caps-word]]
=== caps-word

**Reference**

The `caps-word` action puts Kanata into a state where
typed keys are automatically shifted by `lsft`.
The state persists until terminated by timeout
or by typing a key that ends the state.
Typing a non-terminating key refreshes the timeout duration.

The `-toggle` variants will end the caps-word state
if pressed while caps-word is active,
whereas the re-pressing the standard variants
will keep the state active and refresh the timeout duration.

.Syntax:
[source]
----
(caps-word $timeout)
(caps-word-toggle $timeout)
(caps-word-custom $timeout $shifted-list $non-terminal-list)
(caps-word-custom-toggle $timeout $shifted-list $non-terminal-list)
----

[cols="1,4"]
|===
| `$timeout`
| Number of milliseconds after which the caps-word state ends.
The duration is refreshed upon typing a non-terminating character.

| `$shifted-list`
| List of keys that will be automatically shifted.

| `$non-terminal-list`
| List of keys that are not shifted
but which do not terminate the caps-word state.
|===

**Description**

The `caps-word` or `word⇪` action triggers a state where the `lsft` key
will be added to the active key list
when a set of specific keys are active.
The keys are: `a-z` and `-`, which will be outputted as `A-Z` and `_`
respectively when using the US layout.

Examples where this is helpful
is capitalizing a single important word
like in `IMPORTANT!`
or defining a constant in code
like `const P99_99_VALUE: ...`.
This has an advantage over the regular caps lock
because it automatically ends
so it doesn't need to be toggled off manually,
and it also shifts `-` to `_`
which caps lock does not do.

The `caps-word` state ends when the keyboard is idle
for the duration of the defined timeout (1st parameter),
or a terminating key is pressed.
Every key is a terminating key
except the keys which get capitalized
and the extra keys in this list:

- `0-9`
- `kp0-kp9`
- `bspc del`
- `up down left rght`

You can use `caps-word-custom` or `word⇪-custom` instead of `caps-word`
if you want to manually define which keys are capitalized (2nd parameter)
and what the extra non-terminal+non-capitalized keys should be (3rd parameter).

.Example:
[source]
----
(defalias
  cw (caps-word 2000)

  ;; This example is similar to the default caps-word behaviour but it moves the
  ;; 0-9 keys to the capitalized key list from the extra non-terminating key list.
  cwc (caps-word-custom
    2000
    (a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
    (kp0 kp1 kp2 kp3 kp4 kp5 kp6 kp7 kp8 kp9 bspc del up down left rght)
  )
)
----

==== caps-word-toggle[[caps-word-toggle]]

There are `-toggle` variants of the `caps-word` actions.
By default re-pressing `caps-word` will keep `caps-word` active.
The `-toggle` variants will end `caps-word` if it is currently active,
otherwise `caps-word` will be activate as normal.

.Example:
[source]
----
(defalias
  cwt (caps-word-toggle 2000)
  cct (caps-word-custom-toggle
    2000
    (a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
    (kp0 kp1 kp2 kp3 kp4 kp5 kp6 kp7 kp8 kp9 bspc del up down left rght)
  )
)
----

=== unmod[[unmod]]

**Reference**

The `unmod` action will deactivate modifier keys while outputting
one or more defined keys.

.Syntax:
[source]
----
(unmod $key1 $key2 ... $keyN)
(unmod ($mod1 $mod2 ... $modN) $key1 $key2 ... $keyN)
----

[cols="1,5"]
|===
| `$key`
| A key name to output while unmodded.

| `$mod`
| By default `unmod` will deactivate all modifier keys.
An optional list as the first parameter
allows specfying a subset of modifiers to deactivate during the action.
|===

**Description**

The `unmod` action will release all modifiers temporarily
and send one or more keys.
After the `unmod` key is released, the released modifiers are pressed again.
The affected modifiers are: `lsft,rsft,lctl,rctl,lmet,rmet,lalt,ralt`.

A variant of `unmod` is `unshift` or `un⇧`.
This action only releases the `lsft,rsft` keys.
This can be useful for forcing unshifted keys while AltGr is still held.

NOTE: In case the modifiers to be undone are not part of `defsrc`,
<<process-unmapped-keys>> needs to be enabled in `defcfg` in order for their
states to be tracked correctly.

.Example:
[source]
----
(defalias
  ;; holding shift and tapping a @um1 key will still output 1.
  um1 (unmod 1)
  ;; dead keys é (as opposed to using AltGr) that outputs É when shifted
  dké (macro (unmod ') e)

  ;; In ISO German QWERTZ, force unshifted symbols even if shift is held
  { (unshift ralt 7)
  [ (unshift ralt 8)
)
----

A list may optionally be used as the first parameter of `unmod`.
The list must be non-empty and must contain only modifier keys,
which are the keys in the affected modifiers list from earlier in this document section.

When this list exists, the action will temporarily release only the keys listed
rather than all modifiers.

.Example:
[source]
----
(defalias
	;; only unshift the alt keys
	unalt-a (unmod (lalt ralt) a)
)
----

[[fork]]
=== fork

**Reference**

The `fork` action allows choosing between a default and an alternate action
based on whether specific keys are active. `fork` is the equivalent of the
basic key checks in `switch`, using none of the list logic items, i.e. virtual
keys are not supported.

.Syntax:
[source]
----
(fork $left-action $right-action $right-trigger-keys)
----

[cols="1,3"]
|===
| `$left-action`
| Action to activate by default.

| `$right-action`
| Action to activate if any of `$right-trigger-keys` are active.

| `$right-trigger-keys`
| List of keys that, if active when fork activates,
causes `$right-action` to happen in place of `$left-action`.
|===

TIP: The keys `nop0-nop9` can be used as no-op outputs that
can still be checked within `fork`, unlike what `XX` does.

[[switch]]
=== switch

**Reference**

The `switch` action allows conditionally activating 0 or more actions,
depending on conditional checks.

.Syntax:
[source]
----
(switch
  $logic-check1 $action1 $post-activate1
  $logic-check2 $action2 $post-activate2
  ...
  $logic-checkN $actionN $post-activateN)
----

[cols="1,4"]
|===
| `$logic-check`
| The condition, which if it evaluates to true,
will trigger the corresponding action.

| `$action`
| Action to activate when logic evaluates to true.

| `$post-activate`
| Valid values are `fallthrough` and `break`.
With `fallthrough`, when an action activates
switch will continue evaluating further logic checks
and potentially trigger more actions.
With `break`, further actions will not activate.
|===

The logic check is a list.
The items within the list can either be key names or a special list check.
A key name item evaluates to true if that key name
is a currently active key output of Kanata upon activating `switch`.
The outer-most list evaluates to true
if any of the items evaluates to true.

.Syntax of logic check:
[source]
----
($item1 $item2 ... $itemN)
----

.Syntax of special checks:
[source]
----
(or  $item1 $item2 ... $itemN)
(and $item1 $item2 ... $itemN)
(not $item1 $item2 ... $itemN)
(key-history $key-name $key-recency)
(key-timing  $key-recency $comparator $time)
(input         $input-type $key-name)
(input-history $input-type $key-name $input-recency)
(layer      $layer-name)
(base-layer $layer-name)
----

[cols="1,4"]
|===
| `or`
| Evaluates to true if any `$item` is true.

| `and`
| Evaluates to true if all of `$item` are true.

| `not`
| Evaluates to true if all of `$item` are false.

| `key-history`
| Evaluates to true if the key in the recency slot matches `$key-name`.
A `$key-recency` of 1 is the most recent key pressed according to Kanata processing.
The max recency is 8.

| `key-timing`
| The valid values for `$comparator` are `less-than` and `greater-than`,
with `lt` and `gt` as shorthand if desired.
This item evaluates to true if the key with the corresponding recency
was pressed — for `lt` more recently than, or for `gt` later than —
the defined `$time` with unit milliseconds.

| `input`
| Evaluates to true if the `$key-name` is currently pressed.
The `$input-type` must be either `real` or `virtual`.
If using `real`, this will check against the defsrc inputs.
If using `virtual`, this will check against virtual key activations.

| `input-history`
| Evaluates to true if the input in the `$input-recency` slot matches `$key-name`.
Two input types use the same history with respect to recency slots.
A recency of 1 is the most recent input i.e. the input activating `switch` itself.
The max recency is 8.

| `layer`
| Evaluates to true if the active layer matches `$layer-name`.

| `base-layer`
| Evaluates to true if the most-recently-switched-to layer
from a `layer-switch` action matches `$layer-name`.
|===

**Description**

Conceptually, the `switch` action is similar to <<fork>>
but has more capabilities as well as more complexity.
The `switch` action accepts multiple cases.
One case is a triple of:

- logic check
- action: to activate if logic check evaluates to true
- `fallthrough|break`: choose to continue vs. stop evaluating cases

The default use of the logic check behaves similarly to fork.

For example, the logic check `(a b c)` will activate the corresponding action
if any of a, b, or c are currently pressed.

TIP: the keys `nop0-nop9` can be used as no-op outputs that
can still be checked within `switch`, unlike what `XX` does.

The logic check also accepts the boolean operators `and|or|not` to allow more
complex use cases.

The order of cases matters.
For example, if two different cases match the currently pressed keys,
the case listed earlier in the configuration will activate first.
If the early case uses break, the second case will not activate.
Otherwise if fallthrough is used,
the second case will activate sequentially after the first case.
This idea generalizes to more than two cases,
but the two case example is hopefully simple and effective enough.

.Example:
[source]
----
(defalias
  swt (switch
    ;; case 1
    ((and a b (or c d) (or e f))) @ac1 break
    ;; case 2
    (a b c) @ac2 fallthrough
    ;; case 3
    () @ac3 break
  )
)
----

Below is a description of how this example behaves.

==== Case 1

----
((and a b (or c d) (or e f))) a break
----

Translating case 1's logic check to some other common languages
might look like:

----
(a && b && (c || d) && (e || f))
----

If the logic check passes, the action `@ac1` will activate.
No other action will activate since `break` is used.

==== Cases 2 and 3

----
(a b c) c fallthrough
() b break
----

Case 2's key check behaves like that of `fork`, i.e.

    (or a b c)

or for some other common languages:

    a || b || c

If this logic check passes and the case 1 does not pass,
the action `@ac2` will activate first.
Since the logic check of case 3 always passes, `@ac3` will activate next.

If neither case 1 or case 2 pass their logic checks,
case 3 will always activate with `@ac3`.

[[key-history-and-key-timing]]
==== key-history and key-timing

In addition to simple keys there are two list items
that can be used within the case logic check
that compare against your typed key history:

* `key-history`
* `key-timing`

The `key-history` item compares the order that keys were typed.
It accepts, in order:

* a key
* the key recency

The key recency must be in the range 1-8,
where 1 is the most recent key that was pressed
and 8 is 8th most recent key pressed.

.Example:
[source]
----
(defalias
  swh (switch
    ((key-history a 1)) S-a break
    ((key-history b 1)) S-b break
    ((key-history c 1)) S-c break
    ((key-history d 8)) (macro d d d) break
    () XX break
  )
)
----

The `key-timing` compares how long ago recent key typing events occurred.
It accepts, in order,

* the key recency
* a comparison string, which is one of: `less-than|greater-than|lt|gt`
* number of milliseconds to compare against

The key recency must be in the range 1-8,
where 1 is the most recent key that was pressed
and 8 is 8th most recent key pressed.
Most use cases are expected to use a value of 1 for this parameter,
but perhaps you can find a creative use for the other values.

The comparison string determines how the actual key event timing
will be compared to the provided timing.

The number of milliseconds must be 0-65535.

WARNING: The maximum milliseconds value of this configuration item
across your whole configuration
will be a lower bound of how long it takes for kanata to become idle
and stop processing its state machine every approximately 1ms.

.Example:
[source]
----
(defalias
  swh (switch
    ((key-timing 1 less-than 200)) S-a break
    ((key-timing 1 greater-than 500)) S-b break
    ((key-timing 2 lt 1000)) S-c break
    ((key-timing 8 gt 2000)) (macro d d d) break
    () XX break
  )
)
----

==== not

The examples presented so far have not included the `not` boolean operator.
This operator will now be discussed.
Syntactically, the `not` operator is used similarly to `or|and`.
Functionally, it means "not **any** of" the list elements.

.Example:
[source]
----
(defalias
  swn (switch
    ((not x y z)) S-a break
    ;; the above and below cases are equivalent in logic
    ((not (or x y z))) S-a break
  )
)
----

In potentially more familiar notation, both cases have the logic:

    !(x || y || z)

==== input

Until now, all `switch` logic has been associated to key code outputs.
It is also possible to operate on inputs.
Inputs can be either real keys or "virtual" (fake) keys.

.Example:
[source]
----
(defalias switch-input-example
  (switch
    ((input real lctl)) $ac1 break
    ((input virtual vk1)) $ac2 break
    () $ac3 break
  )
)
----

Similar to `key-history` for regular active keys, `input-history` also exists.
A perhaps surprising, but hopefully logical, behaviour of input-history
when compared to key-history is that, at the time of switch activation,
the history of `input-history` for recency `1` will be the just-pressed input.
In other words recency `1` is the input activating the `switch` action itself.
Whereas with `key-history` for example, the key that will be next outputted
may be determined by the switch logic itself, so is not in the history.
The consequence of this is that you should use a recency of `2`
when referring to the previously pressed input
because the current input is in the recency `1` slot.

.Example:
[source]
----
(defalias switch-input-history-example
  (switch
    ((input-history real lsft 2)) $var1 break
    ((input-history virtual vk2 2)) $var1 break
    () $ac3 break
  )
)
----

==== layer

The `layer` list item can be used in `switch` logic to operate on the active layer.
It accepts a single layer name
and evaluates to true if the configured layer name is the active layer,
otherwise it evaluates to false.

.Example:
[source]
----
(defalias switch-layer-example
  (switch
     ((layer base)) x break
    ((layer other)) y break
                 () z break
  )
)
----

==== base-layer

The `base-layer` list item evaluates to true
if the configured layer name is the base layer.
The base layer is the most recently switched-to layer
from a `layer-switch` action,
or the first layer defined in your configuration
if `layer-switch` has never been activated.

.Example:
[source]
----
(defalias switch-layer-example
  (switch
     ((base-layer base)) x break
    ((base-layer other)) y break
                 () z break
  )
)
----


[[cmd]]
=== cmd

WARNING: This action does not work unless you use the appropriate binary
or - if compiling yourself - the appropriate feature flag.
Additionally you must add the <<danger-enable-cmd>> `defcfg` option.

**Reference**

The `cmd` action allows you to execute arbitrary binaries with arbitrary arguments.
The `cmd-log` variant behaves similarly but allows customization
of the stdout and stderr log levels within Kanata's output logging.
The `cmd-output-keys` is like `cmd`, but stdout of the command
will be parsed as a list of keys, output chords, and delays similar to <<macro>>
and be typed as kanata outputs.

.Syntax:
[source]
----
(cmd $binary $arg1 $arg2 ... $argN)
(cmd-log $stdout-log-level $stderr-log-level)
(cmd-output-keys $binary $arg1 $arg2 ... $argN)
----

[cols="1,3"]
|===
| `$binary`
| Executable binary to run.

| `$arg`
| Argument passed into the binary.

| `$stdout-log-level`
| Log level for stdout of the command. Must be `+debug+`, `+info+`, `+warn+`, `+error+`, or `+none+`.

| `$stderr-log-level`
| Log level for stderr of the command. Must be `+debug+`, `+info+`, `+warn+`, `+error+`, or `+none+`.
|===

**Description**

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.
Lists may also be used within `cmd`
which you may desire to do for reuse via `defvar`.
Lists will be flattened such that arguments are provided to the program
in the order written in the config file, regardless of list nesting.
To be technical, it would be a depth-first flattening (similar to DFS).

Commands are executed directly and not via a shell, so you cannot make
use of environment variables or symbols with special meaning.
For example `+~+` or `+$HOME+` in Linux will not be
substituted with your home directory.
If you want to execute with a shell program
use the shell as the first parameter, e.g. `bash` or `powershell.exe`.

The user executing the command
is the user that kanata was started with.
For example, if kanata was started by root,
the command will be run by the root user.
If you need to execute as a different user,
on Unix platforms you can use `sudo -u USER`
before the rest of your command to achieve this.

.Example:
[source]
----
(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")

  ;; You can prefix commands with sudo -u USER
  ;; to execute commands as a different user.
  cm3 (cmd sudo -u other_user bash -c "echo goodbye")
)
----

By default, `+cmd+` logs start of command, completion of command, stdout, and stderr.
Using the variant `+cmd-log+`, these log levels can be changed, and even disabled.
It takes two arguments, `+<log_level>+` and `+<error_log_level>+`. `+<log_level>+`
will be the level where the command to run, stdout, and stderr are logged.
The error channel is logged only if there is a failure with running the
command (typically if the command can't be found, or there is trouble spawning it).

The valid levels are `+debug+`, `+info+`, `+warn+`, `+error+`, and `+none+`.

.Example:
[source]
----
(defalias
  ;; The first two arguments are the log levels, then just the normal command
  ;; This will only error if `bash` is not found or something else goes
  ;; wrong with the initial execution. Any logs produced by bash will not
  ;; be shown.
  noisy-cmd (cmd-log none error bash -c "echo hello this produces a log")

  ;; This will only log the output of the command, but it won't start
  ;; because the command doesn't exist.
  ignore-failure-cmd (cmd-log info none thiscmddoesnotexist)

  verbose-only-log (cmd-log verbose verbose bash -c "echo yo")
)
----

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, macro action>>. However — unlike macro — only delays, keys, chords, and
chorded lists are supported. Other actions are not supported.

[source]
----
(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-;') ')'")
)
----

[[push-msg]]
=== push-msg

NOTE: This action requires the TCP server to be enabled via the
<<args-port,`-p` / `--port`>> command line argument.
If used without the TCP server enabled, a warning will be logged
and the action will have no effect.

**Reference**

The `push-msg` action sends an arbitrary message to all connected TCP clients.
This enables communication between your keyboard configuration and external tools
without the security risks or performance overhead of executing shell commands.

.Syntax:
[source]
----
(push-msg $message)
----

[cols="1,3"]
|===
| `$message`
| A string or S-expression to send to TCP clients.
  Strings are sent as-is; S-expressions are converted to JSON arrays.
|===

**Description**

The `push-msg` action broadcasts a message to all TCP clients connected to Kanata's
TCP server. This is useful for:

- Triggering external tool actions from keyboard shortcuts
- Layer change notifications to status bars or tray applications
- Integration with automation tools like Raycast, Alfred, or Hammerspoon
- Controlling other applications without spawning shell processes

Messages are sent as newline-terminated JSON in the format:

[source,json]
----
{"MessagePush":{"message":"your-message-here"}}
----

Compared to the <<cmd>> action, `push-msg` offers:

- **Better security**: No shell execution, no privilege escalation risks
- **Better performance**: Sub-millisecond latency vs ~100ms for command execution
- **No special flags**: Does not require `danger-enable-cmd` in defcfg

.Example - Basic messages:
[source]
----
(defalias
  ;; Send a simple string message
  notify (push-msg "key-pressed")

  ;; Send layer change notification
  nav-on (push-msg "layer:nav:activated")
  nav-off (push-msg "layer:nav:deactivated")
)
----

.Example - Integration with external tools:
[source]
----
(defalias
  ;; Trigger a Raycast extension
  raycast-clip (push-msg "raycast:clipboard-history")

  ;; Control media playback via external script
  play-pause (push-msg "media:toggle")
  next-track (push-msg "media:next")

  ;; Switch keyboard layouts via external tool
  layout-next (push-msg "xkb:next-layout")
)
----

.Example - Using with defvirtualkeys for external triggering:
[source]
----
;; Define virtual keys that can be triggered via TCP's ActOnFakeKey command
;; External tools can trigger these using: {"ActOnFakeKey":{"name":"email-sig","action":"Tap"}}
(defvirtualkeys
  email-sig (macro S-b e s t spc r e g a r d s , ret ret S-j o h n)
  nav-mode (layer-switch nav)
)

;; Combine push-msg with other actions
(defalias
  launch-term (multi
    (push-msg "app:launch:terminal")
    (layer-switch base)
  )
)
----

To receive these messages, connect a TCP client to Kanata's server port.
See the https://github.com/jtroo/kanata/blob/main/example_tcp_client/src/main.rs[example TCP client]
for implementation guidance.

[[clipboard-actions]]
=== clipboard actions

**Reference**

Clipboard actions can manipulate the operating system clipboard
alongside "save ids".
To paste, you would use an action such as `C-v`;
Kanata has no builtin paste action.

.Syntax:
[source]
----
(clipboard-set   $clipboard-string)
(clipboard-save  $save-id)
(clipboard-restore    $save-id)
(clipboard-save-swap  $save-id $save-id)
(clipboard-cmd-set  $binary $arg1 $arg2 ... $argN)
(clipboard-save-cmd-set  $save-id $binary $arg1 $arg2 ... $argN)
----

[cols="1,3"]
|===
| `clipboard-set`
| Sets the clipboard to the specified string.

| `clipboard-save`
| Saves the current clipboard content with the specified ID.

| `clipboard-restore`
| Sets the clipboard to content saved with the specified ID.
If the save ID content is blank, this will do nothing.

| `clipboard-save-swap`
| Swaps the content of two save IDs.

| `clipboard-cmd-set`
| Sets the clipboard to the output of the command.
The current content of the clipboard is passed to stdin of the command
if the current content is text.
If the content is an image, nothing is passed to stdin.

| `clipboard-save-cmd-set`
| Sets the save ID content to the output of the command.
The current content of the save ID is passed to stdin of the command
if the current content is text.
If the content is an image, nothing is passed to stdin.

| `$clipboard-string`
| Fixed string to set the operating system clipboard to.

| `$save-id`
| A number `0-65535` representing an ID of saved clipboard content.

| `$binary`
| Executable binary to run.

| `$arg`
| Argument passed into the binary.
|===

**Description**

You can use clipboard actions to save clipboard content,
paste arbitrary content,
and then restore the original clipboard content.
This functionality is similar to what some text expanders do.
You can additionally use shell commands to manipulate
clipboard content in arbitrary ways.
This can all be done in a single command via <<macro>>.

Note that you will likely want to add delays in between components,
because clipboard systems take some time to propagate updates.

The example below is a macro that pastes the content of the clipboard
twice with a space in between,
while restoring the original clipboard content at the end.
Notable is that the example uses `C-v` but this may not work for you.
If you have OS-level remapping, the `v` may be different
to effectively paste.
Something that may also work is `S-ins`

.Example:
[source]
----
(macro
 (clipboard-save 0)
 20
 (clipboard-cmd-set powershell.exe -c r#"$v = ($Input | Select-Object -First 1); Write-Host -NoNewLine "$v $v""#)
 300
 C-v
 (clipboard-restore 0)
)
----

As another example you can use templates
with clipboard actions to have a convenient clipboard-based
text output while preserving the old clipboard content.
This can fit the use case of "text expansion".

.Example:
[source]
----
(deftemplate text-paste (text)
 (macro
  (clipboard-save 0)
  20
  (clipboard-set $text)
  300
  C-v
  (clipboard-restore 0)
 ))

(defalias
 myalias1 (t! text-paste "Hello world")
 myalias2 (t! text-paste "Goodbye my old friend")
)
----

[[arbitrary-code]]
=== arbitrary-code

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.

[source]
----
(defalias ab1 (arbitrary-code 700))
----

[[global-overrides]]
== Global overrides

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:

1. the input key list that gets replaced
2. 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.

NOTE: Depending on your use case
you may want to adjust <<override-release-on-activation>> in `defcfg`.

.Example:
[source]
----
;; 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
)
----

[[defoverridesv2]]
=== defoverridesv2

There is a more recent version of defoverrides that offers more customizability.
Instead of 2 list items per override entry,
`defoverridesv2` mandates 4, though the extra 2 can be empty.

You cannot have both a v1 and v2 of `defoverrides` at the same time.

The 3rd item is an "exclude modifiers list" which is composed of modifier key names
(such as `lctl`, `lalt`) that, if held, will disable the override from activating.

NOTE: if all excluded list modifiers are released while the
override input is still active, the override will activate.
You can avoid this by ensuring to always release the non-modifier key
before releasing any modifiers.

The 4th item is an "exclude layers" list which is composed of layer names
that while active as the most recent `layer-switch` or `layer-while-held`,
will disable the override from activating.

NOTE: if the layer changes while the override input is still active,
the override will activate.

.Example:
[source]
----
(defoverridesv2
  ;; lctl+a will become lalt+9
  ;; except when lsft is held or other-layer is active.
  (lctl a) (lalt 9) (lsft) (other-layer))

  ;; lctl+b will always become lalt+0
  (lctl b) (lalt 0) () ()
)
----

== Include other files[[include]]

The `include` optional configuration item
allows you to include other files into the configuration.
This configuration accepts a single string which is a file path.
The file path can be an absolute path or a relative path.
The path will be relative to the defined configuration file.

At the time of writing, includes can only be placed at the top level.
The included files also cannot contain includes themselves.

Non-existing files will be ignored.

.Example:
----
;; This is in the file initially read by kanata, e.g. kanata.kbd
(include other-file.kbd)

;; This is in the other file
(defalias
  included-alias XX
  ;; ...
)

;; This is in the other file
(deflayer included-layer
  ;; ...
)
----

[[platform]]
== Platform-specific configuration

If you put any top-level configuration item
within a list beginning with `platform`,
it will become a platform-specific configuration
that is only active for the specified platforms.

.Syntax:
[source]
----
(platform (applicable-platforms) ...)
----

The valid values for applicable platforms are:

- `win`
- `winiov2`
- `wintercept`
- `linux`
- `macos`

.Example:
[source]
----
(platform (macos)
  ;; Only on macos, use command arrows to jump/delete words
  ;; because command is used for so many other things
  ;; and it's weird that these cases use alt.
  (defoverrides
    (lmet bspc) (lalt bspc)
    (lmet left) (lalt left)
    (lmet right) (lalt right)
  )
)

(platform (win winiov2 wintercept)
  (defalias run-my-script (cmd #| something involving powershell |#))
)

(platform (macos linux)
  (defalias run-my-script (cmd #| something involving bash |#))
)
----

[[environment]]
== Environment-conditional configuration

.Syntax:
[source]
----
(environment (env-var-name env-var-value) ...)
----

The items `env-var-name` and `env-var-value` can be arbitrary strings.
The name is the environment variable that is read
for determining if the configuration is used or not.
If the value of the environment variable (set only on kanata startup)
matches `env-var-value`, the configuration is used; otherwise it is ignored.
An empty string for `env-var-value` — `""` — will use the configuration
if the environment variable is an empty string
and also if the environment variable is not defined.

.Example:
[source]
----
(environment (LAPTOP lp1)
  (defalias met @lp1met)
)

(environment (LAPTOP lp2)
  (defalias met @lp2met)
)
----

.Set environment variables in the current terminal process:
[source]
----
# powershell
$env:VAR_NAME = "var_value"

# bash
VAR_NAME=var_value
----


[[input-chords-v2]]
== Input chords / combos (v2)

You may define a single `+defchordsv2+` configuration item.
This enables you to define global input chord behaviour.
One might also find this functionality called another name of "combos"
in other projects.

Input chords enables you to press two or more keys in quick succession
to activate a different action
than would normally be associated with those keys.
When activating a chord, the order of presses is not important;
when all keys belonging to a chord are pressed,
the action activates regardless of press order.

The `+defchordsv2+` feature is configured as shown below:

.Syntax example
[source]
----
(defchordsv2
  (participating-keys1) action1 timeout1 release-behaviour1 (disabled-layers1)
    ...
  (participating-keysN) actionN timeoutN release-behaviourN (disabled-layersN)
)
----

The configuration is made up of 5-tuples of:

[cols="1,3"]
|===
| `$participating-keys`
| These are key names you would use in `defsrc`.
A minimum of two keys must be defined per chord.
The list must be unique per chord.

| `$action`
| These are actions as you would configure in `deflayer` or `defalias`.
The action activates if all participating keys are activated
within the timeout.

| `$timeout`
| The time (unit: milliseconds) within which,
if all participating keys are pressed,
the chord action will activate;
otherwise the key presses are handled by the active layer.
The time begins when the first participant is pressed.

| `$release-behaviour`
| This must be either `first-release` or `all-released`;
`first-release` means the chord action will be released
when the first participant is released,
while `all-released` means the chord action will be released
only when all of the participants have been released.

| `$disabled-layers`
| A list of layer names on which this chord is disabled.
|===

Input chords have a related `defcfg` item: <<chords-v2-min-idle>>.
When any non-chord activation happens,
a timeout begins with duration configured by
`chords-v2-min-idle` (unit: milliseconds).
Until this timeout expires, all inputs will immediately skip
chords processing and be processed by the active layer.

IMPORTANT: When opting into input chords v2,
you must enable `concurrent-tap-hold`.
This is enforced for a more responsive `tap-hold` experience when
activated by a chord.

.Example:
[source]
----
(defcfg concurrent-tap-hold yes)
(defchordsv2
  (a s)    c                200 all-released  (non-chord-layer)
  (a s d) (macro h e l l o) 250 first-release (non-chord-layer)
  (s d f) (macro b y e)     400 first-release (non-chord-layer)
)
----

NOTE: Also see <<input-chords,v1 chords>>,
which are configured differently and can be defined per-layer.

[[chordsv2-processing-order]]
=== Action processing order

The chordsv2 system handles keys before any layer action handling.
This has a consequence of not entering the delay and interruption flow
of actions such as `tap-hold-release`.

If you want a chordsv2 action activation to be processed
by the aforementioned category of actions
you can move the action into a virtual key
(see <<virtual-keys>>) and have the chordsv2 action
press and release the virtual key.

See a sample below that showcases a reusable template.

.Example:
[source]
----
(defcfg concurrent-tap-hold yes)
(defsrc)
(deflayermap (base)
  caps (tap-hold-release 0 200 esc lctl))

;; defines a vkey named v-$key, example v-bspc
;; and an alias @v-bspc that press and and releases the v-key
;; within on-press and on-release respectively.
(deftemplate v- (key)
  (defvirtualkeys (concat v- $key) $key)
  (defalias (concat v- $key)
    (multi (on-press press-vkey (concat v- $key)) (on-release release-vkey (concat v- $key))))
)

(t! v- bspc)
(t! v- del)
(defchordsv2
  (j k) @v-bspc 75 first-release ()
  (s d) @v-del 75 first-release ()
)
----

[[optional-defcfg-options]]
== defcfg options

[[danger-enable-cmd]]
=== danger-enable-cmd

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:
[source]
----
(defcfg
  danger-enable-cmd yes
)
----

[[sequence-timeout]]
=== sequence-timeout

This option customizes the key sequence timeout (unit: ms). Its default value
is 1000. The purpose of this item is explained in <<sequences>>.

.Example:
[source]
----
(defcfg
  sequence-timeout 2000
)
----

[[sequence-input-mode]]
=== sequence-input-mode

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:
[source]
----
(defcfg
  sequence-input-mode visible-backspaced
)
----


[[sequence-backtrack-modcancel]]
=== sequence-backtrack-modcancel

This option customizes the behaviour of key sequences
when modifiers are used.
The default is `yes` and can be overridden to `no` if desired.

Setting it to `yes` allows both `fk1` and `fk2` to be activated
in the following configuration, but with `no`,
`fk1` will be impossible to activate

----
(defseq
  fk1 (lsft a b)
  fk2 (S-(c d))
)
----

See <<sequences>> for more about sequences and
https://github.com/jtroo/kanata/blob/main/docs/sequence-adding-chords-ideas.md[this document]
for more context about this specific configuration.

.Example:
[source]
----
(defcfg
  sequence-backtrack-modcancel no
)
----

[[log-layer-changes]]
=== log-layer-changes

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:
[source]
----
(defcfg
  log-layer-changes no
)
----

If `+--log-layer-changes+` is passed as a command line argument,
a `no` in the configuration file will be overridden
and layer changes will again be logged.
This flag can be helpful when testing new configuration changes
while keeping the default behaviour as "no logging" to save on processing,
so that the `defcfg` item does not need to be adjusted back and forth
when experimenting vs. stable usage.

[[delegate-to-first-layer]]
=== delegate-to-first-layer


By default, transparent keys on layers
will delegate to the corresponding defsrc key
when found on a layer activated by `layer-switch`.

This config entry changes the behaviour
to delegate to the action in the same position on the first layer defined
in the configuration, which is the active layer on startup.

For more context, see https://github.com/jtroo/kanata/issues/435.

.Example:
[source]
----
(defcfg
  delegate-to-first-layer yes
)
----


[[movemouse-inherit-accel-state]]
=== movemouse-inherit-accel-state

By default `movemouse-accel` actions will track the acceleration
state for vertical and horizontal axes separately.

When this setting is enabled, `movemouse-accel` will behave exactly like mouse movements in https://qmk.fm[QMK],
i.e. the acceleration state of new mouse
movement actions will be inherited if others are already being pressed.

.Example:
[source]
----
(defcfg
  movemouse-inherit-accel-state yes
)
----

[[movemouse-smooth-diagonals]]
=== movemouse-smooth-diagonals

By default, mouse movements move one direction at a time
and vertical/horizontal movements are on independent timers.

This can result in non-smooth diagonals when drawing a line in some app.
This option adds a small imperceptible amount of latency to
synchronize the mouse movements.

.Example:
[source]
----
(defcfg
  movemouse-smooth-diagonals yes
)
----

=== dynamic-macro-max-presses [[dynamic-macro-max-presses]]

This configuration allows you to customize the length limit on dynamic macros.
The default length limit is 128 keys.

.Example:
[source]
----
(defcfg
  dynamic-macro-max-presses 1000
)
----

=== concurrent-tap-hold [[concurrent-tap-hold]]
This configuration makes multiple tap-hold actions
that are activated near in time expire their timeout quicker.
By default this is disabled.
When disabled, the timeout for a following tap-hold
will start from 0ms **after** the previous tap-hold expires.
When enabled, the timeout will start
as soon as the tap-hold action is pressed
even if a previous tap-hold action is still held and has not expired.

.Example:
[source]
----
(defcfg
  concurrent-tap-hold yes
)
----

[[block-unmapped-keys]]
=== block-unmapped-keys

If you desire to use only a subset of your keyboard
you can use `block-unmapped-keys` to make every key
other than those that exist in `defsrc` a no-op.

NOTE: this only functions correctly if you also set
<<process-unmapped-keys>> to yes.

.Example:
[source]
----
(defcfg
  block-unmapped-keys yes
)
----

[[rapid-event-delay]]
=== rapid-event-delay

This configuration applies to the following events:

* the release of one-shot-press activation
* the release of the tapped key in a tap-hold activation
* a non-eager tap-dance activation from interruption by another key
* input chord activations, both v1 and v2

Key event processing is paused the defined number of milliseconds (approximate).
The default value is 5.

There will be a minor input latency impact in the mentioned scenarios.
Since 5ms is 1 frame at 200 Hz refresh rate,
in most scenarios this will not be perceptible.

The reason for this configuration existing is that some environments
do not process the scenarios correctly due to the rapidity of key events.
Kanata does send the events in the correct order,
so the fault is more in the environment,
but kanata provides a workaround anyway.

If you are negatively impacted by the latency increase of these events
and your environment is not impacted by increased rapidity,
you can reduce the value to a number between 0 and 4.

.Example:
[source]
----
(defcfg
  ;; If your environment is particularly buggy, might need to delay even more
  rapid-event-delay 20
)
----

[[chords-v2-min-idle]]
=== chords-v2-min-idle

This configuration affects the timer during which chords processing is disabled.
NOTE: For more info, see <<input-chords-v2>>.

The default (and minimum) value is `5` and the unit is milliseconds.

.Example:
[source]
----
(defcfg
  chords-v2-min-idle 200
)
----

[[override-release-on-activation]]
=== override-release-on-activation

This configuration item changes activation behaviour from `defoverrides`.

Take this example override:

[source]
----
(defoverrides (lsft a) (lsft 9))
----

The default behaviour is that if `lsft` is released **before** releasing `a`,
kanata's behaviour would be to send `a`.

A future improvement could be to make the `9` continue to be the key held,
but that is not implemented today.

The workaround in case the above behaviour negatively impacts your workflow
is to enable this configuration.
This configuration will press and then immediately release the `9` output
as soon as the override activates, meaning you are unlikely as a human to ever
release `lsft` first.

The effect of this configuration is that the `9` key cannot remain held
when activated by the override which is important to consider for your use cases.

.Example:
[source]
----
(defcfg
  override-release-on-activation yes
)
----

[[allow-hardware-repeat]]
=== allow-hardware-repeat

By default, any repeat-key events generated by the physical keyboard (or operating system)
will be passed through to the application.  On Linux, under Wayland, this is wasted effort
since the DE handles key-repeat on its own.  Such events can also be distracting when
debugging your configuration with evtest, etc.

Setting this option to "false" will cause such events to be dropped, and not passed through.
This is primarily meant for Linux, but may find some use on Mac.  It is not implemented on
Windows, and will be silently ignored.

.Example:
[source]
----
(defcfg
   allow-hardware-repeat false
)
----

[[alias-to-trigger-on-load]]
=== alias-to-trigger-on-load

Select an alias to execute when first starting, and after each
live-reload of the config. You can use this to run external
commands, or to stack layers (with layer-while-held).

The name of an alias, without a leading "@", is expected as a
parameter.  The example below will beep at startup (assuming
your system has a beep command), and will already be blocking
the swapped "i" and "o" keys.

.Example:
[source]
----
(defcfg
  alias-to-trigger-on-load S
  danger-enable-cmd yes
)

(deffakekeys B (layer-while-held block))

(defalias
  P (on-press toggle-vkey B)
  S (macro @P (cmd beep))
)

(defsrc i o p )
(deflayer base o i @P )
(deflayer block • • _ )
----

[[mouse-movement-key]]
=== Linux or Windows-interception only: mouse-movement-key

Accepts a single key name.
When configured, whenever a mouse cursor movement is received,
the configured key name will be "tapped" by Kanata,
activating the key's action.

This enables reporting of every relative mouse movement, which
corresponds to standard mice, trackballs, trackpads and
trackpoints. Absolute movements, which can be generated by
touchscreens, drawing tablets and some mouse replacement or
accessibility software, are ignored. Scrolling events and mouse
buttons are also ignored.

The intended use of these events is to provide a way to automatically
enable a mouse keys layer while mousing, which can be disabled by a
timeout or typing on other keys, rather than explicit toggling. see
cfg_examples/automousekeys-*.kbd for more.

The `mvmt` key name is specially intended for this purpose.  It has no
output key mapping and cannot be supplied as an action; however, any
key may be used.

Supports live reload on Linux, but with Windows-interception, this
option must be present on startup to enable mouse movement event
collection, so restart is required to enable it. Changing the key name
is always supported, however.

.Example:
[source]
----
(defcfg
  process-unmapped-keys yes
  mouse-movement-key mvmt
)

(defsrc
  k     l     ;
  mvmt
)

(defvirtualkeys
  mouse (layer-while-held mouse-layer)
)

(defalias
  mhld (hold-for-duration 750 mouse)
)

(deflayer qwerty
  k     l     ;
  @mhld
)

(deflayer mouse-layer
  mlft  mmid  mrgt
  @mhld
)
----

[[linux-only-linux-dev]]
=== Linux only: linux-dev

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:
[source]
----
(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:
[source]
----
(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:

[source]
----
(defcfg
  linux-dev /dev/input/path-to\:device
)
----

Alternatively, you can use list syntax, where both backslashes and colons
are parsed literally. List items are separated by spaces or newlines.
Using quotation marks for each item is optional, and only required if an
item contains spaces.

[source]
----
(defcfg
  linux-dev (
    /dev/input/path:to:device
    "/dev/input/path to device"
  )
)
----

For devices that do not have an easily identifiable device path like Bluetooth
keyboards using the `linux-dev-names-include` option below is recommended.

[[linux-only-linux-dev-names-include]]
=== Linux only: linux-dev-names-include

In the case that `linux-dev` is omitted,
this option defines a list of device names that should be included.
Device names that do not exist in the list will be ignored.

Device paths are not supported by this option; instead use the device names
as output by Kanata during startup.  Launch Kanata with a
<<minimal-configuration,minimal configuration>> (any use of a `linux-dev*`
option may hide devices) and look for lines beginning with "registering":

----
registering /dev/input/eventX: "Device name 1"
registering /dev/input/eventY: "Device name 2"
----

The entire name within quotes must be used, partial matches and regex's are not supported.

.Example:
[source]
----
(defcfg
  linux-dev-names-include (
    "Device name 1"
    "Device name 2"
  )
)
----

[[linux-only-linux-dev-names-exclude]]
=== Linux only: linux-dev-names-exclude

In the case that `linux-dev` is omitted,
this option defines a list of device names that should be excluded.
This option is parsed identically to `linux-dev-names-include`.

The `linux-dev-names-include` and `linux-dev-names-exclude` options
are not mutually exclusive
but in practice it probably only makes sense to use one and not both.

.Example:
[source]
----
(defcfg
  linux-dev-names-exclude (
    "Device Name 1"
    "Device Name 2"
  )
)
----

[[linux-only-linux-continue-if-no-devs-found]]
=== Linux only: linux-continue-if-no-devs-found

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:
[source]
----
(defcfg
  linux-continue-if-no-devs-found yes
)
----

[[linux-only-linux-device-detect-mode]]
=== Linux only: linux-device-detect-mode

Kanata on Linux automatically detects and grabs input devices
when none of the explicit device configurations are in use.
In case kanata is undesirably grabbing mouse-like devices,
you can use a configuration item to change detection behaviour.

The configuration is `linux-device-detect-mode` and it has the options:

[cols="1,4"]
|===
| `keyboard-only`
| Grab devices that seem to be a keyboard only.

| `keyboard-mice`
| Grab devices that seem to be a keyboard only
and devices that declare **both** keyboard and mouse functionality.

| `any`
| Grab all keyboard-like and mouse-like devices.
|===

The default behaviour is:

[cols="1,4"]
|===
| `keyboard-mice`
| When no mouse events are in `defsrc`.

| `any`
| When any mouse buttons or mouse scroll events are in `defsrc`.
|===

[[linux-only-linux-unicode-u-code]]
=== Linux only: linux-unicode-u-code

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:
[source]
----
(defcfg
  linux-unicode-u-code v
)
----

[[linux-only-linux-unicode-termination]]
=== Linux only: linux-unicode-termination

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`

.Example:
[source]
----
(defcfg
  linux-unicode-termination space
)
----

=== Linux only: linux-x11-repeat-delay-rate[[linux-only-x11-repeat-rate]]

On Linux, you can tell kanata to run `xset r rate <delay> <rate>`
on startup and on live reload
via the configuration item `linux-only-x11-repeat-rate`.
This takes two numbers separated by a comma.
The first number is the delay in ms
and the second number is the repeat rate in repeats/second.

This configuration item does not affect Wayland or no-desktop environments.

.Example:
[source]
----
(defcfg
  linux-x11-repeat-delay-rate 400,50
)
----

[[linux-only-linux-use-trackpoint-property]]
=== Linux only: linux-use-trackpoint-property

On linux, you can ask kanata to label itself as a trackpoint. This has several
effects on libinput including enabling middle mouse button scrolling and using a
different acceleration curve. Otherwise, a trackpoint intercepted by kanata may
not behave as expected.

If using this feature, it is recommended to filter out any non-trackpoint
pointing devices using <<linux-only-linux-dev-names-include>>,
<<linux-only-linux-dev-names-exclude>> or <<linux-only-linux-dev>> to avoid
changing their behavior as well.

.Example:
[source]
----
(defcfg
  linux-use-trackpoint-property yes
)
----

[[linux-only-linux-output-device-name]]
=== Linux only: linux-output-device-name

This option defines the name of the evdev output device.
The default value is kanata.

.Example:
[source]
----
(defcfg
   linux-output-device-name "kanata output"
)
----

[[linux-only-linux-output-device-bus-type]]
=== Linux only: linux-output-device-bus-type

Kanata on Linux needs to declare a "bus type" for its evdev output device.
The options are `USB` and `I8042`, with the default as `I8042`.
Using USB can https://github.com/jtroo/kanata/pull/661[break disable-touchpad-while-typing on Wayland].
But using I8042 appears to break https://github.com/jtroo/kanata/issues/1131[some other scenarios].
Thus the output bus type is configurable.

.Example:
[source]
----
(defcfg
   linux-output-device-bus-type USB
)
----

[[macos-only-macos-dev-names-include]]
=== macOS only: macos-dev-names-include

This option defines a list of device names that should be included.
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 to intercept
using the `macos-dev-names-include` configuration.
Device names that do not exist in the list will be ignored.
This option is parsed identically to `linux-dev`.

Use `kanata -l` or `kanata --list` to list the available keyboards.

.Example:
[source]
----
(defcfg
  macos-dev-names-include (
    "Device name 1"
    "Device name 2"
  )
)
----

[[macos-only-macos-dev-names-exclude]]
=== macOS only: macos-dev-names-exclude

This option defines a list of device names that should be excluded.
By default, kanata will try to detect which input devices are keyboards and try
to intercept them all. However, you may specify certain keyboard devices to be ignored
using the `macos-dev-names-exclude` configuration.
Device names that do not exist in the list will be included.
This option is parsed identically to `linux-dev`.

Use `kanata -l` or `kanata --list` to list the available keyboards.

.Example:
[source]
----
(defcfg
  macos-dev-names-exclude (
    "Device name 1"
    "Device name 2"
  )
)
----
[[windows-only-windows-altgr]]
=== Windows only: windows-altgr

There is an option for Windows to mitigate the strange behaviour of AltGr (ralt)
if you're using `process-unmapped-keys yes` or have the 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-press`
** This will remove the `lctl` press that is generated alonside `ralt`
* `add-lctl-release`
** This adds an `lctl` release when `ralt` is released

Without these workarounds,
you should use `process-unmapped-keys (all-except lctl ralt))`,
or use `process-unmapped-keys no`
**and** also omit both `ralt` and `lctl` from `defsrc`.

.Example:
[source]
----
(defcfg
  windows-altgr add-lctl-release
)
----

For more context, see: https://github.com/jtroo/kanata/issues/55.

NOTE: Even with these workarounds, putting `+lctl+` and `+ralt+` in your defsrc may not
work properly with other applications that also use keyboard interception.
Known application with issues: GWSL/VcXsrv

=== Windows only: windows-interception-mouse-hwid[[windows-only-windows-interception-mouse-hwid]]

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 release binaries).

The original 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.

Hwids in Kanata are byte array representations of a concatenation of the
ASCII hardware ids, which can be seen in Device Manager on Windows. As such,
they are an arbitrary length and can be very long.

https://github.com/jtroo/kanata/issues/108[Relevant issue].

.Example:
[source]
----
(defcfg
  windows-interception-mouse-hwid "70, 0, 60, 0"
)
----

=== Windows only: windows-interception-mouse-hwids[[windows-only-windows-interception-mouse-hwids]]

This item has a similar purpose as the singular version documented above,
but is instead a list of strings that allows multiple mice to be intercepted.

If both the singular and list items are used,
the singular version will behave as if added to the list.

.Example:
[source]
----
(defcfg
  windows-interception-mouse-hwids (
    "70, 0, 60, 0"
    "71, 0, 62, 0"
  )
)
----

=== Windows only: windows-interception-keyboard-hwids[[windows-only-windows-interception-keyboard-hwids]]

This defcfg item allows you to intercept only specific keyboards.
Its value must be a list of strings
with each string representing one hardware ID.

To know what numbers to put into the string,
you can run the variant with this defcfg item empty.
Then when a button is first pressed on the keyboard,
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.

Hwids in Kanata are byte array representations of a concatenation of the
ASCII hardware ids, which can be seen in Device Manager on Windows. As such,
they are an arbitrary length and can be very long.

.Example:
[source]
----
(defcfg
  windows-interception-keyboard-hwids (
    "70, 0, 60, 0"
    "71, 72, 73, 74"
  )
)
----

=== Windows only: windows-interception-keyboard-hwids-exclude[[windows-only-windows-interception-keyboard-hwids-exclude]]

This defcfg item allows you to exclude certain keyboards from being intercepted.
You cannot define this alongside the inclusive keyboard configuration.

It is parsed identically to the inclusive configuration.

.Example:
[source]
----
(defcfg
  windows-interception-keyboard-hwids-exclude (
    "70, 0, 60, 0"
    "71, 72, 73, 74"
  )
)
----

=== Windows only: windows-interception-mouse-hwids-exclude[[windows-only-windows-interception-mouse-hwids-exclude]]

This defcfg item allows you to exclude certain mice from being intercepted.
You cannot define this alongside the inclusive mouse configuration.

It is parsed identically to the inclusive configuration.

.Example:
[source]
----
(defcfg
  windows-interception-mouse-hwids-exclude (
    "70, 0, 60, 0"
    "71, 0, 62, 0"
  )
)
----

[[windows-only-tray-icon]]
=== Windows only: tray-icon

Show a custom tray icon file for a <<windows-only-win-tray>> gui-enabled build of kanata on Windows.
Accepts either the full path (including the file name with an extension) to the icon file
or just the file name, which is then searched in the following locations:

* Default parent folders:
** config file's, executable's
** env vars: `XDG_CONFIG_HOME`, `APPDATA` (`C:\Users\<Name>\AppData\Roaming`), `USERPROFILE` `/.config` (`C:\Users\<Name>\.config`)
* Default config subfolders: `kanata` `kanata-tray`
* Default image subfolders (optional): `icon` `img` `icons`
* Supported image file formats: `ico` `jpg` `jpeg` `png` `bmp` `dds` `tiff`

If not specified, tries to load any icon file from the same locations with the name matching
config name with extension replaced by one of the supported ones.
See https://github.com/jtroo/kanata/blob/main/cfg_samples/tray-icon/tray-icon.kbd[example config] for more details.

.Example:
[source]
----
;; in a config file C:\Users\<U>\AppData\Roaming\kanata\kanata.kbd
(defcfg
  tray-icon base.png ;; will load C:\Users\<U>\AppData\Roaming\kanata\base.png
)
----

[[windows-only-icon-match-layer-name]]
=== Windows only: icon-match-layer-name

When enabled, attempt to switch to a custom tray icon that matches the name of the active layer
if the layer doesn't specify an explicit icon. If no icon file is found, the default icon will be used (see <<windows-only-tray-icon>>).
File search rules are the same as in <<windows-only-tray-icon>>. Defaults to true.
See https://github.com/jtroo/kanata/blob/main/cfg_samples/tray-icon/tray-icon.kbd[example config] for more details.

[[windows-only-tooltip-layer-changes]]
=== Windows only: tooltip-layer-changes

Show a custom layer icon near the mouse pointer position. Defaults to false. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-tooltip-show-blank]]
=== Windows only: tooltip-show-blank

Show a blank square when instead of an icon if a layer isn't configured to have one. Defaults to false. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-tooltip-no-base]]
=== Windows only: tooltip-no-base

Don't show a tooltip layer icon for the base layer (1st deflayer). Defaults to true. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-tooltip-duration]]
=== Windows only: tooltip-duration

Set duration (in ms) for showing a custom layer icon near the mouse pointer position. 0 to never hide. Defaults to 500. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-tooltip-size]]
=== Windows only: tooltip-size

Set the size (comma-separated Width,Height without spaces) for a custom layer icon near the mouse pointer position. Defaults to 24,24. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-notify-cfg-reload]]
=== Windows only: notify-cfg-reload

Show system notification message on config reload. Defaults to true. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-notify-cfg-reload-silent]]
=== Windows only: notify-cfg-reload-silent

Disable sound for the system notification message on config reload. Defaults to false. Requires <<windows-only-win-tray>> gui-enabled build.

[[windows-only-notify-error]]
=== Windows only: notify-error

Show system notification message on kanata errors. Defaults to true. Requires <<windows-only-win-tray>> gui-enabled build.

[[using-multiple-defcfg-options]]
=== Using multiple defcfg options

The `defcfg` entry is treated as a list with pairs of strings. For example:

[source]
----
(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 many of the options is shown below. It should be
noted options that are Linux-only, Windows-only, or macOS-only will be ignored when used on
a non-applicable operating system.

[source]
----
;; Don't actually use this exact configuration,
;; it's almost certainly not what you want.
(defcfg
  process-unmapped-keys yes
  danger-enable-cmd yes
  sequence-timeout 2000
  sequence-input-mode visible-backspaced
  sequence-backtrack-modcancel no
  log-layer-changes no
  delegate-to-first-layer yes
  movemouse-inherit-accel-state yes
  movemouse-smooth-diagonals yes
  dynamic-macro-max-presses 1000
  linux-dev (/dev/input/dev1 /dev/input/dev2)
  linux-dev-names-include ("Name 1" "Name 2")
  linux-dev-names-exclude ("Name 3" "Name 4")
  linux-continue-if-no-devs-found yes
  linux-unicode-u-code v
  linux-unicode-termination space
  linux-x11-repeat-delay-rate 400,50
  windows-altgr add-lctl-release
  windows-interception-mouse-hwid "70, 0, 60, 0"
)
----

== Command line arguments[[cli-args]]

When executing Kanata, you can pass a variety of arguments to the program
to change Kanata's behaviour.
This section describes the purpose and usage of these arguments.

[[args-help]]
=== Show help text: `-h`, `--help`

Only show help text that describes these arguments,
then exit the program.

[[args-config-file]]
=== Configuration file(s): `-c`, `--cfg`

Configuration file(s) to use with Kanata.
When not specified, default file locations are checked.
The default files checked depend on your operating system
and are described by `--help`.

This argument can be used more than once;
see <<live-reload>> to understand the behaviour of this.
The startup configuration will be the first file listed.

[[args-tcp]]
=== TCP server address: `-p`, `--port`

Enable the TCP server capability and listen on either:
- a specified port, on all IP addresses
- a specific `IP:PORT`

This enables use cases such as
https://github.com/jtroo/kanata?tab=readme-ov-file#community-projects-related-to-kanata[application aware switching].

The protocol is plaintext newline-terminated JSON.
The source of truth is the
https://github.com/jtroo/kanata/blob/main/tcp_protocol/src/lib.rs[TCP protocol code].

You may also be interested in the
https://github.com/jtroo/kanata/blob/main/example_tcp_client/src/main.rs[example client].

==== TCP Protocol Overview

The TCP server uses a simple request/response model with JSON messages.
Each message is a single line of JSON terminated by a newline (`\n`).

- **Client → Server**: Commands to control Kanata (reload config, switch layers, etc.)
- **Server → Client**: Responses to commands and event notifications (layer changes, config reloads, etc.)

==== Client Commands

These JSON messages can be sent from a TCP client to control Kanata:

===== Layer Control

[cols="1,2"]
|===
| Command | Description

| `{"ChangeLayer":{"new":"layer-name"}}`
| Switch to the specified layer. Equivalent to the `layer-switch` keyboard action.

| `{"RequestLayerNames":{}}`
| Request a list of all defined layer names. Server responds with `LayerNames`.

| `{"RequestCurrentLayerName":{}}`
| Request the name of the currently active layer. Server responds with `CurrentLayerName`.

| `{"RequestCurrentLayerInfo":{}}`
| Request the current layer's name and full configuration text. Server responds with `CurrentLayerInfo`.
|===

.Example - Query and switch layers:
[source,bash]
----
# Get all layer names
echo '{"RequestLayerNames":{}}' | nc localhost 7070

# Get current layer
echo '{"RequestCurrentLayerName":{}}' | nc localhost 7070

# Switch to nav layer
echo '{"ChangeLayer":{"new":"nav"}}' | nc localhost 7070
----

===== Virtual Key Actions

[cols="1,2"]
|===
| Command | Description

| `{"RequestFakeKeyNames":{}}`
| Request a list of all defined virtual key names. Server responds with `FakeKeyNames`.

| `{"ActOnFakeKey":{"name":"key-name","action":"Tap"}}`
| Trigger a virtual key defined in `defvirtualkeys`. Actions: `Press`, `Release`, `Tap`, `Toggle`.
|===

Virtual keys must be defined in your configuration using `defvirtualkeys`.
See the <<virtual-keys>> section for details.

.Example - Trigger a text expansion macro:
[source]
----
;; In your config:
(defvirtualkeys
  email-sig (macro S-b e s t spc r e g a r d s)
)

;; From TCP client:
echo '{"ActOnFakeKey":{"name":"email-sig","action":"Tap"}}' | nc localhost 7070
----

===== Mouse Control

[cols="1,2"]
|===
| Command | Description

| `{"SetMouse":{"x":100,"y":200}}`
| Set the mouse cursor position to absolute screen coordinates.
|===

This is the TCP equivalent of the <<set-mouse>> keyboard action.

===== Configuration Reload

[cols="1,2"]
|===
| Command | Description

| `{"Reload":{}}`
| Reload the current configuration file. Equivalent to `lrld` keyboard action.

| `{"ReloadNext":{}}`
| Load the next configuration file in the list. Equivalent to `lrnx` keyboard action.

| `{"ReloadPrev":{}}`
| Load the previous configuration file in the list. Equivalent to `lrpv` keyboard action.

| `{"ReloadNum":{"index":0}}`
| Load configuration file at the specified index (0-based).

| `{"ReloadFile":{"path":"/path/to/config.kbd"}}`
| Load a specific configuration file by path.
|===

All reload commands support optional `wait` and `timeout_ms` fields for synchronous confirmation:

[source,json]
----
{"Reload":{"wait":true,"timeout_ms":5000}}
----

When `wait` is `true`, the server blocks until the reload completes or times out, then sends a `ReloadResult` message. The `timeout_ms` field specifies the maximum wait time in milliseconds (default: 5000).

===== Server Information

[cols="1,2"]
|===
| Command | Description

| `{"Hello":{}}`
| Request server version and capabilities. Server responds with `HelloOk`.
|===

==== Server Messages

These JSON messages are sent from Kanata to connected TCP clients:

===== Event Notifications

These are sent automatically when events occur:

[cols="1,2"]
|===
| Message | Description

| `{"LayerChange":{"new":"layer-name"}}`
| Sent when the active layer changes.

| `{"ConfigFileReload":{"new":"/path/to/config.kbd"}}`
| Sent when a configuration file is reloaded.

| `{"MessagePush":{"message":"your-message"}}`
| Sent when a `push-msg` action is triggered from the keyboard configuration.

| `{"Error":{"msg":"error description"}}`
| Sent when an error occurs processing a command.
|===

===== Query Responses

These are sent in response to client queries:

[cols="1,2"]
|===
| Message | Description

| `{"LayerNames":{"names":["base","nav","num"]}}`
| Response to `RequestLayerNames`. Contains all defined layer names.

| `{"FakeKeyNames":{"names":["email-sig","nav-mode"]}}`
| Response to `RequestFakeKeyNames`. Contains all defined virtual key names.

| `{"CurrentLayerName":{"name":"base"}}`
| Response to `RequestCurrentLayerName`. Contains the active layer name.

| `{"CurrentLayerInfo":{"name":"base","cfg_text":"..."}}`
| Response to `RequestCurrentLayerInfo`. Contains the layer name and its full configuration text.

| `{"HelloOk":{"version":"1.11.0","protocol":1,"capabilities":[...]}}`
| Response to `Hello`. Contains server version, protocol version, and supported capabilities.

| `{"ReloadResult":{"ok":true}}`
| Response to reload commands when `wait` was `true`. Indicates whether the config reload succeeded. If timed out, includes `timeout_ms`.
|===

For a complete implementation example, see the
https://github.com/jtroo/kanata/blob/main/example_tcp_client/src/main.rs[example TCP client].

[[args-quiet]]
=== Disable logs other than errors: `-q`, `--quiet`

Silence info and warning logs so that only errors are logged.
This might be desirable when running in the background as a system service.

[[args-debug]]
=== Enable debug logs: `-d`, `--debug`

Add extra debug logging.
This is helpful when diagnosing an issue
or discovering key names.

[[args-trace]]
=== Enable trace logs: `-t`, `--trace`

Add extra trace logging.
Also includes debug logging.
This is sometimes helpful when diagnosing an issue,
but is very verbose.

[[args-nodelay]]
=== Remove startup delay: `-n`, `--nodelay`

By default, Kanata adds a delay before reading keyboard inputs.
This helps protect against stale states
when started from a terminal.

However, this may be undesirable
when running as a background service.

[[args-check]]
=== Only check configuration: `--check`

Check the configuration file validity and then exit.

[[args-log-layer-changes]]
=== Force log changes: `--log-layer-changes`

Your final desired `defcfg` could be to have logging of layer changes as false.
However, for testing purposes you may want to temporarily log the layers.
This configuration forces logging to happen for this use case.

[[args-no-wait]]
=== Skip exit prompt: `--no-wait`

By default, kanata displays "Press enter to exit" and waits for user input
before exiting. This flag skips that prompt and exits immediately.

This is useful when running kanata as a background service (e.g., systemd)
where you want automatic restart on failure. Without this flag,
the service would hang waiting for stdin input that never comes.

[[args-linux-dev-symlink]]
=== Linux only - Device symlink path: `-s`, `--symlink-path`

If you want another program to consume the Kanata device output,
you can use this flag to have a consistent device filesystem path.

[[args-wait-device]]
=== Linux only - Wait before device grab attempt: `-w`, `--wait-device-ms`

Some devices take a while to become ready and can fail to be grabbed.
This configuration adds a delay before trying to grab devices
in case this is an issue impacting you.

[[args-macos-list-devices]]
=== macOS only - Only list keyboards: `-l`, `--list`

List keyboard names that can be used
within defcfg and then exit.

== Advanced features[[advanced-features]]
[[virtual-keys]]
=== Virtual keys

You can define up to 767 virtual keys.
These keys are not directly mapped to any physical key presses or releases.
Virtual keys can be activated via special actions:

* `(on-press    <action> <virtual key name>)` or `on↓`:
Activate a virtual key action when pressing the associated input key.
* `(on-release  <action> <virtual key name>)` or `on↑`:
Activate a virtual key action when releasing the associated input key.
* `(on-idle <idle time> <action> <virtual key name>)`:
Activate a virtual key action when kanata has been idle
for at least `idle time` milliseconds.
* `(on-physical-idle <idle time> <action> <virtual key name>)`:
Activate a virtual key action when the physical keyboard has
had all keys released
for at least `idle time` milliseconds.
* `(hold-for-duration <hold time> <virtual key name>`):
Press a virtual key for `hold time` milliseconds.
If `hold-for-duration` retriggered on a virtual key before release,
the time will be reset with no additional press/release events.

The `<action>` parameter can be one of:

* `tap-virtualkey     | tap-vkey`:
Press and release the virtual key. If the key is already pressed, this only releases it.
* `press-virtualkey   | press-vkey`:
Press the virtual key. It will not be released until another action triggers a release or tap.
If the key is already pressed, this does nothing.
* `release-virtualkey | release-vkey`:
Release the virtual key. If it is not already pressed, this does nothing.
* `toggle-virtualkey  | toggle-vkey`:
Press the virtual key if it is not already pressed, otherwise release it.

A virtual key can be defined in a `defvirtualkeys` configuration entry.
Configuring this entry is similar to `+defalias+`,
but you cannot make use of aliases inside to shorten an action.
You can refer to previously defined virtual keys.

Expanding on the `on-idle` action some more,
the wording that "kanata" has been idle is important.
Even if the keyboard is idle, kanata may not yet be idle.
For example, if a long-running macro is playing,
or kanata is waiting for the timeout of actions such as `caps-word` or `tap-dance`,
kanata is not yet idle, and the tick count for the `<idle time>` parameter
will not yet be counting even if you no longer have any keyboard keys pressed.
The variant `on-physical-idle` may be more desirable if you want the timer
to only start based on having all keyboard keys be released.

.Example:
[source]
----
(defvirtualkeys
  ;; Define some virtual keys that perform modifier actions
  ctl lctl
  sft lsft
  met lmet
  alt lalt

  ;; A virtual key that toggles all modifier virtual keys above
  tal (multi
        (on-press toggle-virtualkey ctl)
        (on-press toggle-virtualkey sft)
        (on-press toggle-virtualkey met)
        (on-press toggle-virtualkey alt)
      )

  ;; Virtual key that activates a macro
  vkmacro (macro h e l l o spc w o r l d)
)

(defalias
  psf (on-press press-virtualkey   sft)
  rsf (on-press release-virtualkey sft)

  tal (on-press tap-vkey tal)
  mac (on-press tap-vkey vkmacro)

  isf (on-idle 1000 tap-vkey sft)
  hfd (hold-for-duration 1000 met)
)

(deflayer use-virtual-keys
  @psf @rsf @tal @mac a s d f @isf @hfd
)
----

.Older fake keys documentation
[%collapsible]
====
The older configuration style of fake keys are still supported
but the new style is preferred due to (hopefully) clearer naming.

Fake keys can be defined inside of `deffakekeys`.

The actions are:

* `+(on-press-fakekey <fake key name> <action>)+` or `on↓fakekey`: Activate a fake key
  action when pressing the key mapped to this action.
* `+(on-release-fakekey <fake key name> <action>)+` or `on↑fakekey`: Activate a fake key
  action when releasing the key mapped to this action.
* `+(on-idle-fakekey <fake key name> <action> <idle time>)+`:
  Activate a fake key action when kanata has been idle
  for at least `idle time` milliseconds.

The aforementioned `+<key action>+` can be one of four 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.
* `+toggle+`: Press the fake key if not already pressed, otherwise release it.

.Example:
[source]
----
(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)

  isf (on-idle-fakekey sft tap 1000)
)

(deflayer use-virtual-keys
  @psf @rsf @pal @ral a s d f @isf
)
----

====

For more context, you can read the
https://github.com/jtroo/kanata/issues/80[issue that sparked the creation of virtual keys].

Something notable about virtual keys is that they don't always interrupt the state
of an active `+tap-dance-eager+`. If a `macro` action is assigned to a virtual
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.

[[sequences]]
=== Sequences

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
(<<sequence-input-mode,by default>>)
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 `+defvirtualkeys+`. The first parameter of a
pair must be a defined virtual key name. The second parameter is a list of keys
that will activate a virtual key tap when typed in the defined order. More
precisely, the action triggered is:

`+(on-press tap-vkey <virtual key name>)+`

.Example:
[source]
----
(defseq git-status (g s t))
(defvirtualkeys git-status (macro g i t spc s t a t u s))
(defalias rcl (tap-hold-release 200 200 sldr rctl))

(defseq
    dotcom (. S-3)
    dotorg (. S-4)

    ;;     The shifted letters in parentheses means a single press of lsft
    ;;     must remain held while both h and then s are pressed.
    ;;     This is not the same as S-h S-s, which means that the lsft key
    ;;     must be released and repressed between the h and s presses.
    https (S-(h s))
)
(defvirtualkeys
    dotcom (macro . c o m)
    dotorg (macro . o r g)
    https (macro h t t p s S-; / /)
)
----

There are 10 special keys with names `nop0-nop9` which kanata treats specially.
Kanata will never send OS events for these keys
but they can still participate in sequences.

See an example of using the nop keys
alongside templates to define sequences below.

.Example:
[source]
----
(defsrc f7   f8   f9   f10)
(deflayer base
        sldr nop0 nop1 nop2)
(deftemplate seq (vk-name input-keys output-action)
    (defvirtualkeys $vk-name $output-action)
    (defseq $vk-name $input-keys)
)
;; template-expand has a shortened form: t!
(t! seq dotcom (nop0 nop1) (macro . c o m))
(t! seq dotorg (nop0 nop2) (macro . o r g))
----

If 10 special nop keys do not seem sufficient,
you can get creative with your sequences and treat some as a prefix modifier.
For example, you can get 24 "keys" by treating `nop0-nop5` as normal
while treating `nop6-nop9` as prefixes that are always followed by a second nop key.

.Example:
[source]
----
(defalias
  nop0 nop0
  ;; ...
  nop5 nop5
  nop6 (macro nop6 nop0)
  ;; ...
  nop11 (macro nop6 nop5)
  ;; ...
  nop18 (macro nop9 nop0)
  ;; ...
  nop23 (macro nop9 nop5)
)
----

==== Overlapping keys in any order

Within the key list of `defseq` configuration items,
the special `O-` list prefix can be used to denote a set of keys that must
all be pressed before any are released in order to match the sequence.

For an example, `O-(a b c)` is equivalent to `O-(c b a)`.

.Example:
[source]
----
(defvirtualkey hello (macro h (unshift e l) 5 (unshift l o)))
(defseq hello (O-(h l o)))
----

WARNING: The way that sequences implements this functionality behind the scenes
is by generating a sequence for every permutation of the overlapping keys.
This can make kanata use up a lot of memory.
Due to this, the maximum keys allowed in a given `O-(...)` list is 6,
but you are still permitted to add more to the sequence,
including more `O-(...)` lists.
Doing the above can balloon kanata's memory consumption.

.Sample of more advanced usage
[%collapsible]
====

The configuration below showcases context-dependent chording
with auto-space and auto-deleted spaces from typing punctuation.

For example, chording `(d a y)` and then `(t u e)` will output
`Tuesday`, while chording `(t u e)` by itself does nothing.

.Example configuration:
[source]
----
(defsrc f1)
(deflayer base lrld)
(defcfg process-unmapped-keys yes
	sequence-input-mode visible-backspaced
	concurrent-tap-hold true)
(deftemplate seq (vk-name in out)
	(defvirtualkeys $vk-name $out)
	(defseq $vk-name $in))

(defvirtualkeys rls-sft (multi (release-key lsft)(release-key rsft)))
(defvar rls-sft (on-press tap-vkey rls-sft))
(deftemplate rls-sft () $rls-sft 5)

(defchordsv2
	(d a y) (macro sldr d (t! rls-sft) a y spc nop0) 200 first-release ()
	(h l o) (macro h (t! rls-sft) e l l o sldr spc nop0) 200 first-release ()
)
(t! seq Monday (d a y spc nop0 O-(m o n)) (macro S-m $rls-sft o n d a y nop9 sldr spc nop0))
(t! seq Tuesday (d a y spc nop0 O-(t u e)) (macro S-t $rls-sft u e s d a y nop9 sldr spc nop0))
(t! seq DelSpace_. (spc nop0 .) (macro .))
(t! seq DelSpace_; (spc nop0 ;) (macro ;))
----

.Try using the above configuration to type the text:
[source]
----
day;
Day;
Tuesday.
day hello
hello day
Hello day.
hello Tuesday
Hello Monday;
----

====

==== Override the global timeout and input mode

An alternative to using `sldr` is the `sequence` action.
The syntax is `(sequence <timeout>)`.
This enters sequence mode with a sequence timeout
different from the globally configured one.

The `sequence` action can also be called with a second parameter.
The second parameter is an override for `sequence-input-mode`:

----
(sequence <timeout> <input-mode>)
----


.Example:
[source]
----
;; Enter sequence mode and input . with a timeout of 250
(defalias dot-sequence (macro (sequence 250) 10 .))

;; Enter sequence mode and input . with a timeout of 250 and using hidden-delay-type
(defalias dot-sequence (macro (sequence 250 hidden-delay-type) 10 .))
----

==== sequence-noerase

When you have a keyboard locale that uses dead keys,
you may be pressing two keys that only actually output one symbol.
By default, when the `visible-backspaced` input mode does the backtracking backspaces,
it backspaces according to input count.
With dead keys, this may result in too many backspaces.

The `sequence-noerase` action is a no-output action
that tells the sequences action to have one fewer backspace
when backtracking with visible-backspaced.

.Example:
[source]
----
(deflayermap (base)
  0 sldr
  u (t! maybe-noerase u)
)
(deftemplate maybe-noerase (char)
  (multi
    (switch
      ((key-history ' 1)) (sequence-noerase 1) fallthrough
      () $char break
   ))
)
(defvirtualkeys seq-output-1 (macro a b c d e f g))
(defseq seq-output-1 (' u))

----

==== More about sequences

For more context about sequences, you can read the
https://github.com/jtroo/kanata/issues/97[design and motivation of sequences].
You may also be interested in
https://github.com/jtroo/kanata/blob/main/docs/sequence-adding-chords-ideas.md[the document describing chords in sequences]
to read about how chords in sequences behave.

[[input-chords]]
=== Input chords

Not to be confused with <<output-chords-combos,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.

[source]
----
(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.

**Release behaviour**

For single key actions and output chords — like `lctl` or `S-tab` —
and for `layer-while-held`,
an input chord will release the action only when all keys that are part of
the input chord have been released.
In other words, if even one key is held for the input chord
then the output action will be continued to be held,
but only for the mentioned action categories.
The behaviour also applies to the actions mentioned above
when used inside of `multi` but not within any other action.

An exception to the behaviour described above
for the action categories that would normally apply
is if a chord decomposition occurs.
A chord decomposition occurs when you input a chord
that does not correspond to any action.
When this happens, kanata splits up the key presses to activate
other actions from the components of the input chord.
In this scenario, the behaviour described in the next paragraph will occur.

For chord decompositions and all other action categories,
the release behaviour is more confusing:
the output action will end when any key is released during the timeout,
or if the timeout expires, the output action ends when the *first* key
that was pressed in the chord gets released.
This inconsistency is a limitation of the current implementation.
In these scenarios it is recommended
to hold down all keys if you want to keep holding
and to release all keys if you want to do a release.
This is because it will probably be difficult
to know which key was pressed first.

If you want to bypass the behaviour of keys being held for chord outputs,
you could change the chord output actions to be <<macro,macros>> instead.
Using a macro will guarantee a rapid press+release for the output keys.

[[defaliasenvcond]]
=== defaliasenvcond

NOTE: this configuration item is older and instead you may want to use
the newer and more generalized <<environment>> configuration.

There is a variant of `defalias`: `defaliasenvcond`.
This variant is parsed similarly,
but there must be an extra list parameter
that comes before all of the name-action pairs.

The list must contain two strings.
In order, these strings are:
an environment variable name,
and the environment variable value.
When the environment variable defined by the name
has the corresponding value when starting kanata,
the aliases within will be active.
Otherwise, the aliases will be skipped.

A use case for `defaliasenvcond` is when one has multiple devices
which vary in layout of keys,
e.g. different special keys on the bottom row.
Using environment variables, one can use the same kanata
configuration across those multiple devices
while changing key behaviours to keep consistent behaviour
of specific key positions across the multiple devices,
when the hardware keys at those physical key positions are not
the same.


.Example:
[source]
----
(defaliasenvcond (LAPTOP lp1)
  met @lp1met
)

(defaliasenvcond (LAPTOP lp2)
  met @lp2met
)
----

.Set environment variables in the current terminal process:
[source]
----
# powershell
$env:VAR_NAME = "var_value"

# bash
VAR_NAME=var_value
----

[[templates]]
=== Templates

The top-level configuration item `deftemplate`
declares a template that can be expanded multiple times
via the list item `template-expand`.
The short form of `template-expand` is `t!`.

The parameters to `deftemplate` in order are:

* Template name
* List of template variables
* Template content (any combination of lists / strings)

Within the template content, variable names prefixed with `$`
will be substituted with the expression passed into `template-expand`.

The list item `template-expand` can be placed as a top-level list
or within another list.
Its parameters in order are:

* template name
* parameters to substitute into the template

NOTE: Template expansion happens after file includes and before any other parsing.
One consequence of this early parsing is that variables defined in `defvar`
are **not** substituted when used inside of `template-expand`.
This has consequences for condtional content, e.g. with `if-equal`.
This is discussed further in Example 5.

Example 1:

In a simple example, let's say you wanted to set a large group of keys
to do something different when you're holding alt. Yes, this could also
be handled with remapping alt to a layer shift, but there are cases where
you wouldn't want this. Rather than retyping the code with `fork` and
`unmod` (to release alt) a bunch of times, you could template it like so:
[source]
----
(deftemplate alt-fork (original-action new-action)
  (fork $original-action (multi (unmod (ralt lalt) nop0) $new-action) (lalt ralt))
)
(defsrc 1 2 3)
(defalias fn1 (template-expand alt-fork 1 f1))
;; Templates are a simple text substitution, so the above is exactly equivalent to:
;; (defalias fn1 (fork 1 (multi (unmod (ralt lalt) nop0) f1) (lalt ralt)))
(defalias fn2 (template-expand alt-fork 2 f2))
;; You can use t! as a short form of template-expand
(defalias fn3 (t! alt-fork 3 f3))
(deflayer default @fn1 @fn2 @fn3)
----

.Example 2:
[source]
----
(defvar chord-timeout 200)
(defcfg process-unmapped-keys yes)

;; This template defines a chord group and aliases that use the chord group.
;; The purpose is to easily define the same chord position behaviour
;; for multiple layers that have different underlying keys.
(deftemplate left-hand-chords (chordgroupname k1 k2 k3 k4 alias1 alias2 alias3 alias4)
  (defalias
    $alias1 (chord $chordgroupname $k1)
    $alias2 (chord $chordgroupname $k2)
    $alias3 (chord $chordgroupname $k3)
    $alias4 (chord $chordgroupname $k4)
  )
  (defchords $chordgroupname $chord-timeout
    ($k1) $k1
    ($k2) $k2
    ($k3) $k3
    ($k4) $k4
    ($k1 $k2) lctl
    ($k3 $k4) lsft
  )
)

(template-expand left-hand-chords qwerty a s d f qwa qws qwd qwf)
;; t! is short for template-expand
(t! left-hand-chords dvorak a o e u dva dvo dve dvu)

(defsrc a s d f)
(deflayer dvorak @dva @dvo @dve @dvu)
(deflayer qwerty @qwa @qws @qwd @qwf)
----

.Example 3:
[source]
----
;; This template defines a home row that customizes a single key's behaviour
(deftemplate home-row (j-behaviour)
  a s d f g h $j-behaviour k l ; '
)

(defsrc
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
       ;; usable even inside defsrc
  caps (t! home-row j)                            ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)

(deflayer base
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
                                 ;; lists can be passed in too!
  caps (t! home-row (tap-hold 200 200 j lctl))    ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)
----

==== if-equal

Within a template you can use the list item `if-equal`
to have condiditionally-used items within a template.

It accepts a minimum of 2 parameters.
The first two parameters must be strings and are compared
against each other.
If they match, the following parameters are inserted into
the template in place of the `if-equal` list.
Otherwise if the strings do not match
then the whole `if-equal` list is removed from the template.

.Example 4:
----
(deftemplate home-row (version)
  a s d f g h
  (if-equal $version v1 j)
  (if-equal $version v2 (tap-hold 200 200 j lctl))
   k l ; '
)

(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 (template-expand home-row v1)                            ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)

(deflayer base
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
  caps (template-expand home-row v2)                            ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)
----

Similar to `if-equal` are three more conditional operators for templates:

* `if-not-equal`
** the content is used if the first two string parameters are not equal
* `if-in-list`
** the content is used if the first string parameter exists in
the second list-of-strings parameter
* `if-not-in-list`
** the content is used if the first string parameter does not exist in
the second list-of-strings parameter

.Example 5:
----
;; defvar is parsed AFTER template expansion occurs.
(defvar a hello)

(deftemplate template1 (var1)
  a (if-equal hello $var1 b) c
)

;; Below will expand to: `a c` because the string
;; $a itself is compared against the string hello
;; and they are not equal.
(template-expand template1 $a)

(deftemplate template2 (var1)
  a (if-equal $a $var1 b) c
)

;; Below will expand to: `a b c` because the string
;; $a is compared against the string $a and they are equal.
;; But note that the variable $a is still not substituted
;; with its defvar value of: hello.
(template-expand template2 $a)
----

[[concat-in-deftemplate]]
==== concat in deftemplate

Like <<concat-in-defvar,concat in defvar>>,
a list beginning with `concat` within the content of `deftemplate`
will be replaced with a single string that consists of
all the subsequent items in the list concatenated to each other.

[[custom-tap-hold-behaviour]]
=== Custom tap-hold behaviour

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
https://github.com/jtroo/kanata/issues/128[motivation for custom tap-hold behaviour].


[[fancy-key-symbols]]
=== Fancy key symbols

Instead of using the same `+a-z+` letters for special keys, e.g., `+lsft+` for `+LeftShift+`
you can use much shorter, yet more visible, key symbols like `+‹⇧+`.

For more details see
https://github.com/jtroo/kanata/blob/main/docs/fancy_symbols.md[symbol list] and
https://github.com/jtroo/kanata/blob/main/cfg_samples/fancy_symbols.kbd[example config], which not only uses these symbols in layer definitions, but also repurposes `+⎇›+` and `+⇧›+` `+⎇›+` keys into "symbol" keys that allow you to insert these fancy symbols by pressing the key, e.g.,

* hold `+⎇›+` and tap `+Delete+` would insert `+␡+`

[[windows-only-work-elevated]]
=== Windows only: enable in elevated windows

The default `kanata.exe` binary doesn't work in elevated windows (run with administrative privileges),
e.g., `Control Panel`. However, you can use AutoHotkey's "EnableUIAccess" script to self-sign the binary,
move it to "Program Files", then launching kanata from there will also work in these elevated windows.
See https://github.com/jtroo/kanata/blob/main/EnableUIAccess[EnableUIAccess] folder with the script
and its required libraries (needs https://www.autohotkey.com/download/[AutoHotkey v2] installed)

If compiling yourself, you should add the feature flag `win_manifest`
to enable the use of the `EnableUIAccess` script:

```
cargo build --win_manifest
```

[[windows-only-win-tray]]
=== Windows only: win-tray

Kanata can be compiled as a Windows GUI tray app with the feature flag `gui`.
This can simplify launching the app on user login by placing a `.lnk`
at `%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup`, show custom icon indicator per config


image:https://github.com/jtroo/kanata/blob/main/docs/win-tray/win-tray-screen.png[icon indicator per config,477,129]

as well as dynamic icon indicator per layer (might need to click on the gif below to play)

image:https://github.com/jtroo/kanata/blob/main/docs/win-tray/win-tray-layer-change.gif[icon indicator per layer,33,35,opts=autoplay]

(see <<windows-only-win-tray>>). It also supports (re)loading configs.

Currently the only configuration supported is tray icon per profile, all other configuration should
be done by passing cli flags in the `Target` field of `.lnk`, e.g., `"C:\Program Files\kanata\kanata.exe" -d -n`
to launch kanata without a delay in a debug mode

When launched from a command line, the app outputs log to the console, but otherwise the logs are currently
only available via an app capable of viewing `OutputDebugString` debugs, e.g., https://github.com/smourier/TraceSpy[TraceSpy].

[[test-your-config]]
=== Test your config

Kanata has a `+kanata_simulated_input+` tool
to help test your configuration in a predictable manner.

You can try it out on https://jtroo.github.io/[GitHub pages].

Code for the CLI tool can be found under link:https://github.com/jtroo/kanata/blob/main/simulated_input/[simulated_input].

Instead of physically typing to test something
and wondering whether you didn't get the expected result because your config is wrong or
because you mistyped something,
you can write a sequence of key presses in a `+sim.txt+` file,
run the tool with your config
and get a "timeline" view of input/output events that can help understand how kanata translates
your input into various key/mouse presses.

WARNING: The format of this view may change. Emoji output may break vertical alignment.

For more details download the files below and run `kanata_simulated_input -c sim.kbd -s sim.txt` +
  - https://github.com/jtroo/kanata/blob/main/docs/simulated_output/sim.kbd[example config] with simple home row mod bindings +
  - https://github.com/jtroo/kanata/blob/main/docs/simulated_output/sim.txt[example input sequence] +
  - https://github.com/jtroo/kanata/blob/main/docs/simulated_output/sim_out.txt[example output sequence] +

Input sequence file format: whitespace insensitive list of `prefix:key` pairs where prefix is one of: +
  - `🕐`, `t`, or `tick` to add time between key events in `ms` +
  - `↓`, `d`, `down`, or `press` +
  - `↑`, `u`, `up`, or `release` +
  - `⟳`, `r`, or `repeat` +
  - `🎭`, `fakekey`, `vk`, or `virtualkey` to activate virtual keys +
  - `🔀`, `ls`, or `layer-switch` to switch layers +

Virtual key format is `vk:name[:action]`, `fakekey:name[:action]`, or `virtualkey:name[:action]`. +
When using the emoji prefix, the format is `🎭name[:action]`. Action is one of: +
  - `press` or `p` (default if omitted) +
  - `release` +
  - `tap` or `t` +
  - `toggle` or `g` +
Example: `vk:vk_bear:tap` or `🎭vk_bear:tap`. +

Layer switch format is `ls:name` or `layer-switch:name`. +
When using the emoji prefix, the format is `🔀name`. +
This switches to the specified layer as the new default layer. +
Example: `ls:nav` or `🔀nav`. +

And key names are defined in the https://github.com/jtroo/kanata/blob/main/parser/src/keys/mod.rs[str_to_oscode function],
for example, `1` for the numeric key 1 or `kp1`/`🔢₁` for the keypad numeric key 1

Using unicode symbols `🕐`,`↓`,`↑`,`⟳`,`🎭`,`🔀` allows skipping the `:` separator, e.g., `↓k` ≝ `↓:k` ≝ `d:k`

[[zippychord]]
=== Zippychord

**Reference**

You may define a single `+defzippy+` configuration item.
This configuration enables chorded text expansion.

.Configuration syntax within the kanata configuration
[source]
----
(defzippy
  $zippy-filename ;; required
  on-first-press-chord-deadline $deadline-millis  ;; optional
  idle-reactivate-time          $idle-time-millis ;; optional
  smart-space                   $smart-space-cfg  ;; optional
  smart-space-punctuation (                       ;; optional
    $punc1 $punc2 ... $puncN)
  output-character-mappings (    ;; optional
    $character1 $output-mapping1
    $character2 $output-mapping2
    ;; ...
    $characterN $output-mappingN
  )
)
----

[cols="1,4"]
|===
| `$zippy-filename`
| Relative or absolute file path.
If relative, its path is relative to
the directory containing the kanata configuration file.
This must be the first item following `defzippy`.

| `$deadline-millis`
| Number of milliseconds.
After the first press while zippy is enabled,
if no chord activates within this configured time,
zippy is temporarily disabled.

| `$idle-time-millis`
| Number of milliseconds.
After typing ends and this configured number of milliseconds elapses,
zippy will be re-enabled from being temporarily disabled.

| `$smart-space-cfg`
| Determines the smart space behaviour.
The options are `none`, `add-space-only`, and `full`.
With `none`, outputs are typed as-is.
With `add-space-only`, spaces are automatically added after outputs
which end with neither a space or a backspace ⌫.
With `full`, the `add-space-only` behaviour applies
alongside additional behaviour:
typing punctuation (default characters: `, . ;`)
after a zippy activation will delete a prior automatically-added space.

| `$punc`
| A character defined in `output-character-mappings`
or a known key name, which shall be considered as punctuation.
The `smart-space-punctuation` configuration
will overwrite the default punctuation list
considered by smart-space;
if you want to include the default characters,
you must include them in this configuration.

| `$character`
| A single unicode codepoint for use
in the output column of the zippy configuration file.

| `$output-mapping`
| Key or output chord to tell kanata how to type `$character`
when seen in the zippy file output column.
Must be a single key or output chord.
The output chord may contain `AG-` to tell kanata to press with AltGr
and may contain `S-` to tell kanata to press with Shift.

The list items `no-erase` and `single-output` are also usable in this position.

| `no-erase`
| Accepts a single key or output chord as a parameter.
The zippy system will not backspace this character
in case of auto-erasure by a superset chord or followup chord.
Use for dead keys or compose keys.

| `single-output`
| Accepts one or more keys or output chords as a parameter.
The zippy system send only one backspace
in case of auto-erasure by a superset chord or followup chord.
Use for a dead key or compose key sequence with one output symbol.
|===

Regarding output mappings,
you can configure the output of the special-lisp-syntax characters
`+) ( "+` via these lines:

[source]
----
    ")"    $right-paren-output
    "("    $left-paren-output
    r#"""# $double-quote-output
----

As an example, for the US layout these should be the correct lines:

[source]
----
    ")"     S-0
    "("     S-9
    r#"""#  S-'
----

.Configuration syntax within the zippy configuration file
[source]
----
// This is a comment.
//          inputs            ↹  outputs
$chord1 $follow-chord1.1...1.M	$output1
$chord2 $follow-chord2.1...2.M	$output2
// ...
$chordN $follow-chordN.1...N.M	$outputN
----

The format is two columns separated by a single Tab character.
The first column is input and the second is output.

[cols="1,4"]
|===
|`$chord`
| A set of characters.
You can use space by including it as the first character in the chord;
for an example see `Alphabet` in the sample below.
With 0 optional follow chords,
the corresponding output on the same line (`$output`)
will activate when zippy is enabled
and all the defined chord keys are pressed simultaneously.
The order of presses is not important.

| `$follow-chord`
| 0 or more chords, used the same way as `$chord`.
Having follow chords means the `$output` on the same line
will activate upon first activating the earlier chord(s) in the same line,
releasing all keys, and pressing the keys in `$follow-chordN.M`.
Follow chords are separated from the previous chord by a space.
If using a space in the follow chord, use two spaces;
for an example see `Washington` in the sample below.

| `$output`
| The characters to type when the chord and optional follow chord(s)
are all pressed by the user.
This is separated from the input chord column
by a single Tab character.
The characters are typed in sequence
and must all be singular-name key names
as one would configure in `defsrc`.
A capitalized single-character key name
will be parsed successfully
and these will be outputted alongside Shift to output the capitalized key.
Additionally, `output-character-mappings` configuration can be used
to inform kanata of additional mappings that may use Shift or AltGr.
|===

**Examples**

.Sample kanata configuration
[source]
----
(defzippy
  zippy.txt
  on-first-press-chord-deadline 500
  idle-reactivate-time          500
  smart-space-punctuation (? ! . , ; :)
  output-character-mappings (
    ;; This should work for US international.
    ! S-1
    ? S-/
    % S-5
    "(" S-9
    ")" S-0
    : S-;
    < S-,
    > S-.
    r#"""# S-'
    | S-\
    _ S--
    ® AG-r
    ’ (no-erase `)
    é (single-output ' e)
  )
)
----

.Sample zippy file content

[source]
----
dy	day
dy 1	Monday
dy 2	Tuesday
 abc	alphabet
 w  a	Washington
gi	git
gi f p	git fetch -p
----

**Description**

Zippychord is yet another chording mechanism in Kanata.
The inspiration behind it is primarily the
https://github.com/psoukie/zipchord[zipchord project].
The name is similar; it is named "zippy" instead of "zip" because
Kanata's implementation is not a port and does not aim for 100%
behavioural compatibility.

The intended use case is shorthands, or accelerating character output.
Within zippychord, inputs are keycode chords or sequences,
and the outputs are also purely keycodes.
In other words, all other actions are unsupported;
e.g. layers, switch, one-shot.

Zippychord behaves on outputted keycodes, i.e. the key outputs
after kanata has finished processing your
inputs, layers, switch logic and other configurations.
This is similar to how sequences operate
and is unlike chords(v1) and chordsv2.
Furthermore, outputs are all eager like `visible-backspaced` on sequences.
If a zippychord activation occurs, typed keys are backspaced.

To give an example, if one configures zippychord with a line like:

[source]
----
gi	git
----

then either of the following typing event sequences
will erase the input characters
and then proceed to type the output "git"
like if it was `(macro bspc bspc g i t)`.

[source]
----
(press g) (press i)
(press i) (press g)
----

Note that there aren't any release events listed.
To contrast, the following event sequence would not result in an activation:

[source]
----
(press g) (release g) (press i)
----

Zippychord supports fully overlapping chords and sequences.
For example, this configuration is allowed:

[source]
----
gi	git␣
gi s	git␣status
gi c	git checkout␣
gi c b	git checkout -b␣
gi c a	git commit --amend␣
gi c n	git commit --amend --no-edit
gi c a m	git commit --amend -m 'FIX_THIS_COMMIT_MESSAGE'
----

When you begin with the `(g i)` chord, you can follow up
with various character sequences to output different git commands.
This use case is quite similar to git aliases.
One advantage of zippychord is that it eagerly shows you
the true underlying command as you type.