Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init_keys():
'''put this in a function to avoid polluting global namespace'''
for i in range(0, 10):
# set numpad keys
key_name = f'Numpad{i}'
key = Key(key_name)
setattr(KEYS, key_name, key)
setattr(KEYS, key_name.upper(), key)
for i in range(1, 25):
# set function keys
key_name = f'F{i}'
setattr(KEYS, key_name, Key(key_name))
for i in range(1, 33):
# set joystick keys
key_name = f'Joy{i}'
setattr(KEYS, key_name, Key(key_name))
setattr(KEYS, key_name.upper(), Key(key_name))
RIGHT_CONTROL = KeyModifier('RControl')
RCtrl = RIGHT_CONTROL
RControl = RIGHT_CONTROL
ALT = KeyModifier('Alt')
Alt = ALT
LEFT_ALT = KeyModifier('LAlt')
LAlt = LEFT_ALT
RIGHT_ALT = KeyModifier('RAlt')
RAlt = RIGHT_ALT
SHIFT = KeyModifier('Shift')
Shift = SHIFT
LEFT_SHIFT = KeyModifier("LShift")
LShift = LEFT_SHIFT
RIGHT_SHIFT = KeyModifier('RShift')
RShift = RIGHT_SHIFT
NUMPAD_DOT = Key('NumpadDot')
NumpadDot = NUMPAD_DOT
NUMPAD_DEL = Key('NumpadDel')
NumpadDel = NUMPAD_DEL
NUM_LOCK = Key('NumLock')
NumLock = NUM_LOCK
NUMPAD_ADD = Key('NumpadAdd')
NUMPAD_DIV = Key('NumpadDiv')
NUMPAD_SUB = Key('NumpadSub')
NUMPAD_MULT = Key('NumpadMult')
NUMPAD_ENTER = Key('NumpadEnter')
NumpadAdd = NUMPAD_ADD
NumpadDiv = NUMPAD_DIV
NumpadSub = NUMPAD_SUB
NumpadMult = NUMPAD_MULT
NumpadEnter = NUMPAD_ENTER
def key_down(self, key):
"""
Press down a key (without releasing it)
:param key:
:return:
"""
if isinstance(key, str):
key = Key(key_name=key)
return self.send_input(key.DOWN)
SCROLL_LOCK = Key('ScrollLock')
ScrollLock = SCROLL_LOCK
SPACE = Key('Space')
TAB = Key('Tab')
Tab = TAB
ENTER = Key('Enter')
Enter = ENTER
ESCAPE = Key('Escape')
BACKSPACE = Key('Backspace')
Backspace = BACKSPACE
UP = Key('Up')
Up = UP
DOWN = Key('Down')
Down = DOWN
LEFT = Key('Left')
Left = LEFT
RIGHT = Key('Right')
Right = RIGHT
DELETE = Key('Delete')
DEL = DELETE
Delete = DELETE
Del = DELETE
WIN = KeyModifier('Win')
Win = WIN
LEFT_WIN = KeyModifier('LWin')
LWin = LEFT_WIN
RIGHT_WIN = KeyModifier('RWin')
RWin = RIGHT_WIN
CONTROL = KeyModifier('Control')
Control = CONTROL
elif isinstance(other, KeyCombo):
return other + self
return self.symbol + str(other)
class KEYS:
"""
KEYS constants
REF: https://autohotkey.com/docs/KeyList.htm
"""
CAPS_LOCK = Key('CapsLock')
CapsLock = CAPS_LOCK
SCROLL_LOCK = Key('ScrollLock')
ScrollLock = SCROLL_LOCK
SPACE = Key('Space')
TAB = Key('Tab')
Tab = TAB
ENTER = Key('Enter')
Enter = ENTER
ESCAPE = Key('Escape')
BACKSPACE = Key('Backspace')
Backspace = BACKSPACE
UP = Key('Up')
Up = UP
DOWN = Key('Down')
Down = DOWN
LEFT = Key('Left')
Left = LEFT
RIGHT = Key('Right')
Right = RIGHT
def __add__(self, other):
if self._s is not None:
raise ValueError('Key combo is already terminated')
if isinstance(other, KeyCombo):
combo = KeyCombo(*[*self.modifiers, *other.modifiers])
if other._s:
combo = combo + other._s
return combo
if isinstance(other, KeyModifier):
self.modifiers.append(other)
elif isinstance(other, Key) or isinstance(other, str):
self._s = str(other)
return self
else:
raise TypeError(f"unsupported operand type(s) for +: '{self.__class__.__name__}' and '{type(other)}'")
RIGHT_ALT = KeyModifier('RAlt')
RAlt = RIGHT_ALT
SHIFT = KeyModifier('Shift')
Shift = SHIFT
LEFT_SHIFT = KeyModifier("LShift")
LShift = LEFT_SHIFT
RIGHT_SHIFT = KeyModifier('RShift')
RShift = RIGHT_SHIFT
NUMPAD_DOT = Key('NumpadDot')
NumpadDot = NUMPAD_DOT
NUMPAD_DEL = Key('NumpadDel')
NumpadDel = NUMPAD_DEL
NUM_LOCK = Key('NumLock')
NumLock = NUM_LOCK
NUMPAD_ADD = Key('NumpadAdd')
NUMPAD_DIV = Key('NumpadDiv')
NUMPAD_SUB = Key('NumpadSub')
NUMPAD_MULT = Key('NumpadMult')
NUMPAD_ENTER = Key('NumpadEnter')
NumpadAdd = NUMPAD_ADD
NumpadDiv = NUMPAD_DIV
NumpadSub = NUMPAD_SUB
NumpadMult = NUMPAD_MULT
NumpadEnter = NUMPAD_ENTER
def _init_keys():
'''put this in a function to avoid polluting global namespace'''
for i in range(0, 10):
# set numpad keys
key_name = f'Numpad{i}'
key = Key(key_name)
def key_release(self, key):
"""
Release a key that is currently in pressed down state
:param key:
:return:
"""
if isinstance(key, str):
key = Key(key_name=key)
return self.send_input(key.UP)
return self.symbol + str(other)
class KEYS:
"""
KEYS constants
REF: https://autohotkey.com/docs/KeyList.htm
"""
CAPS_LOCK = Key('CapsLock')
CapsLock = CAPS_LOCK
SCROLL_LOCK = Key('ScrollLock')
ScrollLock = SCROLL_LOCK
SPACE = Key('Space')
TAB = Key('Tab')
Tab = TAB
ENTER = Key('Enter')
Enter = ENTER
ESCAPE = Key('Escape')
BACKSPACE = Key('Backspace')
Backspace = BACKSPACE
UP = Key('Up')
Up = UP
DOWN = Key('Down')
Down = DOWN
LEFT = Key('Left')
Left = LEFT
RIGHT = Key('Right')
Right = RIGHT
DELETE = Key('Delete')
DEL = DELETE
CAPS_LOCK = Key('CapsLock')
CapsLock = CAPS_LOCK
SCROLL_LOCK = Key('ScrollLock')
ScrollLock = SCROLL_LOCK
SPACE = Key('Space')
TAB = Key('Tab')
Tab = TAB
ENTER = Key('Enter')
Enter = ENTER
ESCAPE = Key('Escape')
BACKSPACE = Key('Backspace')
Backspace = BACKSPACE
UP = Key('Up')
Up = UP
DOWN = Key('Down')
Down = DOWN
LEFT = Key('Left')
Left = LEFT
RIGHT = Key('Right')
Right = RIGHT
DELETE = Key('Delete')
DEL = DELETE
Delete = DELETE
Del = DELETE
WIN = KeyModifier('Win')
Win = WIN
LEFT_WIN = KeyModifier('LWin')
LWin = LEFT_WIN
RIGHT_WIN = KeyModifier('RWin')
RWin = RIGHT_WIN