def _add_shortcut(shortcut, function, title=None):
"""Bind a function to a keyboard shortcut."""
# Wrap function to accept and ignore arguments
def wrapper(*args, **kwargs):
function()
# Parse shortcut
tokens = _tokenize_shortcut_string(shortcut)
_validate_tokens(tokens)
modifiers = tokens[:-1]
inp = tokens[-1]
# Process components
mod_bitmask = functools.reduce(
operator.ior,
[_modifiers[mod] for mod in modifiers],
0
)
if inp in _special_keys:
inp = _special_keys[inp]
# Make the command
sel = _add_method(_controller, wrapper)
kc = objc_util.ObjCClass("UIKeyCommand")
if title is not None:
c = kc.keyCommandWithInput_modifierFlags_action_discoverabilityTitle_(
inp,
mod_bitmask,
sel,
title
)
else:
c = kc.keyCommandWithInput_modifierFlags_action_(
inp,
mod_bitmask,
sel
)
_registered_commands[frozenset(tokens)] = cp
_controller.addKeyCommand_(c)
# MAIN INTERFACE
评论列表
文章目录