Last active
January 11, 2022 15:49
-
-
Save groves/6bc4aa9d4ae5d5f837c792d15cda926c to your computer and use it in GitHub Desktop.
Scripts to tweak Keyboardio Chrysalis exported JSON keymaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
def usage(): | |
print("python copy_layer.py <source layout JSON file> <destination layout JSON file> <copy from layer index> <copy to layer index>") | |
sys.exit(1) | |
if len(sys.argv) != 5: | |
usage() | |
try: | |
first = int(sys.argv[3]) | |
second = int(sys.argv[4]) | |
except ValueError: | |
usage() | |
if first < 0 or first > 9 or second < 0 or second > 9: | |
print("Layers must be between 0 and 9") | |
usage() | |
if first == second: | |
print("Layers must be unique") | |
usage() | |
layout = json.load(open(sys.argv[1])) | |
maps = layout['keymaps'] | |
maps[second] = maps[first] | |
json.dump(layout, open(sys.argv[2], 'w'), sort_keys=True, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
def usage(): | |
print("python mod_layer.py <source layout JSON file> <destination layout JSON file> <layer index>") | |
sys.exit(1) | |
if len(sys.argv) != 4: | |
usage() | |
try: | |
layer = int(sys.argv[3]) | |
except ValueError: | |
usage() | |
if layer < 0 or layer > 9: | |
print("Layer must be between 0 and 9") | |
usage() | |
layout = json.load(open(sys.argv[1])) | |
for k in layout['keymaps'][layer]: | |
if 'categories' in k: | |
continue | |
# Offset from https://github.com/keyboardio/Chrysalis/blob/master/src/api/keymap/db/modifiers.js#L19 | |
# This is the offset for shift. Switch to another offset from Chrysalis to get a different mod | |
k['code'] += 1 << 11 | |
json.dump(layout, open(sys.argv[2], 'w'), sort_keys=True, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
def usage(): | |
print("python swap_layers.py <source layout JSON file> <destination layout JSON file> <first layer index> <second layer index>") | |
sys.exit(1) | |
if len(sys.argv) != 5: | |
usage() | |
try: | |
first = int(sys.argv[3]) | |
second = int(sys.argv[4]) | |
except ValueError: | |
usage() | |
if first < 0 or first > 9 or second < 0 or second > 9: | |
print("Layers must be between 0 and 9") | |
usage() | |
if first == second: | |
print("Layers must be unique") | |
usage() | |
layout = json.load(open(sys.argv[1])) | |
maps = layout['keymaps'] | |
first_layer = maps[first] | |
maps[first] = maps[second] | |
maps[second] = first_layer | |
for m in maps: | |
for k in m: | |
if 'layer' in k.get('categories', []): | |
target = k['target'] | |
if target == first: | |
shift = second - first | |
k['target'] = second | |
elif target == second: | |
shift = first - second | |
k['target'] = first | |
else: | |
continue | |
# One shot and pure layer shift keys offset by the layer index into their base, | |
# so we shift by the difference directly for them | |
# dualuse keys multiply by 256 to get the offset, so make that adjustment for it: | |
# https://github.com/keyboardio/Chrysalis/blob/master/src/api/keymap/db/base/dualuse.js#L34 | |
if 'dualuse' in k['categories']: | |
shift *= 256 | |
k['code'] += shift | |
json.dump(layout, open(sys.argv[2], 'w'), sort_keys=True, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment