Implement oneshot key stacking
This commit is contained in:
parent
deb941b196
commit
6532497bb2
@ -1,9 +1,13 @@
|
|||||||
from kmk.keys import make_argumented_key
|
from kmk.keys import make_argumented_key
|
||||||
from kmk.modules.holdtap import ActivationType, HoldTap, HoldTapKeyMeta
|
from kmk.modules.holdtap import ActivationType, HoldTap, HoldTapKeyMeta
|
||||||
|
from kmk.utils import Debug
|
||||||
|
|
||||||
|
debug = Debug(__name__)
|
||||||
|
|
||||||
|
|
||||||
def oneshot_validator(kc, tap_time=None):
|
class OneShotKeyMeta(HoldTapKeyMeta):
|
||||||
return HoldTapKeyMeta(tap=kc, hold=kc, prefer_hold=False, tap_time=tap_time)
|
def __init__(self, kc, tap_time=None):
|
||||||
|
super().__init__(tap=kc, hold=kc, prefer_hold=False, tap_time=tap_time)
|
||||||
|
|
||||||
|
|
||||||
class OneShot(HoldTap):
|
class OneShot(HoldTap):
|
||||||
@ -12,30 +16,46 @@ class OneShot(HoldTap):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
make_argumented_key(
|
make_argumented_key(
|
||||||
validator=oneshot_validator,
|
validator=OneShotKeyMeta,
|
||||||
names=('OS', 'ONESHOT'),
|
names=('OS', 'ONESHOT'),
|
||||||
on_press=self.osk_pressed,
|
on_press=self.osk_pressed,
|
||||||
on_release=self.osk_released,
|
on_release=self.osk_released,
|
||||||
)
|
)
|
||||||
|
|
||||||
def process_key(self, keyboard, current_key, is_pressed, int_coord):
|
def process_key(self, keyboard, current_key, is_pressed, int_coord):
|
||||||
'''Release os key after interrupting keyup.'''
|
'''Release os key after interrupting non-os keyup, or reset timeout and
|
||||||
|
stack multiple os keys.'''
|
||||||
|
send_buffer = False
|
||||||
|
|
||||||
for key, state in self.key_states.items():
|
for key, state in self.key_states.items():
|
||||||
if key == current_key:
|
if key == current_key:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if isinstance(current_key.meta, OneShotKeyMeta):
|
||||||
|
keyboard.cancel_timeout(state.timeout_key)
|
||||||
|
if key.meta.tap_time is None:
|
||||||
|
tap_time = self.tap_time
|
||||||
|
else:
|
||||||
|
tap_time = key.meta.tap_time
|
||||||
|
state.timeout_key = keyboard.set_timeout(
|
||||||
|
tap_time,
|
||||||
|
lambda k=key: self.on_tap_time_expired(k, keyboard),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
if state.activated == ActivationType.PRESSED and is_pressed:
|
if state.activated == ActivationType.PRESSED and is_pressed:
|
||||||
state.activated = ActivationType.HOLD_TIMEOUT
|
state.activated = ActivationType.HOLD_TIMEOUT
|
||||||
elif state.activated == ActivationType.RELEASED and is_pressed:
|
elif state.activated == ActivationType.RELEASED and is_pressed:
|
||||||
state.activated = ActivationType.INTERRUPTED
|
state.activated = ActivationType.INTERRUPTED
|
||||||
elif state.activated == ActivationType.INTERRUPTED:
|
elif state.activated == ActivationType.INTERRUPTED:
|
||||||
if is_pressed:
|
if is_pressed:
|
||||||
keyboard.remove_key(key.meta.tap)
|
send_buffer = True
|
||||||
self.key_buffer.append((int_coord, current_key, is_pressed))
|
keyboard.set_timeout(0, lambda k=key: self.ht_released(k, keyboard))
|
||||||
keyboard.set_timeout(False, lambda: self.send_key_buffer(keyboard))
|
|
||||||
current_key = None
|
if send_buffer:
|
||||||
else:
|
self.key_buffer.append((int_coord, current_key, is_pressed))
|
||||||
self.ht_released(key, keyboard)
|
keyboard.set_timeout(0, lambda: self.send_key_buffer(keyboard))
|
||||||
|
current_key = None
|
||||||
|
|
||||||
return current_key
|
return current_key
|
||||||
|
|
||||||
@ -51,8 +71,8 @@ class OneShot(HoldTap):
|
|||||||
try:
|
try:
|
||||||
state = self.key_states[key]
|
state = self.key_states[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if keyboard.debug_enabled:
|
if debug.enabled:
|
||||||
print(f'OneShot.osk_released: no such key {key}')
|
debug(f'OneShot.osk_released: no such key {key}')
|
||||||
return keyboard
|
return keyboard
|
||||||
|
|
||||||
if state.activated == ActivationType.PRESSED:
|
if state.activated == ActivationType.PRESSED:
|
||||||
|
@ -67,7 +67,6 @@ class TestHoldTap(unittest.TestCase):
|
|||||||
[{KC.E}, {KC.D, KC.E}, {KC.E}, {}],
|
[{KC.E}, {KC.D, KC.E}, {KC.E}, {}],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
keyboard.test(
|
keyboard.test(
|
||||||
'OS hold with multiple interrupt keys',
|
'OS hold with multiple interrupt keys',
|
||||||
[
|
[
|
||||||
@ -81,3 +80,32 @@ class TestHoldTap(unittest.TestCase):
|
|||||||
],
|
],
|
||||||
[{KC.E}, {KC.D, KC.E}, {KC.E}, {KC.C, KC.E}, {KC.E}, {}],
|
[{KC.E}, {KC.D, KC.E}, {KC.E}, {KC.C, KC.E}, {KC.E}, {}],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
keyboard.test(
|
||||||
|
'OS stacking within timeout reset',
|
||||||
|
[
|
||||||
|
(4, True),
|
||||||
|
(4, False),
|
||||||
|
t_within,
|
||||||
|
(5, True),
|
||||||
|
(5, False),
|
||||||
|
t_within,
|
||||||
|
(3, True),
|
||||||
|
(3, False),
|
||||||
|
],
|
||||||
|
[{KC.E}, {KC.E, KC.F}, {KC.E, KC.F, KC.D}, {KC.E, KC.F}, {KC.F}, {}],
|
||||||
|
)
|
||||||
|
|
||||||
|
keyboard.test(
|
||||||
|
'OS stacking timed out',
|
||||||
|
[
|
||||||
|
(4, True),
|
||||||
|
(4, False),
|
||||||
|
(5, True),
|
||||||
|
(5, False),
|
||||||
|
t_after,
|
||||||
|
(3, True),
|
||||||
|
(3, False),
|
||||||
|
],
|
||||||
|
[{KC.E}, {KC.E, KC.F}, {KC.E}, {}, {KC.D}, {}],
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user