A matching process ID does not mean a WebView2 control has keyboard focus. A PID identifies a process; focus belongs to GUI window and thread state. To track a WebView2 control, use its controller’s GotFocus and LostFocus events. Use Windows window and thread APIs only when you need diagnostic context, and interpret each API according to its scope.
Why matching PIDs cannot prove WebView2 focus
CoreWebView2.BrowserProcessId identifies the browser process hosting the WebView; Microsoft does not document it as a focus property (Microsoft Learn: CoreWebView2.BrowserProcessId). By contrast, GetForegroundWindow() returns an HWND for the foreground window, while GetWindowThreadProcessId() returns the thread that created a specified window and can also provide its creator PID (GetForegroundWindow; GetWindowThreadProcessId).
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Competitive Programming 4 - Book 2: The Lower Bound of Programming Contests in the 2020s | $24.00 | Buy on Amazon |
| 2 |
|
The C Programming Language | $17.46 | Buy on Amazon |
As an Amazon Associate I earn from qualifying purchases.
Comparing the browser-process PID with a PID obtained from a foreground window can tell you that the identifiers match. It cannot establish that the WebView2 controller has keyboard focus: it does not identify which control within the process owns focus. Windows processes can have multiple threads, and threads can create multiple windows, so reducing window and thread state to one process identifier loses the context needed for a focus verdict (Microsoft Learn: Processes and Threads). Treat PID equality as an identity observation, not a hasFocus result.
Use WebView2 controller events to track the control
The CoreWebView2Controller provides the focus events for its hosted WebView. Microsoft documents that “GotFocus is raised when WebView gets focus.” Subscribe to GotFocus and LostFocus on the relevant controller and associate the events with that controller instance (Microsoft Learn: CoreWebView2Controller).
#1 Best Overall
Tab navigation needs a little care. When the user attempts to tab out, MoveFocusRequested fires while focus is still on WebView2. If the host application handles the request and moves focus to the next or previous host control, LostFocus follows. Use MoveFocus when the host tab order selects the WebView; in the request handler, respond to the requested direction and mark the request handled when the host takes responsibility. This keeps WebView2 and the host’s tab order coordinated.
What each focus check tells you
| Check | What it establishes | Scope and limitation |
|---|---|---|
GotFocus / LostFocus |
The WebView2 controller reports focus transitions for its WebView. | Preferred signal for that control. On tab-out, LostFocus follows the host’s focus move. |
GetForegroundWindow() |
The window handle currently considered the foreground window. | Foreground-window state does not by itself identify the focused child control; the function may return NULL during an activation transition. |
GetWindowThreadProcessId(hwnd, &pid) |
The creator thread ID for a specified window and, through the optional output, its creator PID. | Identifies the window’s creator, not whether a WebView2 control has focus. |
GetFocus() |
The focused window handle for the calling thread’s message queue. | Not a global query. Another GUI thread’s queue may own keyboard focus. |
| PID equality | The compared process identifiers have the same value. | Does not establish which window or control has keyboard focus. |
Inspect Windows focus state without overreading it
Use the Windows APIs to understand the window and thread context around a focus event, not as a substitute for the controller’s focus events.
Rank #2
- Capture the foreground window: Call
GetForegroundWindow()and retain itsHWND. It can returnNULLduring activation changes, so record that as a transient or unknown observation rather than treating it as evidence for a PID-based focus result (Microsoft Learn: GetForegroundWindow). - Inspect that window’s creator: Pass the handle to
GetWindowThreadProcessId(hwnd, &pid). The function’s return value is the creator thread ID; the optional output receives the creator PID. A zero return indicates an invalid window handle (Microsoft Learn: GetWindowThreadProcessId). - Check the calling thread’s focus, if that is what you need:
GetFocus()returns the focusedHWNDfor the calling thread’s queue, orNULL. It does not report focus across all GUI threads (Microsoft Learn: GetFocus). - Query another GUI thread’s queue when necessary: Use
GetGUIThreadInfofor the foreground or another thread’s GUI state rather than assumingGetFocus()is system-wide (Microsoft Learn: GetGUIThreadInfo).
Keep the values distinct in logs: controller focus-event state, foreground HWND, focused HWND from the relevant GUI-thread query, creator thread ID, and creator PID. This makes it clear which observation describes focus and which describes identity.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsQuick Recap
Verify focus behavior with a short event trace
- Subscribe to
GotFocus,LostFocus, andMoveFocusRequestedon the controller you intend to observe. Log the event, its order, and the controller instance. - Use Tab to enter the WebView, Tab to leave it, and Shift+Tab to return. When
MoveFocusRequestedoccurs, verify that the host responds to the requested direction and that the subsequent focus events match the host’s navigation. - During activation changes, sample
GetForegroundWindow()and allow for a temporaryNULLresult. - If diagnosing focus owned by another GUI thread, query that thread’s state with
GetGUIThreadInforather than drawing conclusions fromGetFocus()on your own thread. - Compare the WebView2 event trace with the window and thread observations. Do not treat equal PIDs as proof that the WebView control is focused.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




