2018-09-22 08:44:30 +02:00
|
|
|
import logging
|
2018-09-21 21:57:29 +02:00
|
|
|
|
2018-09-22 08:44:30 +02:00
|
|
|
from kmk.common.event_defs import KEY_DOWN_EVENT, KEY_UP_EVENT
|
2018-10-01 05:21:42 +02:00
|
|
|
from kmk.common.keycodes import Keycodes, RawKeycodes
|
2018-09-21 21:57:29 +02:00
|
|
|
|
2018-09-22 08:44:30 +02:00
|
|
|
|
2018-09-23 06:49:58 +02:00
|
|
|
def process_internal_key_event(state, action, changed_key, logger=None):
|
2018-09-22 08:44:30 +02:00
|
|
|
if logger is None:
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-10-01 05:21:42 +02:00
|
|
|
# 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
|
|
|
|
# objects
|
|
|
|
|
|
|
|
if changed_key.code == RawKeycodes.KC_DF:
|
2018-09-23 06:49:58 +02:00
|
|
|
return df(state, action, changed_key, logger=logger)
|
2018-10-01 05:21:42 +02:00
|
|
|
elif changed_key.code == RawKeycodes.KC_MO:
|
2018-09-23 06:49:58 +02:00
|
|
|
return mo(state, action, changed_key, logger=logger)
|
2018-10-01 05:21:42 +02:00
|
|
|
elif changed_key.code == RawKeycodes.KC_TG:
|
2018-09-23 07:34:20 +02:00
|
|
|
return tg(state, action, changed_key, logger=logger)
|
2018-10-01 05:21:42 +02:00
|
|
|
elif changed_key.code == RawKeycodes.KC_TO:
|
2018-09-23 07:34:20 +02:00
|
|
|
return to(state, action, changed_key, logger=logger)
|
2018-10-01 05:21:42 +02:00
|
|
|
elif changed_key.code == Keycodes.KMK.KC_GESC.code:
|
|
|
|
return grave_escape(state, action, logger=logger)
|
|
|
|
elif changed_key.code == RawKeycodes.KC_UC_MODE:
|
|
|
|
return unicode_mode(state, action, changed_key, logger=logger)
|
2018-09-22 09:46:14 +02:00
|
|
|
else:
|
|
|
|
return state
|
2018-09-22 08:44:30 +02:00
|
|
|
|
|
|
|
|
2018-10-01 05:21:42 +02:00
|
|
|
def grave_escape(state, action, logger):
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_DOWN_EVENT:
|
2018-09-25 03:00:35 +02:00
|
|
|
for key in state.keys_pressed:
|
|
|
|
if key in {Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT}:
|
|
|
|
# if Shift is held, return KC_GRAVE which will become KC_TILDE on OS level
|
|
|
|
return state.update(
|
|
|
|
keys_pressed=(
|
|
|
|
state.keys_pressed | {Keycodes.Common.KC_GRAVE}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
elif key in {Keycodes.Modifiers.KC_LGUI, Keycodes.Modifiers.KC_RGUI}:
|
|
|
|
# if GUI is held, return KC_GRAVE
|
|
|
|
return state.update(
|
|
|
|
keys_pressed=(
|
|
|
|
state.keys_pressed | {Keycodes.Common.KC_GRAVE}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# else return KC_ESC
|
|
|
|
return state.update(
|
|
|
|
keys_pressed=(
|
|
|
|
state.keys_pressed | {Keycodes.Common.KC_ESCAPE}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2018-10-01 07:50:04 +02:00
|
|
|
elif action[0] == KEY_UP_EVENT:
|
2018-09-25 03:00:35 +02:00
|
|
|
return state.update(
|
|
|
|
keys_pressed=frozenset(
|
|
|
|
key for key in state.keys_pressed
|
|
|
|
if key not in {Keycodes.Common.KC_ESCAPE, Keycodes.Common.KC_GRAVE}
|
|
|
|
),
|
|
|
|
)
|
2018-09-23 00:29:24 +02:00
|
|
|
|
|
|
|
|
2018-09-23 06:49:58 +02:00
|
|
|
def df(state, action, changed_key, logger):
|
2018-09-22 02:22:03 +02:00
|
|
|
"""Switches the default layer"""
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_DOWN_EVENT:
|
2018-09-23 07:34:20 +02:00
|
|
|
state.active_layers[0] = changed_key.layer
|
2018-09-22 08:44:30 +02:00
|
|
|
|
|
|
|
return state
|
2018-09-22 02:22:03 +02:00
|
|
|
|
|
|
|
|
2018-09-23 06:49:58 +02:00
|
|
|
def mo(state, action, changed_key, logger):
|
2018-09-22 02:22:03 +02:00
|
|
|
"""Momentarily activates layer, switches off when you let go"""
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_UP_EVENT:
|
2018-09-22 09:46:14 +02:00
|
|
|
state.active_layers = [
|
|
|
|
layer for layer in state.active_layers
|
2018-09-23 06:49:58 +02:00
|
|
|
if layer != changed_key.layer
|
2018-09-22 09:46:14 +02:00
|
|
|
]
|
2018-10-01 07:50:04 +02:00
|
|
|
elif action[0] == KEY_DOWN_EVENT:
|
2018-09-23 06:49:58 +02:00
|
|
|
state.active_layers.append(changed_key.layer)
|
2018-09-22 08:44:30 +02:00
|
|
|
|
|
|
|
return state
|
2018-09-22 02:22:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def lm(layer, mod):
|
|
|
|
"""As MO(layer) but with mod active"""
|
|
|
|
|
|
|
|
|
|
|
|
def lt(layer, kc):
|
|
|
|
"""Momentarily activates layer if held, sends kc if tapped"""
|
|
|
|
|
|
|
|
|
2018-09-23 07:34:20 +02:00
|
|
|
def tg(state, action, changed_key, logger):
|
|
|
|
"""Toggles the layer (enables it if not active, and vise versa)"""
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_DOWN_EVENT:
|
2018-09-23 07:34:20 +02:00
|
|
|
if changed_key.layer in state.active_layers:
|
|
|
|
state.active_layers = [
|
|
|
|
layer for layer in state.active_layers
|
|
|
|
if layer != changed_key.layer
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
state.active_layers.append(changed_key.layer)
|
|
|
|
|
|
|
|
return state
|
2018-09-22 02:22:03 +02:00
|
|
|
|
|
|
|
|
2018-09-23 07:34:20 +02:00
|
|
|
def to(state, action, changed_key, logger):
|
2018-09-22 02:22:03 +02:00
|
|
|
"""Activates layer and deactivates all other layers"""
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_DOWN_EVENT:
|
2018-09-23 07:34:20 +02:00
|
|
|
state.active_layers = [changed_key.layer]
|
|
|
|
|
|
|
|
return state
|
2018-09-22 02:22:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def tt(layer):
|
|
|
|
"""Momentarily activates layer if held, toggles it if tapped repeatedly"""
|
2018-10-01 05:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def unicode_mode(state, action, changed_key, logger):
|
2018-10-01 07:50:04 +02:00
|
|
|
if action[0] == KEY_DOWN_EVENT:
|
2018-10-01 05:21:42 +02:00
|
|
|
state.unicode_mode = changed_key.mode
|
|
|
|
|
|
|
|
return state
|