|
4 | 4 | can be found in [`keycodes.md`](keycodes.md). It's probably worth a look at the raw source if
|
5 | 5 | you're stumped: [`kmk/keys.py`](/kmk/keys.py).
|
6 | 6 |
|
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 | +``` |
8 | 106 |
|
9 | 107 | ## Key Objects
|
10 | 108 |
|
|
0 commit comments