From 413e8b88068450a3f69818cd3fe20e1e6da73643 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Sat, 29 Dec 2018 15:29:11 -0800 Subject: [PATCH] Rename keycodes to keys, simpler and easier to deal with --- docs/{keycodes.md => keys.md} | 48 ++-------------------- kmk/firmware.py | 6 +-- kmk/handlers/sequences.py | 2 +- kmk/hid.py | 17 ++++---- kmk/internal_state.py | 6 +-- kmk/{keycodes.py => keys.py} | 76 +++++++++++++++++------------------ 6 files changed, 56 insertions(+), 99 deletions(-) rename docs/{keycodes.md => keys.md} (83%) rename kmk/{keycodes.py => keys.py} (92%) diff --git a/docs/keycodes.md b/docs/keys.md similarity index 83% rename from docs/keycodes.md rename to docs/keys.md index 618037e..0708371 100644 --- a/docs/keycodes.md +++ b/docs/keys.md @@ -1,6 +1,6 @@ -# Keycodes Overview +# Keys Overview -## [Basic Keycodes] +## [Basic Keys] |Key |Aliases |Description | |-----------------------|--------------------|-----------------------------------------------| @@ -103,8 +103,6 @@ |`KC.KP_0` |`KC.P0` |Keypad `0` and Insert | |`KC.KP_DOT` |`KC.PDOT` |Keypad `.` and Delete | |`KC.NONUS_BSLASH` |`KC.NUBS` |Non-US `\` and | | -|`KC.APPLICATION` |`KC.APP` |Application (Windows Menu Key) | -|`KC.POWER` | |System Power (macOS) | |`KC.KP_EQUAL` |`KC.PEQL` |Keypad `=` | |`KC.F13` | |F13 | |`KC.F14` | |F14 | @@ -118,20 +116,6 @@ |`KC.F22` | |F22 | |`KC.F23` | |F23 | |`KC.F24` | |F24 | -|`KC.EXECUTE` |`KC.EXEC` |Execute | -|`KC.HELP` | |Help | -|`KC.MENU` | |Menu | -|`KC.SELECT` |`KC.SLCT` |Select | -|`KC.STOP` | |Stop | -|`KC.AGAIN` |`KC.AGIN` |Again | -|`KC.UNDO` | |Undo | -|`KC.CUT` | |Cut | -|`KC.COPY` | |Copy | -|`KC.PASTE` |`KC.PSTE` |Paste | -|`KC.FIND` | |Find | -|`KC._MUTE` | |Mute (macOS) | -|`KC._VOLUP` | |Volume Up (macOS) | -|`KC._VOLDOWN` | |Volume Down (macOS) | |`KC.LOCKING_CAPS` |`KC.LCAP` |Locking Caps Lock | |`KC.LOCKING_NUM` |`KC.LNUM` |Locking Num Lock | |`KC.LOCKING_SCROLL` |`KC.LSCR` |Locking Scroll Lock | @@ -155,18 +139,6 @@ |`KC.LANG7` | |Language 7 | |`KC.LANG8` | |Language 8 | |`KC.LANG9` | |Language 9 | -|`KC.ALT_ERASE` |`KC.ERAS` |Alternate Erase | -|`KC.SYSREQ` | |SysReq/Attention | -|`KC.CANCEL` | |Cancel | -|`KC.CLEAR` |`KC.CLR` |Clear | -|`KC.PRIOR` | |Prior | -|`KC.RETURN` | |Return | -|`KC.SEPARATOR` | |Separator | -|`KC.OUT` | |Out | -|`KC.OPER` | |Oper | -|`KC.CLEAR_AGAIN` | |Clear/Again | -|`KC.CRSEL` | |CrSel/Props | -|`KC.EXSEL` | |ExSel | |`KC.LCTRL` |`KC.LCTL` |Left Control | |`KC.LSHIFT` |`KC.LSFT` |Left Shift | |`KC.LALT` | |Left Alt | @@ -175,9 +147,6 @@ |`KC.RSHIFT` |`KC.RSFT` |Right Shift | |`KC.RALT` | |Right Alt | |`KC.RGUI` |`KC.RCMD`, `KC.RWIN`|Right GUI (Windows/Command/Meta key) | -|`KC.SYSTEM_POWER` |`KC.PWR` |System Power Down | -|`KC.SYSTEM_SLEEP` |`KC.SLEP` |System Sleep | -|`KC.SYSTEM_WAKE` |`KC.WAKE` |System Wake | |`KC.AUDIO_MUTE` |`KC.MUTE` |Mute | |`KC.AUDIO_VOL_UP` |`KC.VOLU` |Volume Up | |`KC.AUDIO_VOL_DOWN` |`KC.VOLD` |Volume Down | @@ -185,18 +154,7 @@ |`KC.MEDIA_PREV_TRACK` |`KC.MPRV` |Previous Track (Windows) | |`KC.MEDIA_STOP` |`KC.MSTP` |Stop Track (Windows) | |`KC.MEDIA_PLAY_PAUSE` |`KC.MPLY` |Play/Pause Track | -|`KC.MEDIA_SELECT` |`KC.MSEL` |Launch Media Player (Windows) | |`KC.MEDIA_EJECT` |`KC.EJCT` |Eject (macOS) | -|`KC.MAIL` | |Launch Mail (Windows) | -|`KC.CALCULATOR` |`KC.CALC` |Launch Calculator (Windows) | -|`KC.MY_COMPUTER` |`KC.MYCM` |Launch My Computer (Windows) | -|`KC.WWW_SEARCH` |`KC.WSCH` |Browser Search (Windows) | -|`KC.WWW_HOME` |`KC.WHOM` |Browser Home (Windows) | -|`KC.WWW_BACK` |`KC.WBAK` |Browser Back (Windows) | -|`KC.WWW_FORWARD` |`KC.WFWD` |Browser Forward (Windows) | -|`KC.WWW_STOP` |`KC.WSTP` |Browser Stop (Windows) | -|`KC.WWW_REFRESH` |`KC.WREF` |Browser Refresh (Windows) | -|`KC.WWW_FAVORITES` |`KC.WFAV` |Browser Favorites (Windows) | |`KC.MEDIA_FAST_FORWARD`|`KC.MFFD` |Next Track (macOS) | |`KC.MEDIA_REWIND` |`KC.MRWD` |Previous Track (macOS) | @@ -228,7 +186,7 @@ |`KC.QUESTION` |`KC.QUES` |`?` | -## [Internal Keycodes] +## [Internal Keys] |Key |Description | |-----------------------|---------------------------------------------------------------------| diff --git a/kmk/firmware.py b/kmk/firmware.py index 2e2714b..e916de9 100644 --- a/kmk/firmware.py +++ b/kmk/firmware.py @@ -22,12 +22,12 @@ import kmk.kmktime # isort:skip import kmk.types # isort:skip import kmk.util # isort:skip -# Now handlers that will be used in keycodes later +# Now handlers that will be used in keys later import kmk.handlers.layers import kmk.handlers.stock # Now stuff that depends on the above (and so on) -import kmk.keycodes # isort:skip +import kmk.keys # isort:skip import kmk.matrix # isort:skip import kmk.hid # isort:skip @@ -46,7 +46,7 @@ import supervisor from kmk.consts import LeaderMode, UnicodeMode from kmk.hid import USB_HID from kmk.internal_state import InternalState -from kmk.keycodes import KC +from kmk.keys import KC from kmk.matrix import MatrixScanner diff --git a/kmk/handlers/sequences.py b/kmk/handlers/sequences.py index 933b7a2..bbb57ad 100644 --- a/kmk/handlers/sequences.py +++ b/kmk/handlers/sequences.py @@ -1,5 +1,5 @@ from kmk.consts import UnicodeMode -from kmk.keycodes import KC, make_key +from kmk.keys import KC, make_key from kmk.types import AttrDict, KeySequenceMeta from kmk.util import get_wide_ordinal diff --git a/kmk/hid.py b/kmk/hid.py index 4be6ec6..41c5b1b 100644 --- a/kmk/hid.py +++ b/kmk/hid.py @@ -1,6 +1,5 @@ from kmk.consts import HID_REPORT_SIZES, HIDReportTypes, HIDUsage, HIDUsagePage -from kmk.keycodes import (FIRST_KMK_INTERNAL_KEYCODE, ConsumerKeycode, - ModifierKeycode) +from kmk.keys import FIRST_KMK_INTERNAL_KEY, ConsumerKey, ModifierKey class USB_HID: @@ -30,7 +29,7 @@ class USB_HID: consumer_key = None for key in keys_pressed: - if isinstance(key, ConsumerKeycode): + if isinstance(key, ConsumerKey): consumer_key = key break @@ -53,10 +52,10 @@ class USB_HID: self.add_key(consumer_key) else: for key in keys_pressed: - if key.code >= FIRST_KMK_INTERNAL_KEYCODE: + if key.code >= FIRST_KMK_INTERNAL_KEY: continue - if isinstance(key, ModifierKeycode): + if isinstance(key, ModifierKey): self.add_modifier(key) else: self.add_key(key) @@ -93,8 +92,8 @@ class USB_HID: return self def add_modifier(self, modifier): - if isinstance(modifier, ModifierKeycode): - if modifier.code == ModifierKeycode.FAKE_CODE: + if isinstance(modifier, ModifierKey): + if modifier.code == ModifierKey.FAKE_CODE: for mod in modifier.has_modifiers: self.report_mods[0] |= mod else: @@ -105,8 +104,8 @@ class USB_HID: return self def remove_modifier(self, modifier): - if isinstance(modifier, ModifierKeycode): - if modifier.code == ModifierKeycode.FAKE_CODE: + if isinstance(modifier, ModifierKey): + if modifier.code == ModifierKey.FAKE_CODE: for mod in modifier.has_modifiers: self.report_mods[0] ^= mod else: diff --git a/kmk/internal_state.py b/kmk/internal_state.py index 1cf40d6..f264b06 100644 --- a/kmk/internal_state.py +++ b/kmk/internal_state.py @@ -1,5 +1,5 @@ from kmk.consts import LeaderMode -from kmk.keycodes import KC +from kmk.keys import KC from kmk.kmktime import ticks_ms from kmk.types import TapDanceKeyMeta from kmk.util import intify_coordinate @@ -140,7 +140,7 @@ class InternalState: def _process_internal_key_event(self, changed_key, is_pressed): # Since the key objects can be chained into new objects # with, for example, no_press set, always check against - # the underlying code rather than comparing Keycode + # the underlying code rather than comparing Key # objects return self.internal_key_handlers[changed_key.code]( @@ -150,7 +150,7 @@ class InternalState: def _process_tap_dance(self, changed_key, is_pressed): if is_pressed: if not isinstance(changed_key.meta, TapDanceKeyMeta): - # If we get here, changed_key is not a TapDanceKeycode and thus + # If we get here, changed_key is not a TapDanceKey and thus # the user kept typing elsewhere (presumably). End ALL of the # currently outstanding tap dance runs. for k, v in self.tap_dance_counts.items(): diff --git a/kmk/keycodes.py b/kmk/keys.py similarity index 92% rename from kmk/keycodes.py rename to kmk/keys.py index cda002b..b226f38 100644 --- a/kmk/keycodes.py +++ b/kmk/keys.py @@ -6,19 +6,19 @@ from kmk.consts import UnicodeMode from kmk.types import (AttrDict, KeySeqSleepMeta, LayerKeyMeta, TapDanceKeyMeta, UnicodeModeKeyMeta) -FIRST_KMK_INTERNAL_KEYCODE = 1000 -NEXT_AVAILABLE_KEYCODE = 1000 +FIRST_KMK_INTERNAL_KEY = 1000 +NEXT_AVAILABLE_KEY = 1000 -KEYCODE_SIMPLE = 0 -KEYCODE_MODIFIER = 1 -KEYCODE_CONSUMER = 2 +KEY_SIMPLE = 0 +KEY_MODIFIER = 1 +KEY_CONSUMER = 2 # Global state, will be filled in througout this file, and # anywhere the user creates custom keys KC = AttrDict() -class Keycode: +class Key: def __init__( self, code, @@ -43,7 +43,7 @@ class Keycode: if no_press is None and no_release is None: return self - return Keycode( + return Key( code=self.code, has_modifiers=self.has_modifiers, no_press=no_press, @@ -51,7 +51,7 @@ class Keycode: ) def __repr__(self): - return 'Keycode(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) + return 'Key(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) def on_press(self, state, coord_int, coord_raw): return self._on_press(self, state, KC, coord_int, coord_raw) @@ -60,7 +60,7 @@ class Keycode: return self._on_release(self, state, KC, coord_int, coord_raw) -class ModifierKeycode(Keycode): +class ModifierKey(Key): # FIXME this is atrocious to read. Please, please, please, strike down upon # this with great vengeance and furious anger. @@ -71,21 +71,21 @@ class ModifierKeycode(Keycode): return self if modified_code is not None: - if isinstance(modified_code, ModifierKeycode): - new_keycode = ModifierKeycode( - ModifierKeycode.FAKE_CODE, + if isinstance(modified_code, ModifierKey): + new_keycode = ModifierKey( + ModifierKey.FAKE_CODE, set() if self.has_modifiers is None else self.has_modifiers, no_press=no_press, no_release=no_release, ) - if self.code != ModifierKeycode.FAKE_CODE: + if self.code != ModifierKey.FAKE_CODE: new_keycode.has_modifiers.add(self.code) - if modified_code.code != ModifierKeycode.FAKE_CODE: + if modified_code.code != ModifierKey.FAKE_CODE: new_keycode.has_modifiers.add(modified_code.code) else: - new_keycode = Keycode( + new_keycode = Key( modified_code.code, {self.code}, no_press=no_press, @@ -95,7 +95,7 @@ class ModifierKeycode(Keycode): if modified_code.has_modifiers: new_keycode.has_modifiers |= modified_code.has_modifiers else: - new_keycode = Keycode( + new_keycode = Key( self.code, no_press=no_press, no_release=no_release, @@ -104,10 +104,10 @@ class ModifierKeycode(Keycode): return new_keycode def __repr__(self): - return 'ModifierKeycode(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) + return 'ModifierKey(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) -class ConsumerKeycode(Keycode): +class ConsumerKey(Key): pass @@ -135,7 +135,7 @@ def register_key_names(key, names=tuple()): # NOQA def make_key( code=None, names=tuple(), # NOQA - type=KEYCODE_SIMPLE, + type=KEY_SIMPLE, **kwargs, ): ''' @@ -147,28 +147,28 @@ def make_key( See register_key_names() for details on the assignment. - All **kwargs are passed to the Keycode constructor + All **kwargs are passed to the Key constructor ''' - global NEXT_AVAILABLE_KEYCODE + global NEXT_AVAILABLE_KEY - if type == KEYCODE_SIMPLE: - constructor = Keycode - elif type == KEYCODE_MODIFIER: - constructor = ModifierKeycode - elif type == KEYCODE_CONSUMER: - constructor = ConsumerKeycode + if type == KEY_SIMPLE: + constructor = Key + elif type == KEY_MODIFIER: + constructor = ModifierKey + elif type == KEY_CONSUMER: + constructor = ConsumerKey else: raise ValueError('Unrecognized key type') if code is None: - code = NEXT_AVAILABLE_KEYCODE - NEXT_AVAILABLE_KEYCODE += 1 - elif code >= FIRST_KMK_INTERNAL_KEYCODE: + code = NEXT_AVAILABLE_KEY + NEXT_AVAILABLE_KEY += 1 + elif code >= FIRST_KMK_INTERNAL_KEY: # Try to ensure future auto-generated internal keycodes won't # be overridden by continuing to +1 the sequence from the provided # code - NEXT_AVAILABLE_KEYCODE = max(NEXT_AVAILABLE_KEYCODE, code + 1) + NEXT_AVAILABLE_KEY = max(NEXT_AVAILABLE_KEY, code + 1) key = constructor(code=code, **kwargs) @@ -178,7 +178,7 @@ def make_key( def make_mod_key(*args, **kwargs): - return make_key(*args, **kwargs, type=KEYCODE_MODIFIER) + return make_key(*args, **kwargs, type=KEY_MODIFIER) def make_shifted_key(target_name, names=tuple()): # NOQA @@ -190,7 +190,7 @@ def make_shifted_key(target_name, names=tuple()): # NOQA def make_consumer_key(*args, **kwargs): - return make_key(*args, **kwargs, type=KEYCODE_CONSUMER) + return make_key(*args, **kwargs, type=KEY_CONSUMER) # Argumented keys are implicitly internal, so auto-gen of code @@ -201,22 +201,22 @@ def make_argumented_key( *constructor_args, **constructor_kwargs, ): - global NEXT_AVAILABLE_KEYCODE + global NEXT_AVAILABLE_KEY def _argumented_key(*user_args, **user_kwargs): - global NEXT_AVAILABLE_KEYCODE + global NEXT_AVAILABLE_KEY meta = validator(*user_args, **user_kwargs) if meta: - key = Keycode( - NEXT_AVAILABLE_KEYCODE, + key = Key( + NEXT_AVAILABLE_KEY, meta=meta, *constructor_args, **constructor_kwargs, ) - NEXT_AVAILABLE_KEYCODE += 1 + NEXT_AVAILABLE_KEY += 1 return key