diff --git a/action/action_type.py b/action/action_type.py index 5fedb74..c04c4cc 100644 --- a/action/action_type.py +++ b/action/action_type.py @@ -6,3 +6,4 @@ class ActionType(Enum): ModTapAction = 2 LayerOnAction = 3 LayerModAction = 4 + LayerToggleAction = 5 diff --git a/action/layer_toggle_action.py b/action/layer_toggle_action.py new file mode 100644 index 0000000..bf03d30 --- /dev/null +++ b/action/layer_toggle_action.py @@ -0,0 +1,24 @@ +from evdev.events import KeyEvent + +import mapper +from action.action_type import ActionType +from log import debug + + +class LayerToggleAction: + type: ActionType + layer: int + + def __init__(self, layer, ): + self.type = ActionType.LayerToggleAction + self.layer = layer + + def handle(self, ui, e, config, *args): + debug('-- handling layer toggle action --') + if e.value == KeyEvent.key_down: + if mapper.active_layers[self.layer]: + debug(f'disabling layer [{self.layer}]') + mapper.disable_layer(ui, self.layer, config) + else: + debug(f'enabling layer [{self.layer}]') + mapper.enable_layer(self.layer, self, config) diff --git a/example/config.py b/example/config.py index 9d3ad66..3e723b2 100644 --- a/example/config.py +++ b/example/config.py @@ -10,11 +10,11 @@ KC_TAB , KC_Q , KC_W , KC_E , KC_R , KC_T , KC_Y , KC_U , KC_I , KC_O , KC_P , KC_LBRC, KC_RBRC, KC_BSLS, KC_CAPS, KC_A , KC_S , KC_D , KC_F , KC_G , KC_H , KC_J , KC_K , KC_L , KC_SCLN, KC_QUOT, KC_ENT , KC_LSFT, KC_Z , KC_X , KC_C , KC_V , KC_B , KC_N , KC_M , KC_COMM, KC_DOT , KC_SLSH, KC_RSFT, KC_UP , - KC_LCTL, KC_LGUI, KC_LALT, LSFT_T(KC_SPC) , KC_RALT, KC_APP , KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, + TG(1) , KC_LGUI, KC_LALT, LSFT_T(KC_SPC) , KC_RALT, KC_APP , KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, ], [ T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, - T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, + T______, T______, KC_A , T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, T______, diff --git a/key.py b/key.py index fd178b0..7c2edeb 100644 --- a/key.py +++ b/key.py @@ -2,12 +2,13 @@ from action.layer_mod_action import LayerModAction from action.layer_on_action import LayerOnAction +from action.layer_toggle_action import LayerToggleAction from action.mod_key_action import ModKeyAction from action.mod_tap_action import ModTapAction # special keys -KC_NO = None +KC_NO = 999 KC_TRANSPARENT = None KC_TRNS = KC_TRANSPARENT @@ -705,3 +706,7 @@ def MO(layer): def LM(layer, *modifiers): return LayerModAction(layer, *modifiers) + + +def TG(layer): + return LayerToggleAction(layer) diff --git a/mapper.py b/mapper.py index 3d59985..27faeb8 100644 --- a/mapper.py +++ b/mapper.py @@ -78,12 +78,18 @@ def handle_event(e, kb, ui, config): debug(f'key is mapped to action of type {key.type} at {pos}') key.handle(ui, e, config, pos) else: - debug(f'key is mapped to key: {ecodes.KEY[key]} ({key}) at {pos}') + debug(f'key is mapped to key: {get_key_name(key)} ({key}) at {pos}') write_key(ui, key, e, layer_index, config) update_timestamps(pos, e) +def get_key_name(key): + if key == 999: + return 'KC_NO' + return ecodes.KEY.get(key, "UNKNOWN") + + def map_key_to_pos(code, config): debug(f'looking for key {ecodes.KEY[code]} with code {code}') try: @@ -120,7 +126,7 @@ def find_key(pos, config): def write_key(ui, key, e, layer, config): global active_layers active_layer = active_layers[layer] - if active_layer and active_layer.activator: + if active_layer and active_layer.activator and hasattr(active_layer.activator, 'handle_layer_key'): active_layer.activator.handle_layer_key(ui, key, e, config) else: host.write_code(ui, key, e.value)