def set_batch_lines(interval=None, lines=None): if interval is not None: _ahk.call("SetBatchLines", f"{interval}ms") return if lines is not None: _ahk.call("SetBatchLines", f"{lines}") return raise ValueError("either 'interval' or 'lines' are required")
def set_timer(func=None, period=0.25, countdown=None, priority=0): if func is None: # Return the decorator. return partial(set_timer, period=period, countdown=countdown, priority=priority) if countdown is not None: if countdown < 0: raise ValueError("countdown must be positive") period = -countdown period = int(period * 1000) _ahk.call("SetTimer", func, period, priority) return Timer(func)
def set_title_match_mode(mode=None, speed=None): if mode is not None: match_modes = { "startswith": "1", "contains": "2", "exact": "3", "1": "1", "2": "2", "3": "3", "regex": "regex", } ahk_mode = match_modes.get(str(mode).lower()) if ahk_mode is None: raise ValueError(f"unknown match mode {mode!r}") _ahk.call("SetTitleMatchMode", ahk_mode) if speed is not None: speeds = ["fast", "slow"] if speed.lower() not in speeds: raise ValueError(f"unknown speed {speed!r}") _ahk.call("SetTitleMatchMode", speed)
def hotkey(key_name, func=None, buffer=None, priority=0, max_threads=None, input_level=None): if key_name == "": raise Error("invalid key name") if func is None: # Return the decorator. return partial(hotkey, key_name, buffer=buffer, priority=priority, max_threads=max_threads, input_level=input_level) # TODO: Handle case when func == "AltTab" or other substitutes. # TODO: Set the options. # TODO: Change options of the existing hotkeys. # TODO: Return a Hotkey object. _ahk.call("Hotkey", key_name, func)
def ahk_call(cmd: str, *args): """Call the arbitrary AHK command/function *cmd* with *args* arguments. Use this function when there's no appropriate AutoHotkey.py API. """ locked = global_ahk_lock.acquire(timeout=1) if not locked: if threading.current_thread() is threading.main_thread(): err = RuntimeError( "deadlock occurred; the main thread tried calling AHK " "when it was acquired by another thread", ) # Don't show the message box with an error via AHK. err._ahk_silent_exc = True raise err global_ahk_lock.acquire() try: return _ahk.call(cmd, *args) finally: global_ahk_lock.release()
def test_call(): with pytest.raises(TypeError, match="missing 1 required"): _ahk.call() with pytest.raises(ahk.Error, match="unknown function"): _ahk.call("NoSuchFunction") os.environ["HELLO"] = "Привет 世界" hello = _ahk.call("EnvGet", "HELLO") assert hello == os.environ["HELLO"] temp = _ahk.call("EnvGet", "TEMP") assert isinstance(temp, str) rnd = _ahk.call("Min", -1, "-1") assert isinstance(rnd, int) assert rnd == -1 rnd = _ahk.call("Min", 42, "42") assert isinstance(rnd, int) assert rnd == 42 assert _ahk.call("Min", 1, True) == 1 val = _ahk.call("Max", 9223372036854775807) assert val == 9223372036854775807 val = _ahk.call("Min", -9223372036854775806) assert val == -9223372036854775806 with pytest.raises(OverflowError, match="too big to convert"): val = _ahk.call("Max", 9223372036854775808) val = _ahk.call("Min", 0.5) assert val == 0.5 with pytest.raises(ahk.Error, match="cannot convert '<object object"): _ahk.call("Min", object()) assert _ahk.call("Array", 1, 2, 3) == {1: 1, 2: 2, 3: 3} assert _ahk.call("Object", "a", 1, "b", 2, "c", 3) == { "a": 1, "b": 2, "c": 3 }
def detect_hidden_windows(value): value = "On" if value else "Off" _ahk.call("DetectHiddenWindows", value)
def send_mode(mode): _ahk.call("SendMode", mode)
def send(keys): # TODO: Consider adding `mode` keyword? _ahk.call("Send", keys)
def _main(): sys.excepthook = _excepthook try: _run_from_args() except SystemExit as exc: _ahk.call("ExitApp", _handle_system_exit(exc))
def get_key_state(key_name, mode=None): return _ahk.call("GetKeyState", key_name, mode)
def set_priority(self, priority): _ahk.call("SetTimer", self._func, "", priority)
def delete(self): _ahk.call("SetTimer", self._func, "Delete")
def disable(self): _ahk.call("SetTimer", self._func, "Off")
def enable(self): _ahk.call("SetTimer", self._func, "On")
def message_box(text=None, title="", options=0, timeout=None): if text is None: # Show "Press OK to continue." return _ahk.call("MsgBox") return _ahk.call("MsgBox", options, title, str(text), timeout)
def test_call(): with pytest.raises(ahk.Error): # Calling _ahk.call() without arguments must raise an error. _ahk.call() with pytest.raises(ahk.Error): # _ahk.call() to a non-existent function must raise an error. _ahk.call("NoSuchFunction") os.environ["HELLO"] = "Привет" hello = _ahk.call("EnvGet", "HELLO") assert hello == os.environ["HELLO"] temp = _ahk.call("EnvGet", "TEMP") assert isinstance(temp, str), "EnvGet result must be a string" rnd = _ahk.call("Random", 42, "42") assert isinstance(rnd, int), "Random result must be an integer" assert rnd == 42, f"Result must be 42, got {rnd}" assert _ahk.call("Random", 1, True) == 1, "Result must be 1" val = _ahk.call("Max", 9223372036854775807) assert val == 9223372036854775807, f"Result must be 9223372036854775807, got {val}" val = _ahk.call("Min", -9223372036854775806) assert val == -9223372036854775806, f"Result must be -9223372036854775806, got {val}" with pytest.raises(OverflowError): val = _ahk.call("Max", 9223372036854775808) val = _ahk.call("Min", 0.5) assert val == 0.5, f"Result must be 0.5, got {val}" with pytest.raises(ahk.Error, match="cannot convert '<object object"): _ahk.call("Min", object())
def win_exist(win_title, win_text="", exclude_title="", exclude_text=""): # TODO: Check that empty strings work in this case. return _ahk.call(win_title, win_text, exclude_title, exclude_text)