make TapDance a module (#281)

* extract tapdance logic into a module

* clean out old tapdance code

* canonicalize key variable names

* split _process_tap_dance into td_pressed and td_released

* implement consistent argument order

* update documentation

* implement Module.process_key for key interception and modification

* fix tapdance realesing instead of pressing

* fix: default parameters in key handler

* cleanup holdtap

* add error handling to modules process_key

* fix: key released too late

Tapped keys didn't release on a "key released" event, but waited for a
timeout. Resulted in, for example, modifiers applying to keys after the
modifier was released.

* fix lint/formatting

* fix tap_time reference in modtap + minimal documentation

* fix lint
This commit is contained in:
xs5871
2022-01-18 05:21:05 +00:00
committed by GitHub
parent 10f8c74ad9
commit a62d39a252
9 changed files with 169 additions and 133 deletions

View File

@@ -1,6 +1,7 @@
from micropython import const
from kmk.modules import Module
from kmk.types import ModTapKeyMeta
class ActivationType:
@@ -18,6 +19,8 @@ class HoldTapKeyState:
class HoldTap(Module):
tap_time = 300
def __init__(self):
self.key_states = {}
@@ -28,8 +31,12 @@ class HoldTap(Module):
return
def after_matrix_scan(self, keyboard):
return
def process_key(self, keyboard, key, is_pressed):
'''Before other key down decide to send tap kc down.'''
if self.matrix_detected_press(keyboard):
current_key = key
if is_pressed and not isinstance(key.meta, ModTapKeyMeta):
for key, state in self.key_states.items():
if state.activated == ActivationType.NOT_ACTIVATED:
# press tap because interrupted by other key
@@ -39,7 +46,7 @@ class HoldTap(Module):
)
if keyboard.hid_pending:
keyboard._send_hid()
return
return current_key
def before_hid_send(self, keyboard):
return
@@ -53,16 +60,10 @@ class HoldTap(Module):
def on_powersave_disable(self, keyboard):
return
def matrix_detected_press(self, keyboard):
return (keyboard.matrix_update is not None and keyboard.matrix_update[2]) or (
keyboard.secondary_matrix_update is not None
and keyboard.secondary_matrix_update[2]
)
def ht_pressed(self, key, keyboard, *args, **kwargs):
'''Do nothing yet, action resolves when key is released, timer expires or other key is pressed.'''
timeout_key = keyboard.set_timeout(
keyboard.tap_time,
self.tap_time,
lambda: self.on_tap_time_expired(key, keyboard, *args, **kwargs),
)
self.key_states[key] = HoldTapKeyState(timeout_key, *args, **kwargs)