kb_shortcuts.py 文件源码

python
阅读 122 收藏 0 点赞 0 评论 0

项目:ui2 作者: controversial 项目源码 文件源码
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
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号