Caret service

    Read and position the caret in the current editing view. Use it to place inserted content or restore a visible authoring position.

    The facade is available as editor.Caret; its interface can be replaced by an application implementation.

    Avalonia HTML Editor with complete native toolbars and a readable project update document.
    Avalonia HTML Editor with complete native toolbars and a readable project update document.

    Use it in your application

    Text indices are UTF-16 units in the API-defined current view. They are not positions in an encoded HTML file or interchangeable with legacy MSHTML ranges.

    All examples assume an editor named editor, already attached to a visible window, and an awaited editor.Ready. Keep operations asynchronous instead of blocking the UI thread.

    await editor.Caret.SetCaretAtEndAsync();
    int index = await editor.Caret.GetCaretIndexAsync();
    

    Members

    These declarations come from the package's own XML documentation. Await operations and supply cancellation tokens where your application needs them.

    GetCaretBoundsAsync Method

    Returns the actual document-relative caret rectangle in DIP units, or null when unrealized.

    Syntax
    Task<Rect?> GetCaretBoundsAsync(CancellationToken cancellationToken = default)
    

    GetCaretIndexAsync Method

    Returns the actual current-view UTF-16 caret index, or -1 when unavailable.

    Syntax
    Task<int> GetCaretIndexAsync(CancellationToken cancellationToken = default)
    

    GetCaretIndexInHtmlAsync Method

    Returns a safely mapped body-markup UTF-16 index in the rendered document, or the visible draft index in source mode; -1 indicates unavailable mapping.

    Syntax
    Task<int> GetCaretIndexInHtmlAsync(CancellationToken cancellationToken = default)
    

    GetCaretPositionAsync Method

    Returns the actual document-relative caret location, or null when no caret geometry is available.

    Syntax
    Task<Point?> GetCaretPositionAsync(CancellationToken cancellationToken = default)
    

    HideAsync Method

    Hides the actual caret without moving it or changing content.

    Syntax
    Task HideAsync(CancellationToken cancellationToken = default)
    

    ScrollToCaretViewAsync Method

    Reveals the actual caret without changing its offset or the document.

    Syntax
    Task ScrollToCaretViewAsync(CancellationToken cancellationToken = default)
    

    SetCaretAtAsync Method

    Places the caret at a finite document point in DIP coordinates, optionally revealing it.

    Syntax
    Task SetCaretAtAsync(double x, double y, bool scrollToView = true, CancellationToken cancellationToken = default)
    

    SetCaretAtEndAsync Method

    Moves to the end of the actual current-view document.

    Syntax
    Task SetCaretAtEndAsync(CancellationToken cancellationToken = default)
    

    SetCaretAtHomeAsync Method

    Moves to the start of the actual current-view document.

    Syntax
    Task SetCaretAtHomeAsync(CancellationToken cancellationToken = default)
    

    SetCaretAtLineBeginningAsync Method

    Moves to the beginning of the caret's current visual line, honoring wrapping.

    Syntax
    Task SetCaretAtLineBeginningAsync(CancellationToken cancellationToken = default)
    

    SetCaretAtLineEndAsync Method

    Moves to the end of the caret's current visual line, honoring wrapping.

    Syntax
    Task SetCaretAtLineEndAsync(CancellationToken cancellationToken = default)
    

    SetCaretByIndexAsync Method

    Collapses the current-view selection at a bounded UTF-16 offset.

    Syntax
    Task SetCaretByIndexAsync(int index, CancellationToken cancellationToken = default)
    

    Selects a bounded current-view UTF-16 range without editing the document or draft.

    Syntax
    Task SetCaretByIndexAsync(int index, int length, CancellationToken cancellationToken = default)
    

    SetCaretPositionAsync Method

    Places the caret at the same document point as SetCaretAtAsync.

    Syntax
    Task SetCaretPositionAsync(double x, double y, bool scrollToView = true, CancellationToken cancellationToken = default)
    

    ShowAsync Method

    Shows the actual caret, optionally scrolling it into the current viewport.

    Syntax
    Task ShowAsync(bool scrollToView = true, CancellationToken cancellationToken = default)
    

    Related guides

    Last updated on Oct 30, 2026

    Put this into practice.

    Start with the C# Avalonia sample. Restore this edition's package and install the native browser prerequisites before running it.

    Prefer a guided look? Book a free live demo with our engineers - live on Zoom or Teams, never a chatbot.