Skip to content

How to programmatically set keyboard focus to the web view (without requiring an initial mouse click)? #25

@davidroth

Description

@davidroth

Hi!

Congrats for open sourcing this library!

I am migrating a Blazor Server application hosted in Electron to Avalonia, using NativeWebDialog as the main window wrapper. Everything loads correctly, but I am running into a strict focus issue.

When the dialog opens and the web page loads, the web view does not receive keyboard events (e.g., typing in a textbox or global key-down events). To get keyboard events to register, I have to physically click anywhere inside the loaded web page first. After that initial click, it works perfectly.

Is there a recommended way to programmatically hand over the OS-level keyboard focus to the embedded web view upon launch?

I attempted to workaround this by grabbing the native window handle to forcefully invoke SetFocus via user32.dll. However, calling TryGetPlatformHandle() on the dialog/window consistently returns null (tested on Windows), blocking this workaround.

A built-in, simple API (e.g., dialog.FocusWebView()) would be highly preferable to ensure a consistent cross-platform experience without forcing developers to drop into native interop for a standard UI requirement.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions