def test_clipboard_returns(request, child_ahk): stored = ahk.get_clipboard() request.addfinalizer(lambda: ahk.set_clipboard(stored)) def clipboards(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) @ahk.on_clipboard_change() def objector(): return object() print("ok00") child_ahk.popen_code(clipboards) child_ahk.wait(0) ahk.set_clipboard("371") assert not ahk.windows.wait( title="Python.ahk", text="Error: cannot convert '<object object", timeout=0.1, ) ahk.send("{F24}")
def test_on_message_timeout(child_ahk): def code(): import ahkpy as ahk import os import sys ahk.hotkey("F24", sys.exit) @ahk.on_message(0x5555) def slow_handler(): ahk.sleep(1) return 42 print(os.getpid()) proc = child_ahk.popen_code(code) ahk_pid = int(proc.stdout.readline().strip()) win = ahk.all_windows.first(pid=ahk_pid) with pytest.raises(ahk.Error, match="response timed out"): win.send_message(0x5555, 0, 99, timeout=0.1) ahk.send("{F24}") child_ahk.close() assert proc.stdout.read() == "" assert proc.stderr.read() == "" assert proc.returncode == 0
def test_keyboard_interrupt_broken_handler(request, tmpdir, child_ahk): def code(): import ahkpy as ahk import signal import sys ahk.hotkey("F24", sys.exit) def handler(*args): 1 / 0 signal.signal(signal.SIGINT, handler) print("ok00") script = tmpdir / "script.py" script.write(child_ahk.extract_code(code)) proc = winpty.PtyProcess.spawn(f"py.exe -m ahkpy -q {script}", dimensions=(24, 120)) request.addfinalizer(proc.terminate) assert "ok00" in proc.readline() proc.sendintr() proc.read() assert "ZeroDivisionError" in proc.read() assert proc.isalive() ahk.send("{F24}") proc.wait()
def compose_send(keys, shift_keys=None): shift_pressed = ahk.is_key_pressed("Shift") ahk.wait_key_released(COMPOSE) if shift_keys and shift_pressed: ahk.send(shift_keys) else: ahk.send(keys)
def test_title_match_mode(child_ahk, settings): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.message_box("Hello from AutoHotkey.py", title="AutoHotkey") child_ahk.popen_code(code) settings.win_delay = 0 ahk_windows = ahk.windows.filter(text="Hello from AutoHotkey.py") assert ahk_windows.wait(timeout=1) assert ahk_windows.exist(title="AutoHot") assert ahk_windows.exist(title="AutoHot", match="startswith") assert not ahk_windows.exist(title="utoHot", match="startswith") assert ahk_windows.exist(title="AutoHot", match="contains") assert ahk_windows.exist(title="toHot", match="contains") assert ahk_windows.exist(title="AutoHotkey", match="exact") assert not ahk_windows.exist(title="AutoHot", match="exact") assert not ahk_windows.exist(title="toHot", match="exact") assert ahk_windows.exist(title=r"Hotkey$", match="regex") assert ahk_windows.exist(exe=r"utoHotkey.*\.exe", match="regex") with pytest.raises(ValueError, match="is not a valid title match mode"): assert ahk_windows.exist(title="AutoHot", match="nooo") ahk.send("{F24}")
def msg_boxes(self, child_ahk): def windows(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.set_countdown(0.1, ahk.message_box, "win1", "ahkpy win1") ahk.set_countdown(0.3, ahk.message_box, "win2", "ahkpy win2") ahk.sleep(1) sys.exit() assert ahk.windows.wait_active() child_ahk.popen_code(windows) msg_boxes = ahk.windows.filter(title="ahkpy win") with ahk.local_settings() as settings: settings.win_delay = 0 wait_result = msg_boxes.wait(timeout=1) assert wait_result is not None assert isinstance(wait_result, ahk.Window) yield msg_boxes ahk.send("{F24}")
def test_coop(child_ahk): def code(): import runpy import sys import ahkpy as ahk ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.message_box, "hello") print("ok00") sys.argv.append("8010") ahk.coop( runpy.run_module, mod_name="http.server", run_name="__main__", alter_sys=True, ) proc = child_ahk.popen_code(code) child_ahk.wait(0) assert proc.stdout.readline().startswith("Serving HTTP") from urllib.request import urlopen resp = urlopen("http://localhost:8010") assert b"Directory listing for /" in resp.read() assert '"GET / HTTP/1.1" 200' in proc.stderr.readline() ahk.send("{F13}") assert ahk.windows.wait_active(title="Python.ahk", text="hello", timeout=1) # TODO: Test sending KeyboardInterrupt. ahk.send("{F24}")
def test_block_input(child_ahk): def hotkeys(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) @ahk.hotkey("F15") def block(): with ahk.block_input(): ahk.sleep(0.1) print("ok01") print("ok00") child_ahk.popen_code(hotkeys) child_ahk.wait(0) ahk.send("{F15}") x, y = ahk.get_mouse_pos() ahk.mouse_move(100, 100, relative_to="cursor", mode="event") assert ahk.get_mouse_pos() == (x, y) child_ahk.wait(1) x, y = ahk.get_mouse_pos() ahk.mouse_move(100, 100, relative_to="cursor", mode="event") assert ahk.get_mouse_pos() == (x + 100, y + 100) ahk.mouse_move(-100, -100, relative_to="cursor", mode="event") ahk.send("{F24}")
def test_key_wait(child_ahk): def code(): import ahkpy as ahk import sys print("ok00") result = ahk.wait_key_pressed("RShift") assert result is True, f"result must be True, got {result!r}" print("ok01") result = ahk.wait_key_released("RShift") assert result is True, f"result must be True, got {result!r}" print("ok02") result = ahk.wait_key_pressed("RShift", timeout=.1) assert result is False, f"result must be False, got {result!r}" print("ok03") sys.exit() proc = child_ahk.popen_code(code) child_ahk.wait(0) ahk.send("{RShift Down}") child_ahk.wait(1) ahk.send("{RShift Up}") child_ahk.wait(2) child_ahk.wait(3) child_ahk.close() assert proc.returncode == 0
def find_dialog(self, notepad, edit): edit.text = "q" # Enter some text to enable searching assert ahk.windows.get_active().id == notepad.id ahk.send("^f") find_dialog = ahk.windows.wait_active(title="Find", pid=notepad.pid, timeout=1) assert find_dialog yield find_dialog assert find_dialog.close(timeout=1) notepad.get_control("Edit1").text = ""
async def handle(reader, writer): try: send_bytes = await reader.read() send_str = send_bytes.decode() print(repr(send_str)) ahk.send(send_str) finally: writer.close()
def test_match_text_slow(notepad): text = "beep boop autohotkey" notepad.send("{Text}" + text) ahk.sleep(0.1) assert text in notepad.text text_filter = ahk.windows.filter(exe="Notepad.exe", text=text) assert not text_filter.exist() assert text_filter.match_text_slow().exist() ahk.send("{F24}")
def test_get_key_state(child_ahk): with pytest.raises( ValueError, match= "'beep' is not a valid key or the state of the key could not be determined" ): ahk.is_key_pressed("beep") assert ahk.is_key_pressed("F13") is False ahk.send("{F13 Down}") assert ahk.is_key_pressed("F13") is True ahk.send("{F13 Up}") assert ahk.is_key_pressed("F13") is False
def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) @ahk.hotkey("F13") def _(): # This executes after SystemExit was raised, but have not been # handled yet. pass ahk.send("{F13}") for _ in range(1_000_000): pass
def test_active_window_context(child_ahk, settings): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.message_box, "General", max_threads=2) ctx = ahk.windows.active_window_context(title="Python.ahk", text="General") ctx.hotkey("F13", ahk.message_box, "Context-specific") print("ok00") child_ahk.popen_code(code) child_ahk.wait(0) settings.win_delay = 0 general_windows = ahk.windows.filter(title="Python.ahk", text="General") context_windows = ahk.windows.filter(title="Python.ahk", text="Context-specific") assert not general_windows.exist() ahk.send("{F13}") assert general_windows.wait(timeout=1) assert not context_windows.exist() assert general_windows.get_active() ahk.send("{F13}") assert context_windows.wait(timeout=1) assert len(general_windows) == 1 assert general_windows.exist() assert not general_windows.get_active() ahk.send("{F13}") assert_equals_eventually(general_windows.__len__, 2) ahk.send("{F24}")
def test_block_input_while_sending(child_ahk, notepad): def hotkeys(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) @ahk.hotkey("F15") def block(): with ahk.block_input_while_sending(): print("ok01") ahk.mouse_move(100, 100, relative_to="cursor", mode="event") ahk.mouse_move(-100, -100, relative_to="cursor", mode="event") print("ok02") print("ok00") child_ahk.popen_code(hotkeys) child_ahk.wait(0) edit = notepad.get_control("Edit1") ahk.send("{F15}") child_ahk.wait(1) ahk.sleep(0.1) ahk.send("{Text}Hello!") assert edit.text == "" child_ahk.wait(2) ahk.send("{Text}Hello!") ahk.sleep(0.1) assert edit.text == "Hello!" ahk.send("{F24}")
def test_exclude_window_context(child_ahk, settings): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.message_box, "General", max_threads=2) ahk.hotkey("F14", ahk.message_box, "Extra") ctx = ahk.windows.filter(title="Python.ahk").exclude(text="General").window_context() # If there are any AutoHotkey windows beside General. ctx.hotkey("F13", ahk.message_box, "Context-specific") print("ok00") child_ahk.popen_code(code) child_ahk.wait(0) settings.win_delay = 0 general_windows = ahk.windows.filter(title="Python.ahk", text="General") non_general_windows = ahk.windows.filter(title="Python.ahk").exclude(text="General") extra_windows = ahk.windows.filter(title="Python.ahk", text="Extra") context_windows = ahk.windows.filter(title="Python.ahk", text="Context-specific") assert not non_general_windows.exist() ahk.send("{F13}") assert general_windows.wait(timeout=1) assert not context_windows.exist() ahk.send("{F14}") assert extra_windows.wait(timeout=1) assert non_general_windows.exist() ahk.send("{F13}") assert context_windows.wait(timeout=1) ahk.send("{F24}")
def test_update_separator(child_ahk): def code(): import ahkpy as ahk menu = ahk.Menu() menu.add_separator() menu.update(0, new_name="Test", callback=lambda: print("ok01")) print("ok00") menu.show() child_ahk.popen_code(code) child_ahk.wait(0) ahk.sleep(0) ahk.send("{Down}{Enter}") child_ahk.wait(1)
def test_line_stuff(self, notepad, edit: ahk.Control): edit.text = "" assert edit.line_count == 1 edit.text = "0\r\n1" assert edit.line_count == 2 edit.text = "0\r\n1\r\n" assert edit.line_count == 3 assert edit.current_line_number == 0 assert edit.current_column == 0 edit.send("{Right}") assert edit.current_column == 1 assert edit.current_line == "0" edit.send("{Down}") assert edit.current_line_number == 1 assert edit.current_line == "1" assert edit.current_column == 1 edit.send("{Left}") assert edit.current_column == 0 assert edit.get_line(0) == "0" assert edit.get_line(1) == "1" assert edit.get_line(2) == "" assert edit.get_line(-1) == "" assert edit.get_line(-2) == "1" assert edit.get_line(-3) == "0" with pytest.raises(ahk.Error, match="line number out of range"): assert edit.get_line(3) with pytest.raises(ahk.Error, match="line number out of range"): assert edit.get_line(4) with pytest.raises(ahk.Error, match="line number out of range"): assert edit.get_line(-5) assert edit.selected_text == "" assert ahk.windows.get_active().id == notepad.id ahk.send("+{Right}") assert edit.selected_text == "1" ahk.send("^a") assert_equals_eventually(lambda: edit.selected_text, "0\r\n1\r\n")
def test_timer_returns(child_ahk): def timers(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.set_countdown(0.01, object) print("ok00") child_ahk.popen_code(timers) child_ahk.wait(0) assert not ahk.windows.wait( title="Python.ahk", text="Error: cannot convert '<object object", timeout=0.1, ) ahk.send("{F24}")
def test_restart(child_ahk, tmpdir): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.restart) ahk.message_box("ok00") script = tmpdir / "code.py" script.write(child_ahk.extract_code(code)) child_ahk.popen([script]) assert ahk.windows.wait(title="Python.ahk", text="ok00", timeout=2) ahk.send("{F13}") assert ahk.windows.wait_close(title="Python.ahk", text="ok00", timeout=1) assert ahk.windows.wait(title="Python.ahk", text="ok00", timeout=1) ahk.send("{F24}")
def win1(self, child_ahk): def window(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.message_box("win1", title="ahkpy win1") child_ahk.popen_code(window) win1 = ahk.windows.wait(title="ahkpy win1") assert win1 with ahk.local_settings() as settings: settings.win_delay = 0 yield win1 ahk.send("{F24}")
def test_reenable_on_init(request): hk = ahk.hotkey("F13", lambda: None) request.addfinalizer(hk.disable) hk.disable() called = False @ahk.hotkey("F13") def hk2(): nonlocal called called = True request.addfinalizer(hk2.disable) assert not called ahk.send("{F13}", level=1) ahk.sleep(0) assert called
def test_hotstring_returns(self, child_ahk): def hotstrings(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotstring("517", object) print("ok00") child_ahk.popen_code(hotstrings) child_ahk.wait(0) ahk.send_event("517 ") assert not ahk.windows.wait( title="Python.ahk", text="Error: cannot convert '<object object", timeout=0.1, ) ahk.send("{F24}")
def set_keyboard_layout(language_code): # This depends on having only two layouts in the Windows language list. # Inspired by GetCurrentLocale and ActiveWindow functions # https://github.com/BladeMight/Mahou/blob/9423423163/Mahou/Classes/Locales.cs#L20. # TODO: Handle Command Prompt windows. active_win = ahk.windows.get_active() focused_control = active_win.get_focused_control() or active_win if not focused_control: return thread_id = windll.user32.GetWindowThreadProcessId(focused_control.id, 0) if not thread_id: return current_layout = windll.user32.GetKeyboardLayout(thread_id) current_layout &= 0xffff if current_layout != language_code: ahk.send("#{Space}")
def test_hotkey_returns(child_ahk): def hotkeys(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", object) # object() cannot be converted to an AHK value print("ok00") child_ahk.popen_code(hotkeys) child_ahk.wait(0) ahk.send_event("{F13}", level=10) # Must not raise an error assert not ahk.windows.wait( title="Python.ahk", text="Error: cannot convert '<object object", timeout=0.1, ) ahk.send("{F24}")
def switch_between_app_windows(): active_win = ahk.windows.get_active() if active_win.class_name == "MultitaskingViewFrame": # Alt+Tab window is active. Pressing Alt+Escape should close it without # switching the windows. ahk.send("!{Escape}") return process_name = active_win.process_name app_windows = [ win # Filter all windows to find windows on all virtual desktops. for win in ahk.all_windows.filter(exe=process_name) if is_alt_tab_window(win) ] if len(app_windows) < 2: return global app_win_index app_win_index = min(app_win_index + 1, len(app_windows) - 1) app_windows[app_win_index].activate()
def test_hotkey_context(child_ahk): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.message_box, "Beep") ctx = ahk.HotkeyContext( lambda: ahk.windows.get_active(title="Python.ahk", text="Beep")) ctx.hotkey("F13", ahk.message_box, "Boop") print("ok00") child_ahk.popen_code(code) child_ahk.wait(0) beep_windows = ahk.windows.filter(title="Python.ahk", text="Beep") boop_windows = ahk.windows.filter(title="Python.ahk", text="Boop") ahk.send("{F13}") assert beep_windows.wait(timeout=1) assert not boop_windows.exist() ahk.send("{F13}") assert boop_windows.wait(timeout=1) assert beep_windows.exist() ahk.send("{F24}")
def test_suspend(child_ahk): def code(): import ahkpy as ahk import sys ahk.hotkey("F13", print, "ok01") @ahk.hotkey("F14") def sus(): ahk.suspend() print("ok02") ahk.set_countdown(0.5, sys.exit) print("ok00") proc = child_ahk.popen_code(code) child_ahk.wait(0) ahk.send("{F13}") child_ahk.wait(1) ahk.send("{F14}") child_ahk.wait(2) ahk.send("{F13}") proc.wait() assert proc.stdout.read() == ""
def test_window_context(child_ahk, settings): def code(): import ahkpy as ahk import sys ahk.hotkey("F24", sys.exit) ahk.hotkey("F13", ahk.message_box, "General") ctx = ahk.windows.window_context(title="Python.ahk", text="General") ctx.hotkey("F13", ahk.message_box, "Context-specific") print("ok00") child_ahk.popen_code(code) child_ahk.wait(0) settings.win_delay = 0 general_windows = ahk.windows.filter(title="Python.ahk", text="General") context_windows = ahk.windows.filter(title="Python.ahk", text="Context-specific") assert not general_windows.exist() ahk.send("{F13}") assert general_windows.wait(timeout=1) assert not context_windows.exist() assert general_windows.exist() ahk.send("{F13}") assert context_windows.wait(timeout=1) ahk.send("{F24}")