Skip to content

Commit 05c3b88

Browse files
committed
Add documentation for custom keys
1 parent a3a58ab commit 05c3b88

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

docs/en/keys.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,105 @@
44
can be found in [`keycodes.md`](keycodes.md). It's probably worth a look at the raw source if
55
you're stumped: [`kmk/keys.py`](/kmk/keys.py).
66

7-
---
7+
## Custom Keys
8+
9+
Here's a very contrived example for a custom key which limits the usage of a key
10+
to certain amount.
11+
Custom keys are, as a rule of thumb, the way to go to implement custom
12+
functionality.
13+
If the objective is to type out a sequence of keys however, or an action has to
14+
be performed asynchronously while holding a key down, then [macros](macros.md)
15+
are worth trading higher convenience for the hit in performance.
16+
17+
### Quick and Dirty
18+
19+
The base key class, of which all keys are derived, accepts custom handlers.
20+
It's "single use" and should cover most use cases and not recommended for stateful keys.
21+
Both `on_press` and `on_release` methods are optional and a custom key is
22+
allowed to do absolutely nothing.
23+
24+
```python
25+
from kmk.keys import Key
26+
27+
limit = 10
28+
29+
def limit_on_press(key, keyboard, *args):
30+
global limit
31+
if limit > 0:
32+
keyboard.add_key(KC.A)
33+
34+
def limit_on_release(key, keyboard, *args):
35+
global limit
36+
if limit > 0:
37+
keyboard.remove_key(KC.A)
38+
limit -= 1
39+
40+
KC_A10 = Key(on_press=limit_on_press, on_release=limit_on_release)
41+
```
42+
43+
### Generally Recommended
44+
45+
Reusable or stateful keys are better implemented as a custom key derived from
46+
the base class.
47+
Giving the key a custom type (i.e. name) can make it easier to spot in
48+
debug messages and opens up to possibility to react on key types in custom
49+
modules; the downside is a potential slight increased in memory consumption.
50+
All methods are technically optional, although it is recommended to implement
51+
them anyway or the default implementations of `Key` may look for handlers that
52+
don't exist.
53+
54+
```python
55+
from kmk.keys import Key
56+
57+
class LimitKey(Key):
58+
def __init__(self, key, limit):
59+
self.key = KC.A
60+
self.limit = limit
61+
62+
def on_press(self, keyboard, coord_int=None):
63+
if self.limit > 0:
64+
keyboard.add_key(self.key)
65+
66+
def on_release(self, keyboard, coord_int=None):
67+
if self.limit > 0:
68+
self.limit -= 1
69+
keyboard.remove_key(self.key)
70+
71+
KC_A10 = LimitKey(KC.A, 10)
72+
KC_B20 = LimitKey(KC.B, 20)
73+
```
74+
75+
### Unnecessary
76+
77+
For completeness sake: this is how keys can be entered into the `KC` dictionary.
78+
There's no reason to do this as it will have a negative, if probably small
79+
effect on memory usage for no actual benefit.
80+
81+
```python
82+
from kmk.keys import make_key
83+
84+
# with an instance of base key class with 1 alias
85+
make_key(
86+
names=('A10',),
87+
constructor=Key,
88+
on_press=limit_on_press,
89+
on_release=limit_on_release,
90+
)
91+
92+
# with a custom base key class with 3 aliases
93+
make_key(
94+
names=('B20', 'LIMIT_B_20', 'B_ONLY_20_TIMES'),
95+
constructor=LimitKey,
96+
key=KC.B,
97+
count=20,
98+
)
99+
100+
# makes those keys available as:
101+
KC.A10
102+
KC.B20
103+
KC.LIMIT_B_20
104+
KC.B_ONLY_20_TIMES
105+
```
8106

9107
## Key Objects
10108

docs/en/macros.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Macros are used for sending multiple keystrokes in a single action, and can
44
be used for things like Unicode characters (even emojis! 🇨🇦), _Lorem ipsum_
55
generators, triggering side effects (think lighting, speakers,
66
microcontroller-optimized cryptocurrency miners, whatever).
7+
Macros have worse performance and higher memory usage than [custom keys](keys.md),
8+
so unless the objective is to type out a sequence or to perform an action repeatedly
9+
and asynchronously while a key is pressed, custom keys are the recommended solution.
710

811
## Setup
912

util/aspell.en.pws

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
personal_ws-1.1 en 355
1+
personal_ws-1.1 en 357
22
ADNS
33
AMS
44
ANAVI
@@ -333,6 +333,8 @@ rp
333333
runtime
334334
scotto
335335
splitkb
336+
stateful
337+
StickyKeys
336338
sublicensable
337339
sublicenses
338340
subrezon

0 commit comments

Comments
 (0)