Merge pull request #33 from KMKfw/topic-macros-have-codes

Handle macros more consistently
This commit is contained in:
Josh Klar 2018-10-01 12:02:56 -07:00 committed by GitHub
commit 5fe54f6ca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 30 deletions

View File

@ -30,6 +30,8 @@ def process_internal_key_event(state, action_type, changed_key, logger=None):
return grave_escape(state, action_type, logger=logger)
elif changed_key.code == RawKeycodes.KC_UC_MODE:
return unicode_mode(state, action_type, changed_key, logger=logger)
elif changed_key.code == RawKeycodes.KC_MACRO:
return macro(state, action_type, changed_key, logger=logger)
else:
return state
@ -111,3 +113,15 @@ def unicode_mode(state, action_type, changed_key, logger):
state.unicode_mode = changed_key.mode
return state
def macro(state, action_type, changed_key, logger):
if action_type == KEY_UP_EVENT:
if changed_key.keyup:
state.macro_pending = changed_key.keyup
return state
elif action_type == KEY_DOWN_EVENT:
if changed_key.keydown:
state.macro_pending = changed_key.keydown
return state

View File

@ -8,7 +8,6 @@ from kmk.common.event_defs import (HID_REPORT_EVENT, INIT_FIRMWARE_EVENT,
MACRO_COMPLETE_EVENT, NEW_MATRIX_EVENT)
from kmk.common.internal_keycodes import process_internal_key_event
from kmk.common.keycodes import FIRST_KMK_INTERNAL_KEYCODE, Keycodes
from kmk.common.macros import KMKMacro
class ReduxStore:
@ -147,22 +146,12 @@ def kmk_reducer(state=None, action=None, logger=None):
for changed_key in released:
if not changed_key:
continue
elif isinstance(changed_key, KMKMacro):
if changed_key.keyup:
state.macro_pending = changed_key.keyup
elif changed_key.code >= FIRST_KMK_INTERNAL_KEYCODE:
state = process_internal_key_event(state, KEY_UP_EVENT, changed_key, logger=logger)
for changed_key in pressed:
if not changed_key:
continue
elif isinstance(changed_key, KMKMacro):
if changed_key.keydown:
state.macro_pending = changed_key.keydown
elif changed_key.code >= FIRST_KMK_INTERNAL_KEYCODE:
state = process_internal_key_event(
state,

View File

@ -40,7 +40,8 @@ class RawKeycodes:
KC_UC_MODE = 1109
KC_MACRO_SLEEP_MS = 1110
KC_MACRO = 1110
KC_MACRO_SLEEP_MS = 1111
# These shouldn't have all the fancy shenanigans Keycode allows
@ -99,6 +100,23 @@ class ConsumerKeycode(Keycode):
pass
class Macro:
'''
A special "key" which triggers a macro.
'''
code = RawKeycodes.KC_MACRO
def __init__(self, keydown=None, keyup=None):
self.keydown = keydown
self.keyup = keyup
def on_keydown(self):
return self.keydown() if self.keydown else None
def on_keyup(self):
return self.keyup() if self.keyup else None
class KeycodeCategory(type):
@classmethod
def to_dict(cls):

View File

@ -1,10 +0,0 @@
class KMKMacro:
def __init__(self, keydown=None, keyup=None):
self.keydown = keydown
self.keyup = keyup
def on_keydown(self):
return self.keydown() if self.keydown else None
def on_keyup(self):
return self.keyup() if self.keyup else None

View File

@ -2,8 +2,7 @@ import string
from kmk.common.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.common.keycodes import Keycodes, RawKeycodes, char_lookup
from kmk.common.macros import KMKMacro
from kmk.common.keycodes import Keycodes, Macro, RawKeycodes, char_lookup
from kmk.common.util import sleep_ms
@ -22,7 +21,7 @@ def simple_key_sequence(seq):
yield keycode_up_event(key)
yield hid_report_event()
return KMKMacro(keydown=_simple_key_sequence)
return Macro(keydown=_simple_key_sequence)
def send_string(message):

View File

@ -1,8 +1,7 @@
from kmk.common.consts import UnicodeModes
from kmk.common.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.common.keycodes import Common, Modifiers
from kmk.common.macros import KMKMacro
from kmk.common.keycodes import Common, Macro, Modifiers
from kmk.common.macros.simple import simple_key_sequence
IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U))
@ -34,7 +33,7 @@ def unicode_sequence(codepoints):
elif state.unicode_mode == UnicodeModes.WINC:
yield from _winc_unicode_sequence(codepoints, state)
return KMKMacro(keydown=_unicode_sequence)
return Macro(keydown=_unicode_sequence)
def _ralt_unicode_sequence(codepoints, state):

View File

@ -6,7 +6,6 @@ from kmk.common.consts import HID_REPORT_STRUCTURE, HIDReportTypes
from kmk.common.event_defs import HID_REPORT_EVENT
from kmk.common.keycodes import (FIRST_KMK_INTERNAL_KEYCODE, ConsumerKeycode,
ModifierKeycode)
from kmk.common.macros import KMKMacro
def generate_pyb_hid_descriptor():
@ -72,7 +71,7 @@ class HIDHelper:
self.add_key(consumer_key)
else:
for key in state.keys_pressed:
if isinstance(key, KMKMacro) or key.code >= FIRST_KMK_INTERNAL_KEYCODE:
if key.code >= FIRST_KMK_INTERNAL_KEYCODE:
continue
if isinstance(key, ModifierKeycode):