Skip to content

Wait

flaui.core.input.Wait

Various helper tools used in various places, wrapper over Wait class in FlaUI.Core.Input namespace

until_input_is_processed(wait_timeout_in_secs=None) staticmethod

Waits a little to allow inputs (mouse, keyboard, ...) to be processed.

Parameters:

Name Type Description Default
wait_timeout_in_secs Optional[float]

An optional timeout. If no value or None is passed, the timeout is 100ms.

None
Source code in flaui/core/input.py
@staticmethod
def until_input_is_processed(wait_timeout_in_secs: Optional[float] = None) -> None:
    """
    Waits a little to allow inputs (mouse, keyboard, ...) to be processed.

    :param wait_timeout_in_secs: An optional timeout. If no value or None is passed, the timeout is 100ms.
    """
    wait_time = wait_timeout_in_secs if wait_timeout_in_secs is not None else 0.1  # default to 100ms
    time.sleep(wait_time)

until_responsive(automation_element, timeout_in_secs=None) staticmethod

Wait until the window that owns the element accepts input again.

Mirrors C# Wait.UntilResponsive(AutomationElement, TimeSpan).

Parameters:

Name Type Description Default
automation_element AutomationElement

The element (usually a window) to wait for. A raw C# element works too.

required
timeout_in_secs Optional[float]

How long to wait, in seconds, defaults to Wait.DEFAULT_TIMEOUT (1 s).

None

Returns:

Type Description
bool

True if the element became responsive in time, False otherwise.

Source code in flaui/core/input.py
@staticmethod
def until_responsive(automation_element: AutomationElement, timeout_in_secs: Optional[float] = None) -> bool:
    """Wait until the window that owns the element accepts input again.

    Mirrors C# ``Wait.UntilResponsive(AutomationElement, TimeSpan)``.

    :param automation_element: The element (usually a window) to wait for. A raw C# element works too.
    :param timeout_in_secs: How long to wait, in **seconds**, defaults to ``Wait.DEFAULT_TIMEOUT`` (1 s).
    :return: True if the element became responsive in time, False otherwise.
    """
    from System import TimeSpan  # pyright: ignore

    raw = getattr(automation_element, "raw_element", automation_element)
    seconds = timeout_in_secs if timeout_in_secs is not None else Wait.DEFAULT_TIMEOUT
    return bool(CSWait.UntilResponsive(raw, TimeSpan.FromSeconds(seconds)))

until_responsive_hwnd(hWnd, timeout=None) staticmethod

Wait until the window with the given native handle accepts input again.

Mirrors C# Wait.UntilResponsive(IntPtr, TimeSpan).

Parameters:

Name Type Description Default
hWnd Any

The window handle, as an int (e.g. app.main_window_handle) or IntPtr.

required
timeout Optional[float]

How long to wait, in seconds, defaults to Wait.DEFAULT_TIMEOUT (1 s).

None

Returns:

Type Description
bool

True if the window became responsive in time, False otherwise.

Source code in flaui/core/input.py
@staticmethod
def until_responsive_hwnd(hWnd: Any, timeout: Optional[float] = None) -> bool:
    """Wait until the window with the given native handle accepts input again.

    Mirrors C# ``Wait.UntilResponsive(IntPtr, TimeSpan)``.

    :param hWnd: The window handle, as an ``int`` (e.g. ``app.main_window_handle``) or ``IntPtr``.
    :param timeout: How long to wait, in **seconds**, defaults to ``Wait.DEFAULT_TIMEOUT`` (1 s).
    :return: True if the window became responsive in time, False otherwise.
    """
    from System import IntPtr, TimeSpan  # pyright: ignore

    handle = hWnd if isinstance(hWnd, IntPtr) else IntPtr(int(hWnd))
    seconds = timeout if timeout is not None else Wait.DEFAULT_TIMEOUT
    return bool(CSWait.UntilResponsive(handle, TimeSpan.FromSeconds(seconds)))

while_cursor_is_busy(timeout_in_secs=30.0, poll_interval_secs=0.1) staticmethod

Wait while the global mouse cursor shows a busy/loading state.

Many large desktop applications show the wait (hourglass/spinner) or app-starting cursor while a new window loads its data. Calling this after navigating between screens avoids the sync issues that WaitWhileBusy / UntilResponsive miss, by polling the actual cursor the OS is showing until it returns to a non-busy shape (or the timeout elapses).

Implemented in pure Python via Win32 GetCursorInfo (no C# dependency), per the project's preference for Python for non-core system utilities.

Parameters:

Name Type Description Default
timeout_in_secs float

Maximum time to wait for the cursor to become idle.

30.0
poll_interval_secs float

How often to poll the cursor state, in seconds.

0.1

Returns:

Type Description
bool

True if the cursor became idle within the timeout, False if it timed out while still busy. Also returns True when the cursor state cannot be read.

Source code in flaui/core/input.py
@staticmethod
def while_cursor_is_busy(timeout_in_secs: float = 30.0, poll_interval_secs: float = 0.1) -> bool:
    """Wait while the global mouse cursor shows a busy/loading state.

    Many large desktop applications show the *wait* (hourglass/spinner) or *app-starting* cursor
    while a new window loads its data. Calling this after navigating between screens avoids the
    sync issues that ``WaitWhileBusy`` / ``UntilResponsive`` miss, by polling the actual cursor
    the OS is showing until it returns to a non-busy shape (or the timeout elapses).

    Implemented in pure Python via Win32 ``GetCursorInfo`` (no C# dependency), per the project's
    preference for Python for non-core system utilities.

    :param timeout_in_secs: Maximum time to wait for the cursor to become idle.
    :param poll_interval_secs: How often to poll the cursor state, in seconds.
    :return: ``True`` if the cursor became idle within the timeout, ``False`` if it timed out
        while still busy. Also returns ``True`` when the cursor state cannot be read.
    """
    import ctypes
    from ctypes import wintypes

    class _POINT(ctypes.Structure):
        """Win32 ``POINT`` structure."""

        _fields_ = [("x", wintypes.LONG), ("y", wintypes.LONG)]

    class _CURSORINFO(ctypes.Structure):
        """Win32 ``CURSORINFO`` structure."""

        _fields_ = [
            ("cbSize", wintypes.DWORD),
            ("flags", wintypes.DWORD),
            ("hCursor", wintypes.HANDLE),
            ("ptScreenPos", _POINT),
        ]

    try:
        user32 = ctypes.windll.user32  # type: ignore[attr-defined]
    except (AttributeError, OSError):
        return True  # Not on Windows or no user32 — nothing to wait on.

    idc_wait, idc_appstarting = 32514, 32650  # IDC_WAIT, IDC_APPSTARTING
    busy_cursors = {user32.LoadCursorW(None, idc_wait), user32.LoadCursorW(None, idc_appstarting)}
    busy_cursors.discard(0)

    def _is_busy() -> bool:
        """Return whether the OS is currently showing a busy cursor."""
        info = _CURSORINFO()
        info.cbSize = ctypes.sizeof(_CURSORINFO)
        if not user32.GetCursorInfo(ctypes.byref(info)):
            return False  # Cannot read cursor state; treat as not busy.
        return info.hCursor in busy_cursors

    deadline = time.monotonic() + timeout_in_secs
    while _is_busy():
        if time.monotonic() >= deadline:
            return False
        time.sleep(poll_interval_secs)
    return True