41 lines
1.9 KiB
Python
41 lines
1.9 KiB
Python
from keytypes import LayerKey
|
|
|
|
|
|
def test_keymap(keymap: list[list], output: bool = False) -> None:
|
|
valid_keycodes = range(4, 120)
|
|
valid_modifiers = [0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000]
|
|
if output:
|
|
print("Testing keymap for errors")
|
|
for layer in keymap:
|
|
for key in layer:
|
|
if not isinstance(key, LayerKey):
|
|
if key in valid_keycodes:
|
|
if output:
|
|
print(
|
|
f"key is Keycode [{keymap.index(layer)}][{layer.index(key)}]", end=".. ")
|
|
if key in valid_modifiers:
|
|
if output:
|
|
print(
|
|
f"key is Modifier [{keymap.index(layer)}][{layer.index(key)}]", end=".. ")
|
|
if output:
|
|
print("and is valid")
|
|
continue
|
|
if isinstance(key, LayerKey):
|
|
layer_key_name = key.__class__.__name__
|
|
if output:
|
|
print(
|
|
f"key is {layer_key_name} [{keymap.index(layer)}][{layer.index(key)}]", end=".. ")
|
|
key_on_layer_to_switch_to = keymap[key.layer][layer.index(key)]
|
|
key_is_not_hold = type(
|
|
key_on_layer_to_switch_to) is not type(key)
|
|
hold_key_is_not_same_layer = type(
|
|
key_on_layer_to_switch_to) is type(key) and key_on_layer_to_switch_to.layer != key.layer
|
|
if key_is_not_hold or hold_key_is_not_same_layer:
|
|
raise Exception(
|
|
f"{layer_key_name} layer key on layer[{keymap.index(layer)}] key[{layer.index(key)}] must have an identical {layer_key_name} layer key on layer[{key.layer}] key[{layer.index(key)}]")
|
|
else:
|
|
if output:
|
|
print("and is valid")
|
|
if output:
|
|
print("Done testing keymap")
|