IS_MACOS = sys.platform.startswith('darwin') EVK_ALT = 10003 EVK_CTRL = 10001 EVK_MOUSE_WHEEL_DOWN = 11002 EVK_MOUSE_WHEEL_UP = 11001 EVK_SHIFT = 10004 EVK_WIN = 10002 KVK_COMMAND = 0X37 KVK_CONTROL = 0X3B KVK_OPTION = 0X3A KVK_RIGHTCOMMAND = 0X36 KVK_RIGHTCONTROL = 0X3E KVK_RIGHTOPTION = 0X3D KVK_RIGHTSHIFT = 0X3C KVK_SHIFT = 0X38 MAP_CHAR_TO_WIN_VK = {
    '0': VK_KEY_0,
    '1': VK_KEY_1,
    '2': VK_KEY_2,
    '3': VK_KEY_3,
    '4': VK_KEY_4,
    '5': VK_KEY_5,
    '6': VK_KEY_6,
    '7': VK_KEY_7,
    '8': VK_KEY_8,
    '9': VK_KEY_9,
    'a': VK_KEY_A,
    'b': VK_KEY_B,
    'c': VK_KEY_C,
    'd': VK_KEY_D,
    'e': VK_KEY_E,
    'f': VK_KEY_F,
    'g': VK_KEY_G,
    'h': VK_KEY_H,
    'i': VK_KEY_I,
    'j': VK_KEY_J,
    'k': VK_KEY_K,
    'l': VK_KEY_L,
    'm': VK_KEY_M,
    'n': VK_KEY_N,
    'o': VK_KEY_O,
    'p': VK_KEY_P,
    'q': VK_KEY_Q,
    'r': VK_KEY_R,
    's': VK_KEY_S,
    't': VK_KEY_T,
    'u': VK_KEY_U,
    'v': VK_KEY_V,
    'w': VK_KEY_W,
    'x': VK_KEY_X,
    'y': VK_KEY_Y,
    'z': VK_KEY_Z,
    ' ': VK_SPACE,
    '-': VK_OEM_MINUS,
    '=': VK_OEM_PLUS,
    ';': VK_OEM_1,
    '/': VK_OEM_2,
    '`': VK_OEM_3,
    '[': VK_OEM_4,
    '\\': VK_OEM_5,
    ']': VK_OEM_6,
    "'": VK_OEM_7,
    ',': VK_OEM_COMMA,
    '.': VK_OEM_PERIOD,
    '\b': VK_BACK,
    '\t': VK_TAB,
    '\n': VK_RETURN,
} MAP_NAME_TO_MAC_VK = dict(
    (k, v) for k, v in globals().items() if k.startswith('KVK_')
) MAP_NAME_TO_WIN_VK = dict(
    (k, v) for k, v in globals().items() if k.startswith('VK_')
) MAP_SHIFTED_CHAR_TO_WIN_VK = {
    ')': VK_KEY_0,
    '!': VK_KEY_1,
    '@': VK_KEY_2,
    '#': VK_KEY_3,
    '$': VK_KEY_4,
    '%': VK_KEY_5,
    '^': VK_KEY_6,
    '&': VK_KEY_7,
    '*': VK_KEY_8,
    '(': VK_KEY_9,
    'A': VK_KEY_A,
    'B': VK_KEY_B,
    'C': VK_KEY_C,
    'D': VK_KEY_D,
    'E': VK_KEY_E,
    'F': VK_KEY_F,
    'G': VK_KEY_G,
    'H': VK_KEY_H,
    'I': VK_KEY_I,
    'J': VK_KEY_J,
    'K': VK_KEY_K,
    'L': VK_KEY_L,
    'M': VK_KEY_M,
    'N': VK_KEY_N,
    'O': VK_KEY_O,
    'P': VK_KEY_P,
    'Q': VK_KEY_Q,
    'R': VK_KEY_R,
    'S': VK_KEY_S,
    'T': VK_KEY_T,
    'U': VK_KEY_U,
    'V': VK_KEY_V,
    'W': VK_KEY_W,
    'X': VK_KEY_X,
    'Y': VK_KEY_Y,
    'Z': VK_KEY_Z,
    '_': VK_OEM_MINUS,
    '+': VK_OEM_PLUS,
    ':': VK_OEM_1,
    '?': VK_OEM_2,
    '~': VK_OEM_3,
    '{': VK_OEM_4,
    '|': VK_OEM_5,
    '}': VK_OEM_6,
    '"': VK_OEM_7,
    '<': VK_OEM_COMMA,
    '>': VK_OEM_PERIOD,
} MAP_WIN_VK_TO_MAC_VK = {
    VK_ESCAPE: KVK_ESCAPE,
    VK_BACK: KVK_DELETE,
    VK_RETURN: KVK_RETURN,
    VK_TAB: KVK_TAB,
    VK_CAPITAL: KVK_CAPSLOCK,
    VK_LSHIFT: KVK_SHIFT,
    VK_RSHIFT: KVK_RIGHTSHIFT,
    VK_LCONTROL: KVK_CONTROL,
    VK_RCONTROL: KVK_RIGHTCONTROL,
    VK_LWIN: KVK_COMMAND,
    VK_RWIN: KVK_RIGHTCOMMAND,
    VK_LMENU: KVK_OPTION,
    VK_RMENU: KVK_RIGHTOPTION,
    VK_F1: KVK_F1,
    VK_F2: KVK_F2,
    VK_F3: KVK_F3,
    VK_F4: KVK_F4,
    VK_F5: KVK_F5,
    VK_F6: KVK_F6,
    VK_F7: KVK_F7,
    VK_F8: KVK_F8,
    VK_F9: KVK_F9,
    VK_F10: KVK_F10,
    VK_F11: KVK_F11,
    VK_F12: KVK_F12,
    VK_F13: KVK_F13,
    VK_F14: KVK_F14,
    VK_F15: KVK_F15,
    VK_F16: KVK_F16,
    VK_F17: KVK_F17,
    VK_F18: KVK_F18,
    VK_F19: KVK_F19,
    VK_F20: KVK_F20,
    VK_KEY_0: KVK_ANSI_0,
    VK_KEY_1: KVK_ANSI_1,
    VK_KEY_2: KVK_ANSI_2,
    VK_KEY_3: KVK_ANSI_3,
    VK_KEY_4: KVK_ANSI_4,
    VK_KEY_5: KVK_ANSI_5,
    VK_KEY_6: KVK_ANSI_6,
    VK_KEY_7: KVK_ANSI_7,
    VK_KEY_8: KVK_ANSI_8,
    VK_KEY_9: KVK_ANSI_9,
    VK_KEY_A: KVK_ANSI_A,
    VK_KEY_B: KVK_ANSI_B,
    VK_KEY_C: KVK_ANSI_C,
    VK_KEY_D: KVK_ANSI_D,
    VK_KEY_E: KVK_ANSI_E,
    VK_KEY_F: KVK_ANSI_F,
    VK_KEY_G: KVK_ANSI_G,
    VK_KEY_H: KVK_ANSI_H,
    VK_KEY_I: KVK_ANSI_I,
    VK_KEY_J: KVK_ANSI_J,
    VK_KEY_K: KVK_ANSI_K,
    VK_KEY_L: KVK_ANSI_L,
    VK_KEY_M: KVK_ANSI_M,
    VK_KEY_N: KVK_ANSI_N,
    VK_KEY_O: KVK_ANSI_O,
    VK_KEY_P: KVK_ANSI_P,
    VK_KEY_Q: KVK_ANSI_Q,
    VK_KEY_R: KVK_ANSI_R,
    VK_KEY_S: KVK_ANSI_S,
    VK_KEY_T: KVK_ANSI_T,
    VK_KEY_U: KVK_ANSI_U,
    VK_KEY_V: KVK_ANSI_V,
    VK_KEY_W: KVK_ANSI_W,
    VK_KEY_X: KVK_ANSI_X,
    VK_KEY_Y: KVK_ANSI_Y,
    VK_KEY_Z: KVK_ANSI_Z,
    VK_OEM_3: KVK_ANSI_GRAVE,
    VK_OEM_MINUS: KVK_ANSI_MINUS,
    VK_OEM_PLUS: KVK_ANSI_EQUAL,
    VK_OEM_4: KVK_ANSI_LEFTBRACKET,
    VK_OEM_6: KVK_ANSI_RIGHTBRACKET,
    VK_OEM_5: KVK_ANSI_BACKSLASH,
    VK_OEM_1: KVK_ANSI_SEMICOLON,
    VK_OEM_7: KVK_ANSI_QUOTE,
    VK_OEM_COMMA: KVK_ANSI_COMMA,
    VK_OEM_PERIOD: KVK_ANSI_PERIOD,
    VK_OEM_2: KVK_ANSI_SLASH,
    VK_DELETE: KVK_FORWARDDELETE,
    VK_HOME: KVK_HOME,
    VK_END: KVK_END,
    VK_PRIOR: KVK_PAGEUP,
    VK_NEXT: KVK_PAGEDOWN,
    VK_UP: KVK_UPARROW,
    VK_LEFT: KVK_LEFTARROW,
    VK_RIGHT: KVK_RIGHTARROW,
    VK_DOWN: KVK_DOWNARROW,
    VK_DIVIDE: KVK_ANSI_KEYPADDIVIDE,
    VK_MULTIPLY: KVK_ANSI_KEYPADMULTIPLY,
    VK_SUBTRACT: KVK_ANSI_KEYPADMINUS,
    VK_ADD: KVK_ANSI_KEYPADPLUS,
    VK_DECIMAL: KVK_ANSI_KEYPADDECIMAL,
    VK_RETURN: KVK_ANSI_KEYPADENTER,
    VK_NUMPAD1: KVK_ANSI_KEYPAD1,
    VK_NUMPAD2: KVK_ANSI_KEYPAD2,
    VK_NUMPAD3: KVK_ANSI_KEYPAD3,
    VK_NUMPAD4: KVK_ANSI_KEYPAD4,
    VK_NUMPAD5: KVK_ANSI_KEYPAD5,
    VK_NUMPAD6: KVK_ANSI_KEYPAD6,
    VK_NUMPAD7: KVK_ANSI_KEYPAD7,
    VK_NUMPAD8: KVK_ANSI_KEYPAD8,
    VK_NUMPAD9: KVK_ANSI_KEYPAD9,
} VK_DIVIDE = 0x6F VK_ESCAPE = 0x1B VK_LCONTROL = 0xA2 VK_LMENU = 0xA4 VK_LSHIFT = 0xA0 VK_LWIN = 0x5B VK_MOUSE_LEFT_DOWN = 513 VK_MOUSE_LEFT_UP = 514 VK_MOUSE_MIDDLE_DOWN = 519 VK_MOUSE_MIDDLE_UP = 520 VK_MOUSE_MOVE = 512 VK_MOUSE_RIGHT_DOWN = 516 VK_MOUSE_RIGHT_UP = 517 VK_MOUSE_WHEEL = 522 VK_MULTIPLY = 0x6A VK_NEXT = 0x22 VK_NUMPAD0 = 0x60 VK_NUMPAD1 = 0x61 VK_NUMPAD2 = 0x62 VK_NUMPAD3 = 0x63 VK_NUMPAD4 = 0x64 VK_NUMPAD5 = 0x65 VK_NUMPAD6 = 0x66 VK_NUMPAD7 = 0x67 VK_NUMPAD8 = 0x68 VK_NUMPAD9 = 0x69 VK_OEM_1 = 0xBA VK_OEM_2 = 0xBF VK_OEM_3 = 0xC0 VK_OEM_4 = 0xDB VK_OEM_5 = 0xDC VK_OEM_6 = 0xDD VK_OEM_7 = 0xDE VK_PRIOR = 0x21 VK_RCONTROL = 0xA3 VK_RETURN = 0xD VK_RMENU = 0xA5 VK_RSHIFT = 0xA1 VK_RWIN = 0x5C VK_SUBTRACT = 0x6D
from copy import copy
from .compat import IS_MACOS
from .virtualkey import EVK_ALT
from .virtualkey import EVK_CTRL
from .virtualkey import EVK_MOUSE_WHEEL_DOWN
from .virtualkey import EVK_MOUSE_WHEEL_UP
from .virtualkey import EVK_SHIFT
from .virtualkey import EVK_WIN
from .virtualkey import KVK_COMMAND
from .virtualkey import KVK_CONTROL
from .virtualkey import KVK_OPTION
from .virtualkey import KVK_RIGHTCOMMAND
from .virtualkey import KVK_RIGHTCONTROL
from .virtualkey import KVK_RIGHTOPTION
from .virtualkey import KVK_RIGHTSHIFT
from .virtualkey import KVK_SHIFT
from .virtualkey import MAP_CHAR_TO_WIN_VK
from .virtualkey import MAP_NAME_TO_MAC_VK
from .virtualkey import MAP_NAME_TO_WIN_VK
from .virtualkey import MAP_SHIFTED_CHAR_TO_WIN_VK
from .virtualkey import MAP_WIN_VK_TO_MAC_VK
from .virtualkey import VK_DIVIDE
from .virtualkey import VK_ESCAPE
from .virtualkey import VK_LCONTROL
from .virtualkey import VK_LMENU
from .virtualkey import VK_LSHIFT
from .virtualkey import VK_LWIN
from .virtualkey import VK_MOUSE_LEFT_DOWN
from .virtualkey import VK_MOUSE_LEFT_UP
from .virtualkey import VK_MOUSE_MIDDLE_DOWN
from .virtualkey import VK_MOUSE_MIDDLE_UP
from .virtualkey import VK_MOUSE_MOVE
from .virtualkey import VK_MOUSE_RIGHT_DOWN
from .virtualkey import VK_MOUSE_RIGHT_UP
from .virtualkey import VK_MOUSE_WHEEL
from .virtualkey import VK_MULTIPLY
from .virtualkey import VK_NEXT
from .virtualkey import VK_NUMPAD0
from .virtualkey import VK_NUMPAD1
from .virtualkey import VK_NUMPAD2
from .virtualkey import VK_NUMPAD3
from .virtualkey import VK_NUMPAD4
from .virtualkey import VK_NUMPAD5
from .virtualkey import VK_NUMPAD6
from .virtualkey import VK_NUMPAD7
from .virtualkey import VK_NUMPAD8
from .virtualkey import VK_NUMPAD9
from .virtualkey import VK_OEM_1
from .virtualkey import VK_OEM_2
from .virtualkey import VK_OEM_3
from .virtualkey import VK_OEM_4
from .virtualkey import VK_OEM_5
from .virtualkey import VK_OEM_6
from .virtualkey import VK_OEM_7
from .virtualkey import VK_PRIOR
from .virtualkey import VK_RCONTROL
from .virtualkey import VK_RETURN
from .virtualkey import VK_RMENU
from .virtualkey import VK_RSHIFT
from .virtualkey import VK_RWIN
from .virtualkey import VK_SUBTRACT
import re
# coding: utf-8
"""
This module contains hotkey parse function.
"""
from __future__ import absolute_import

# Standard imports

# Local imports


def _dict_swap_kv(dict_obj):
    """
    Swap given dict's key-value pairs.

    :param dict_obj: Dict.

    :return: New dict with key-value pairs swapped.
    """
    # Swap given dict's keys and values
    return dict((x[1], x[0]) for x in dict_obj.items())


def _extend_short_names(name_to_vk_map, keep_orig=True):
    """
    Extend given `name to virtual key` map with short names.

    Rules for converting standard name to short name:
        VK_KEY_A -> A
        VK_HOME -> HOME
        KVK_ANSI_A -> A
        KVK_HOME -> HOME

    :param name_to_vk_map: Name to virtual key map.

    :param keep_orig: Whether keep original names.

    :return: New `name to virtual key` map with short names added.
    """
    # Result dict
    res_dict = {}

    # For given `name to virtual key` map's each item
    for name, vk in name_to_vk_map.items():
        # If need keep original names
        if keep_orig:
            # Add the original name to the result dict
            res_dict[name] = vk

        # Short name
        short_name = None

        # If the name starts with `VK_KEY_`
        if name.startswith('VK_KEY_'):
            # Strip prefix `VK_KEY_`, e.g. `VK_KEY_A` -> `A`
            short_name = name[7:]

        # If the name not starts with `VK_KEY_`.

        # If the name starts with `VK_`.
        elif name.startswith('VK_'):
            # Strip prefix `VK_`, e.g. `VK_HOME` -> `HOME`
            short_name = name[3:]

        # If the name starts with `KVK_ANSI_`
        elif name.startswith('KVK_ANSI_'):
            # Strip prefix `KVK_ANSI_`, e.g. `KVK_ANSI_A` -> `A`
            short_name = name[9:]

        # If the name not starts with `KVK_ANSI_`.

        # If the name starts with `KVK_`.
        elif name.startswith('KVK_'):
            # Strip prefix `KVK_`, e.g. `KVK_HOME` -> `HOME`
            short_name = name[4:]

        # If the name is none of above
        else:
            # Set short name be None
            short_name = None

        # If short name is found
        if short_name is not None:
            # Add the short name to the result dict
            res_dict[short_name] = vk

    # Return the result dict
    return res_dict


# Map standard name or short name to MacOS virtual key
_MAP_NAME_SHORT_NAME_TO_MAC_VK = _extend_short_names(MAP_NAME_TO_MAC_VK)


# Map custom name to Windows virtual key
_MAP_CUSTOM_NAME_TO_WIN_VK = {
    '^': EVK_CTRL,
    '#': EVK_WIN,
    '!': EVK_ALT,
    '+': EVK_SHIFT,
    'ESC': VK_ESCAPE,
    'SUB': VK_SUBTRACT,
    'MUL': VK_MULTIPLY,
    'DIV': VK_DIVIDE,
    'PAGEUP': VK_PRIOR,
    'PAGEDOWN': VK_NEXT,

    'ENTER': VK_RETURN,