def code(): import ahkpy as ahk import threading ahk.set_countdown(0.1, print, 1) threading.Timer(0.2, lambda: print(2)).start() ahk.sleep(0.3) print(3)
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 assert_equals_eventually(func, expected, timeout=1): stop = time.perf_counter() + timeout while time.perf_counter() < stop: actual = func() if actual == expected: return ahk.sleep(0.01) assert actual == expected
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()
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_refcounts(request): func = lambda: None # noqa: E731 timer = ahk.set_countdown(1, func) request.addfinalizer(timer.stop) func_refcount = sys.getrefcount(func) timer.stop() assert sys.getrefcount(func) == func_refcount - 1 timer = ahk.set_countdown(0.01, func) func_refcount = sys.getrefcount(func) ahk.sleep(0.01) assert sys.getrefcount(func) == func_refcount - 1
def test_top_bottom(self, msg_boxes, win1): assert len(msg_boxes) == 2 win1.bring_to_top() assert msg_boxes.first() == win1 win1.send_to_bottom() ahk.sleep(.1) assert msg_boxes.last() == win1 win1.bring_to_top() assert msg_boxes.first() == win1
def test_status_bar(notepad): ahk.sleep(0.2) assert "Ln 1, Col 1" in notepad.get_status_bar_text(1) notepad.send("q") ahk.sleep(0) assert "Ln 1, Col 2" in notepad.get_status_bar_text(1) ahk.set_countdown(0.5, notepad.send, "q") assert notepad.wait_status_bar(" Ln 1, Col x", part=1, timeout=0.1) is False assert notepad.wait_status_bar(" Ln 1, Col 3", part=1, timeout=1) is True assert notepad.get_status_bar_text(1) == " Ln 1, Col 3"
def test_on_clipboard_change(request): stored = ahk.get_clipboard() request.addfinalizer(lambda: ahk.set_clipboard(stored)) history = [] @ahk.on_clipboard_change def handler(clipboard): history.append(clipboard) request.addfinalizer(handler.unregister) ahk.set_clipboard("") ahk.sleep(0) assert history == [""] history.clear() ahk.set_clipboard("hello") ahk.sleep(0) assert history == ["hello"] @ahk.on_clipboard_change(prepend_handler=True) def prepended_handler(clipboard): history.append(clipboard.upper()) request.addfinalizer(prepended_handler.unregister) history.clear() ahk.set_clipboard("hey") ahk.sleep(0) assert history == ["HEY", "hey"] @ahk.on_clipboard_change() def exclamator(clipboard): history.append(clipboard + "!!") request.addfinalizer(exclamator.unregister) history.clear() ahk.set_clipboard("yay") ahk.sleep(0) assert history == ["YAY", "yay", "yay!!"] history.clear() handler_func = handler.func handler_func_refcount = sys.getrefcount(handler_func) handler.unregister() assert sys.getrefcount(handler_func) == handler_func_refcount - 1 ahk.set_clipboard("hello again") ahk.sleep(0.1) assert history == ["HELLO AGAIN", "hello again!!"]
def test_change_periodic(request): times = [] timer = ahk.set_timer(0.1, times.append, 1) request.addfinalizer(timer.stop) ahk.sleep(0.29) assert len(times) == 2 times.clear() timer.update(periodic=False) ahk.sleep(0.29) assert len(times) == 1
def test_countdown_start(request): times = [] timer = ahk.set_countdown(1, times.append, 1) request.addfinalizer(timer.stop) timer.start(interval=0.1) # Restart a non-finished countdown ahk.sleep(0.11) assert len(times) == 1 timer.start() # Start a finished countdown with its previous interval ahk.sleep(0.11) assert len(times) == 2
def test_focus(self, find_dialog): edit = find_dialog.get_control("Edit1") focused_control = find_dialog.get_focused_control() assert focused_control == edit assert focused_control.class_name == "Edit" assert focused_control.is_focused is True match_case_button = find_dialog.get_control("Button2") match_case_button.focus() ahk.sleep(0.01) focused_control = find_dialog.get_focused_control() assert focused_control == match_case_button assert match_case_button.is_focused is True assert edit.is_focused is False
def test_settings_bleed(settings): settings.win_delay = 0.1 win_delays = [] def f(): local = ahk.local_settings().activate() win_delays.append(ahk.get_settings().win_delay) local.win_delay = 0 # This must not affect other callbacks ahk.sleep(0.02) ahk.set_countdown(0.01, f) ahk.set_countdown(0.02, f) ahk.sleep(0.03) assert win_delays == [0.1, 0.1]
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_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_send_level(child_ahk): with pytest.raises(ValueError, match="level must be between 0 and 100"): ahk.send("{F13}", level=-1) with pytest.raises(ValueError, match="level must be between 0 and 100"): ahk.send("{F13}", level=101) called = False @ahk.hotkey("F15", input_level=10) def f15(): nonlocal called called = True # Use SendEvent here because hotkeys with input_level > 0 use keyboard hook # and SendInput temporarily disables that hook. ahk.send_event("{F15}") ahk.sleep(0) # Let AHK process the hotkey. assert not called ahk.send_event("{F15}", level=20) ahk.sleep(0) assert called
def test_update_options(request): calls = [] hk = ahk.hotkey("F13", calls.append, "F13") request.addfinalizer(hk.disable) ahk.send_event("{F13}", level=10) ahk.sleep(0) assert calls == ["F13"] calls.clear() hk.update(input_level=20) ahk.send_event("{F13}", level=10) ahk.sleep(0) assert calls == [] ahk.send_event("{F13}", level=21) ahk.sleep(0) assert calls == ["F13"]
def test_timer_update(request): times = [] timer = ahk.set_timer(1, times.append, 1) request.addfinalizer(timer.stop) timer.update(interval=0.1) ahk.sleep(0.59) timer.stop() assert len(times) == 5 times.clear() assert timer.interval == 0.1 timer.start() ahk.sleep(0.06) timer.update(priority=40) # Updating priority should not restart the timer ahk.sleep(0.06) assert len(times) == 1
def test_timeout(request): t = ahk.ToolTip("test") request.addfinalizer(t.hide) tooltips = ahk.windows.filter(class_name="tooltips_class32", pid=os.getpid()) t.show(timeout=0.1) assert tooltips.exist() ahk.sleep(0.11) assert not tooltips.exist() t.show(timeout=0.1) t.show() # This must cancel the timeout. assert tooltips.exist() ahk.sleep(0.11) assert tooltips.exist() t.show() t.show(timeout=0.1) assert tooltips.exist() ahk.sleep(0.11) assert not tooltips.exist() t.show(timeout=0.1) ahk.sleep(0.06) t.show(timeout=0.1) # This must reset the timeout. assert tooltips.exist() ahk.sleep(0.06) assert tooltips.exist() ahk.sleep(0.06) assert not tooltips.exist()
def f(): local = ahk.local_settings().activate() win_delays.append(ahk.get_settings().win_delay) local.win_delay = 0 # This must not affect other callbacks ahk.sleep(0.02)
def code(): import ahkpy as ahk ahk.set_countdown(0.1, print, 1) ahk.sleep(0.2) # sleep longer than the countdown print(2)
def sleeper(loop): ahk.sleep(0.01) loop.call_soon(sleeper, loop)
def slow_handler(): ahk.sleep(1) return 42
def code(): import ahkpy as ahk ahk.sleep(0.1) ahk.set_clipboard("hello from ahk")
def block(): with ahk.block_input(): ahk.sleep(0.1) print("ok01")