From b494857f215fedc7509b2222d93135aeb8f78297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Pf=C3=BCtzenreuter?= Date: Thu, 25 Apr 2019 22:39:02 -0300 Subject: [PATCH 1/3] Fix bug in TO layer key handling --- kmk/handlers/layers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmk/handlers/layers.py b/kmk/handlers/layers.py index 0e448dd..d436318 100644 --- a/kmk/handlers/layers.py +++ b/kmk/handlers/layers.py @@ -77,7 +77,7 @@ def tg_pressed(key, state, *args, **kwargs): def to_pressed(key, state, *args, **kwargs): """Activates layer and deactivates all other layers""" - state.active_layers = [key.meta.kc] + state.active_layers = [key.meta.layer] state.reversed_active_layers = list(reversed(state.active_layers)) return state From 9c4a136989b8785c16b6b6b1f2dc274fdeceb844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Pf=C3=BCtzenreuter?= Date: Fri, 26 Apr 2019 11:21:12 -0300 Subject: [PATCH 2/3] Use return value of pre-press/pre-release callbacks When the return value evaluates to False, the key press or key release is ignored. This allows for implementation of elaborate keyboard behaviors. --- kmk/keys.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kmk/keys.py b/kmk/keys.py index 0272acd..6ec36f0 100644 --- a/kmk/keys.py +++ b/kmk/keys.py @@ -60,7 +60,8 @@ class Key: def _on_press(self, state, coord_int, coord_raw): for fn in self._pre_press_handlers: - fn(self, state, KC, coord_int, coord_raw) + if not fn(self, state, KC, coord_int, coord_raw): + return None ret = self._handle_press(self, state, KC, coord_int, coord_raw) @@ -71,7 +72,8 @@ class Key: def _on_release(self, state, coord_int, coord_raw): for fn in self._pre_release_handlers: - fn(self, state, KC, coord_int, coord_raw) + if not fn(self, state, KC, coord_int, coord_raw): + return None ret = self._handle_release(self, state, KC, coord_int, coord_raw) @@ -110,8 +112,9 @@ class Key: provided for consistency with the internal handlers) - coord_raw (an X,Y tuple of the matrix coordinate - also likely not useful) - The return value of the provided callback is discarded. Exceptions are _not_ - caught, and will likely crash KMK if not handled within your function. + If return value of the provided callback is evaluated to False, press + processing is cancelled. Exceptions are _not_ caught, and will likely + crash KMK if not handled within your function. These handlers are run in attachment order: handlers provided by earlier calls of this method will be executed before those provided by later calls. @@ -156,8 +159,9 @@ class Key: provided for consistency with the internal handlers) - coord_raw (an X,Y tuple of the matrix coordinate - also likely not useful) - The return value of the provided callback is discarded. Exceptions are _not_ - caught, and will likely crash KMK if not handled within your function. + If return value of the provided callback evaluates to False, the release + processing is cancelled. Exceptions are _not_ caught, and will likely crash + KMK if not handled within your function. These handlers are run in attachment order: handlers provided by earlier calls of this method will be executed before those provided by later calls. From f9b56d82f708d7976da417a2f3ab9c985c81661f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Pf=C3=BCtzenreuter?= Date: Fri, 26 Apr 2019 18:54:08 -0300 Subject: [PATCH 3/3] Add cancel_timeout method This method is useful when the keymap needs to use state.set_timeout() and this timeout needs to be cancellable. Also, the set_timeout() now returns a timer handle. --- kmk/internal_state.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kmk/internal_state.py b/kmk/internal_state.py index b27454b..e552c01 100644 --- a/kmk/internal_state.py +++ b/kmk/internal_state.py @@ -67,8 +67,15 @@ class InternalState: else: timeout_key = ticks_ms() + after_ticks + while timeout_key in self.timeouts: + timeout_key += 1 + self.timeouts[timeout_key] = callback - return self + return timeout_key + + def cancel_timeout(self, timeout_key): + if timeout_key in self.timeouts: + del self.timeouts[timeout_key] def process_timeouts(self): if not self.timeouts: