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
    if IS_MACOS:
        # Map standard name or short name to MacOS virtual key
        mac_vk = _MAP_NAME_SHORT_NAME_TO_MAC_VK.get(name.upper(), None)

        # If virtual key is found
        if mac_vk is not None:
            # Return the virtual key
            return mac_vk

    # Map standard name or short name to Windows virtual key.
    win_vk = _map_name_to_win_vk(name)

    # If virtual key is found
    if win_vk is not None:
        # If the platform is MacOS
        if IS_MACOS:
            # If the virtual key is pre-defined
            if win_vk < 1000:
                # Map the Windows virtual key to MacOS virtual key.
                # May be None.
                vk = MAP_WIN_VK_TO_MAC_VK.get(win_vk, None)

            # If the virtual key is not pre-defined,
            # it means the virtual key is extended virtual key, e.g. EVK_CTRL.
            # Extended virtual keys will be processed by hotkey parse function.
            else:
                # Use the virtual key as-is
                vk = win_vk

            # Return the virtual key, or None
            return vk

    # Return the virtual key, or None
    return win_vk


# Map Windows virtual key to custom name
_MAP_WIN_VK_TO_CUSTOM_NAME = {}

# Add standard names
_MAP_WIN_VK_TO_CUSTOM_NAME.update(_dict_swap_kv(MAP_NAME_TO_WIN_VK))

# Add short names
_MAP_WIN_VK_TO_CUSTOM_NAME.update(
    _dict_swap_kv(_extend_short_names(MAP_NAME_TO_WIN_VK, keep_orig=False))
)

# Add custom names
_MAP_WIN_VK_TO_CUSTOM_NAME.update(_dict_swap_kv(_MAP_CUSTOM_NAME_TO_WIN_VK))

# Specify custom names for virtual keys that have multiple custom names
_MAP_WIN_VK_TO_CUSTOM_NAME.update({
    EVK_CTRL: 'ECTRL',
    EVK_WIN: 'EWIN',
    EVK_ALT: 'EALT',
    EVK_SHIFT: 'ESHIFT',
    VK_LCONTROL: 'LCTRL',
    VK_RCONTROL: 'RCTRL',
    VK_LWIN: 'LWIN',
    VK_RWIN: 'RWIN',
    VK_LMENU: 'LALT',
    VK_RMENU: 'RALT',
    VK_LSHIFT: 'LSHIFT',
    VK_RSHIFT: 'RSHIFT',
    VK_ESCAPE: 'ESC',
})


# Map MacOS virtual key to custom name
_MAP_MAC_VK_TO_CUSTOM_NAME = {}

# Add standard names
_MAP_MAC_VK_TO_CUSTOM_NAME.update(_dict_swap_kv(MAP_NAME_TO_MAC_VK))

# Add short names
_MAP_MAC_VK_TO_CUSTOM_NAME.update(
    _dict_swap_kv(_extend_short_names(MAP_NAME_TO_MAC_VK, keep_orig=False))
)


def vk_ctn(vk):
    """
    Map given virtual key to name.

    :param vk: Virtual key.

    :return: Virtual key's name, or None if not found.
    """
    # If the platform is MacOS
    if IS_MACOS:
        # Map given virtual key to name.
        # May be None.
        return _MAP_MAC_VK_TO_CUSTOM_NAME.get(vk, None)

    # If the platform is not MacOS.
    else:
        # Map given virtual key to name.
        # May be None.
        return _MAP_WIN_VK_TO_CUSTOM_NAME.get(vk, None)


# 2AO2I
# Map virtual key to a tuple containing two sided Windows virtual keys.
#
# The two-item tuple structure is required by 3MMWQ.
_MAP_VK_TO_SIDED_WIN_VK_TUPLE = {
    EVK_CTRL: (VK_LCONTROL, VK_RCONTROL),
    EVK_WIN: (VK_LWIN, VK_RWIN),
    EVK_ALT: (VK_LMENU, VK_RMENU),
    EVK_SHIFT: (VK_LSHIFT, VK_RSHIFT),
    VK_MOUSE_WHEEL: (EVK_MOUSE_WHEEL_UP, EVK_MOUSE_WHEEL_DOWN),
}

# Map virtual key to a tuple containing two sided MacOS virtual keys.
#
# The two-item tuple structure is required by 3MMWQ.
_MAP_VK_TO_SIDED_MAC_VK_TUPLE = {
    EVK_CTRL: (KVK_CONTROL, KVK_RIGHTCONTROL),
    EVK_WIN: (KVK_COMMAND, KVK_RIGHTCOMMAND),
    EVK_ALT: (KVK_OPTION, KVK_RIGHTOPTION),

    EVK_SHIFT: (KVK_SHIFT, KVK_RIGHTSHIFT),