Skip to content

Commit

Permalink
Add KeyboardKey class to distinguish from internal Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Jun 13, 2024
1 parent 831ff68 commit 8e9b0f0
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 200 deletions.
8 changes: 4 additions & 4 deletions docs/en/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ The next few steps are the interesting part, but to understand them, we need to
understand a bit about what a `Key` object is (found in [`kmk/keys.py`](/kmk/keys.py)). `Key`
objects have a few core pieces of information:

- Their `code`, which can be any integer. Integers below
`FIRST_KMK_INTERNAL_KEY` are sent through to the HID stack (and thus the
computer, which will translate that integer to something meaningful - for
example, `code=4` becomes `a` on a US QWERTY/Dvorak keyboard).
- Their `code`, which can be any integer or None. Integers sent through to the
HID stack (and thus the computer, which will translate that integer to
something meaningful - for example, `code=4` becomes `a` on a US QWERTY/Dvorak
keyboard).

- Handlers for "press" (sometimes known as "keydown") and "release" (sometimes
known as "keyup") events. KMK provides handlers for standard keyboard
Expand Down
13 changes: 2 additions & 11 deletions kmk/extensions/display/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from adafruit_display_text import label

from kmk.extensions import Extension
from kmk.handlers.stock import passthrough as handler_passthrough
from kmk.keys import make_key
from kmk.kmktime import PeriodicTimer, ticks_diff
from kmk.modules.split import Split, SplitSide
Expand Down Expand Up @@ -147,16 +146,8 @@ def __init__(
self.dim_period = PeriodicTimer(50)
self.split_side = None

make_key(
names=('DIS_BRI',),
on_press=self.display_brightness_increase,
on_release=handler_passthrough,
)
make_key(
names=('DIS_BRD',),
on_press=self.display_brightness_decrease,
on_release=handler_passthrough,
)
make_key(names=('DIS_BRI',), on_press=self.display_brightness_increase)
make_key(names=('DIS_BRD',), on_press=self.display_brightness_decrease)

def render(self, layer):
splash = displayio.Group()
Expand Down
13 changes: 3 additions & 10 deletions kmk/extensions/peg_rgb_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from storage import getmount

from kmk.extensions import Extension
from kmk.handlers.stock import passthrough as handler_passthrough
from kmk.keys import make_key


Expand Down Expand Up @@ -69,15 +68,9 @@ def __init__(
else:
self.ledDisplay = ledDisplay

make_key(
names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough
)
make_key(
names=('RGB_BRI',), on_press=self._rgb_bri, on_release=handler_passthrough
)
make_key(
names=('RGB_BRD',), on_press=self._rgb_brd, on_release=handler_passthrough
)
make_key(names=('RGB_TOG',), on_press=self._rgb_tog)
make_key(names=('RGB_BRI',), on_press=self._rgb_bri)
make_key(names=('RGB_BRD',), on_press=self._rgb_brd)

def _rgb_tog(self, *args, **kwargs):
if self.enable:
Expand Down
74 changes: 15 additions & 59 deletions kmk/extensions/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from math import e, exp, pi, sin

from kmk.extensions import Extension
from kmk.handlers.stock import passthrough as handler_passthrough
from kmk.keys import make_key
from kmk.scheduler import create_task
from kmk.utils import Debug, clamp
Expand Down Expand Up @@ -135,68 +134,25 @@ def __init__(

self._substep = 0

make_key(
names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough
)
make_key(
names=('RGB_HUI',), on_press=self._rgb_hui, on_release=handler_passthrough
)
make_key(
names=('RGB_HUD',), on_press=self._rgb_hud, on_release=handler_passthrough
)
make_key(
names=('RGB_SAI',), on_press=self._rgb_sai, on_release=handler_passthrough
)
make_key(
names=('RGB_SAD',), on_press=self._rgb_sad, on_release=handler_passthrough
)
make_key(
names=('RGB_VAI',), on_press=self._rgb_vai, on_release=handler_passthrough
)
make_key(
names=('RGB_VAD',), on_press=self._rgb_vad, on_release=handler_passthrough
)
make_key(
names=('RGB_ANI',), on_press=self._rgb_ani, on_release=handler_passthrough
)
make_key(
names=('RGB_AND',), on_press=self._rgb_and, on_release=handler_passthrough
)
make_key(
names=('RGB_MODE_PLAIN', 'RGB_M_P'),
on_press=self._rgb_mode_static,
on_release=handler_passthrough,
)
make_key(
names=('RGB_MODE_BREATHE', 'RGB_M_B'),
on_press=self._rgb_mode_breathe,
on_release=handler_passthrough,
)
make_key(
names=('RGB_MODE_RAINBOW', 'RGB_M_R'),
on_press=self._rgb_mode_rainbow,
on_release=handler_passthrough,
)
make_key(names=('RGB_TOG',), on_press=self._rgb_to)
make_key(names=('RGB_HUI',), on_press=self._rgb_hui)
make_key(names=('RGB_HUD',), on_press=self._rgb_hud)
make_key(names=('RGB_SAI',), on_press=self._rgb_sai)
make_key(names=('RGB_SAD',), on_press=self._rgb_sad)
make_key(names=('RGB_VAI',), on_press=self._rgb_vai)
make_key(names=('RGB_VAD',), on_press=self._rgb_vad)
make_key(names=('RGB_ANI',), on_press=self._rgb_ani)
make_key(names=('RGB_AND',), on_press=self._rgb_and)
make_key(names=('RGB_MODE_PLAIN', 'RGB_M_P'), on_press=self._rgb_mode_static)
make_key(names=('RGB_MODE_BREATHE', 'RGB_M_B'), on_press=self._rgb_mode_breathe)
make_key(names=('RGB_MODE_RAINBOW', 'RGB_M_R'), on_press=self._rgb_mode_rainbow)
make_key(
names=('RGB_MODE_BREATHE_RAINBOW', 'RGB_M_BR'),
on_press=self._rgb_mode_breathe_rainbow,
on_release=handler_passthrough,
)
make_key(
names=('RGB_MODE_SWIRL', 'RGB_M_S'),
on_press=self._rgb_mode_swirl,
on_release=handler_passthrough,
)
make_key(
names=('RGB_MODE_KNIGHT', 'RGB_M_K'),
on_press=self._rgb_mode_knight,
on_release=handler_passthrough,
)
make_key(
names=('RGB_RESET', 'RGB_RST'),
on_press=self._rgb_reset,
on_release=handler_passthrough,
)
make_key(names=('RGB_MODE_SWIRL', 'RGB_M_S'), on_press=self._rgb_mode_swirl)
make_key(names=('RGB_MODE_KNIGHT', 'RGB_M_K'), on_press=self._rgb_mode_knight)
make_key(names=('RGB_RESET', 'RGB_RST'), on_press=self._rgb_reset)

def on_runtime_enable(self, sandbox):
return
Expand Down
18 changes: 2 additions & 16 deletions kmk/handlers/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ def passthrough(key, keyboard, *args, **kwargs):
return keyboard


def default_pressed(key, keyboard, KC, coord_int=None, *args, **kwargs):
keyboard.hid_pending = True

keyboard.keys_pressed.add(key)

return keyboard


def default_released(key, keyboard, KC, coord_int=None, *args, **kwargs): # NOQA
keyboard.hid_pending = True
keyboard.keys_pressed.discard(key)

return keyboard


def reset(*args, **kwargs):
import microcontroller

Expand Down Expand Up @@ -143,4 +128,5 @@ def any_pressed(key, keyboard, *args, **kwargs):
from random import randint

key.code = randint(4, 56)
default_pressed(key, keyboard, *args, **kwargs)
keyboard.keys_pressed.add(key)
keyboard.hid_pending = True
11 changes: 4 additions & 7 deletions kmk/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from storage import getmount

from kmk.keys import FIRST_KMK_INTERNAL_KEY, ConsumerKey, ModifierKey, MouseKey
from kmk.keys import ConsumerKey, KeyboardKey, ModifierKey, MouseKey
from kmk.utils import Debug, clamp

try:
Expand Down Expand Up @@ -116,17 +116,14 @@ def create_report(self, keys_pressed, axes):
self.clear_all()

for key in keys_pressed:
if key.code >= FIRST_KMK_INTERNAL_KEY:
continue

if isinstance(key, ModifierKey):
if isinstance(key, KeyboardKey):
self.add_key(key)
elif isinstance(key, ModifierKey):
self.add_modifier(key)
elif isinstance(key, ConsumerKey):
self.add_cc(key)
elif isinstance(key, MouseKey):
self.add_pd(key)
else:
self.add_key(key)

for axis in axes:
self.move_axis(axis)
Expand Down
Loading

0 comments on commit 8e9b0f0

Please sign in to comment.