Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor key types #988

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions docs/en/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +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 attached modifiers (to implement things like shifted keys or `KC.HYPR`,
which are single key presses sending along more than one key in a single HID
report. For almost all purposes outside of KMK core,
this field should be ignored - it can be safely populated through far more
sane means than futzing with it by hand.
- 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
29 changes: 14 additions & 15 deletions kmk/extensions/media_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from kmk.extensions import Extension
from kmk.keys import make_consumer_key
from kmk.keys import ConsumerKey, make_key


class MediaKeys(Extension):
Expand All @@ -14,20 +14,19 @@ def __init__(self):
# support PC media keys, so I don't know how much value we would get out of
# adding the old Apple-specific consumer codes, but again, PRs welcome if the
# lack of them impacts you.
make_consumer_key(code=226, names=('AUDIO_MUTE', 'MUTE')) # 0xE2
make_consumer_key(code=233, names=('AUDIO_VOL_UP', 'VOLU')) # 0xE9
make_consumer_key(code=234, names=('AUDIO_VOL_DOWN', 'VOLD')) # 0xEA
make_consumer_key(code=111, names=('BRIGHTNESS_UP', 'BRIU')) # 0x6F
make_consumer_key(code=112, names=('BRIGHTNESS_DOWN', 'BRID')) # 0x70
make_consumer_key(code=181, names=('MEDIA_NEXT_TRACK', 'MNXT')) # 0xB5
make_consumer_key(code=182, names=('MEDIA_PREV_TRACK', 'MPRV')) # 0xB6
make_consumer_key(code=183, names=('MEDIA_STOP', 'MSTP')) # 0xB7
make_consumer_key(
code=205, names=('MEDIA_PLAY_PAUSE', 'MPLY')
) # 0xCD (this may not be right)
make_consumer_key(code=184, names=('MEDIA_EJECT', 'EJCT')) # 0xB8
make_consumer_key(code=179, names=('MEDIA_FAST_FORWARD', 'MFFD')) # 0xB3
make_consumer_key(code=180, names=('MEDIA_REWIND', 'MRWD')) # 0xB4

make_key(code=0xE2, names=('AUDIO_MUTE', 'MUTE'), key_type=ConsumerKey)
make_key(code=0xE9, names=('AUDIO_VOL_UP', 'VOLU'), key_type=ConsumerKey)
make_key(code=0xEA, names=('AUDIO_VOL_DOWN', 'VOLD'), key_type=ConsumerKey)
make_key(code=0x6F, names=('BRIGHTNESS_UP', 'BRIU'), key_type=ConsumerKey)
make_key(code=0x70, names=('BRIGHTNESS_DOWN', 'BRID'), key_type=ConsumerKey)
make_key(code=0xB5, names=('MEDIA_NEXT_TRACK', 'MNXT'), key_type=ConsumerKey)
make_key(code=0xB6, names=('MEDIA_PREV_TRACK', 'MPRV'), key_type=ConsumerKey)
make_key(code=0xB7, names=('MEDIA_STOP', 'MSTP'), key_type=ConsumerKey)
make_key(code=0xCD, names=('MEDIA_PLAY_PAUSE', 'MPLY'), key_type=ConsumerKey)
make_key(code=0xB8, names=('MEDIA_EJECT', 'EJCT'), key_type=ConsumerKey)
make_key(code=0xB3, names=('MEDIA_FAST_FORWARD', 'MFFD'), key_type=ConsumerKey)
make_key(code=0xB4, names=('MEDIA_REWIND', 'MRWD'), key_type=ConsumerKey)

def on_runtime_enable(self, sandbox):
return
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
26 changes: 6 additions & 20 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,20 +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)
if key.has_modifiers:
for mod in key.has_modifiers:
self.add_modifier(mod)

for axis in axes:
self.move_axis(axis)
Expand Down Expand Up @@ -175,23 +169,15 @@ def clear_non_modifiers(self):

def add_modifier(self, modifier):
if isinstance(modifier, ModifierKey):
if modifier.code == ModifierKey.FAKE_CODE:
for mod in modifier.has_modifiers:
self.report_mods[0] |= mod
else:
self.report_mods[0] |= modifier.code
self.report_mods[0] |= modifier.code
else:
self.report_mods[0] |= modifier

return self

def remove_modifier(self, modifier):
if isinstance(modifier, ModifierKey):
if modifier.code == ModifierKey.FAKE_CODE:
for mod in modifier.has_modifiers:
self.report_mods[0] ^= mod
else:
self.report_mods[0] ^= modifier.code
self.report_mods[0] ^= modifier.code
else:
self.report_mods[0] ^= modifier

Expand Down
Loading