Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Last active May 25, 2020 12:28
Show Gist options
  • Save frankrolf/8e8ac67cdffcc4614368c10a348fac81 to your computer and use it in GitHub Desktop.
Save frankrolf/8e8ac67cdffcc4614368c10a348fac81 to your computer and use it in GitHub Desktop.
Robofont: which shortkeys are assigned to what action?
'''
Robofont: which shortkeys are assigned to what action?
'''
import AppKit
import itertools
import mojo
modifiers = {
AppKit.NSEventModifierFlagOption: '⌥',
AppKit.NSEventModifierFlagCommand: '⌘',
AppKit.NSEventModifierFlagControl: '^',
AppKit.NSEventModifierFlagShift: '⇧',
}
key_codes = {
# no idea how to get to those key codes in a smarter way
'\uf700': '↑',
'\uf701': '↓',
'\uf72c': '⇞',
'\uf72d': '⇟',
}
for i in range(1, len(modifiers) + 1):
# calculcate key codes for all possible combinations of modifier keys
for key_combination in itertools.combinations(modifiers.keys(), i):
key_labels = [modifiers.get(key_code) for key_code in key_combination]
key_code = sum(key_combination)
key_codes[key_code] = ' + '.join(key_labels)
shortcuts = mojo.UI.getMenuShortCuts()
menu_dict = {}
for menu_path, key_sequence in shortcuts.items():
readable_key_sequence = ' + '.join(
[key_codes.get(k, str(k)) for k in key_sequence])
joined_menu_path = ' > '.join(menu_path)
menu_dict[f'{readable_key_sequence}'] = f'{joined_menu_path}'
longest_command = max([len(command) for command in menu_dict.keys()])
mojo.UI.OutputWindow().clear()
# sorted by action
# for key_sequence, menu_path in sorted(
# menu_dict.items(), key=lambda item: item[1]
# ):
# print(key_sequence.rjust(longest_command), ':', menu_path)
# # sorted by shortcut
for key_sequence, menu_path in sorted(
menu_dict.items()
):
print(key_sequence.rjust(longest_command), ':', menu_path)
mojo.UI.OutputWindow().show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment