Focus stays on the window instead of a RichEdit control after Alt+Tab back to it

Open
#20,994 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

The focus-event flow lives in the MSAA handling code, including processFocusWinEvent and OrderedWinEventLimiter.flushEvents in the IAccessibleHandler package, with _shouldGetEvents deferring foreground events. Start by reproducing the RichEdit focus loss with the limiter's maxFocusItems lowered, then check the log for the dropped OBJID_CLIENT focus event. Done means focus lands on the edit field after Alt+Tab, verified across repeated returns.

Written by the indexing model from the issue text.

Description

needs-technical-investigation p3 triaged
Steps to reproduce
  1. Restart NVDA with add-ons disabled.
  2. Open a window with a read-only rich edit control. It reproduces both in a separate wxPython process and inside NVDA's process. The quickest way is the Python console (NVDA+control+z):
import wx
f = wx.Frame(None, title="Focus repro")
t = wx.TextCtrl(f, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH)
t.SetValue("text")
f.Show(); f.Maximize(); t.SetFocus()
  1. Alt+Tab to another window, then Alt+Tab back.
  2. Press the arrow keys.
  3. Repeat steps 3 and 4 a few times.

To make it reproduce more often, lower the focus limit first:

import IAccessibleHandler.internalWinEventHandler as h; h.winEventLimiter.maxFocusItems = 2
Actual behavior

NVDA announces only the window title. NVDA+tab reports the window, not the edit field. The caret does move (Windows beeps at the first/last line), but NVDA doesn't read it.

Switching back with Windows+number doesn't reproduce it.

Expected behavior

NVDA reports the edit field and tracks the caret.

NVDA logs, crash dumps and other attachments

MSAA debug log, on return to the window:

  1. In one core cycle the limiter receives: foreground of the wx window, focus on RICHEDIT50W OBJID_CLIENT, a stale foreground of XamlExplorerHostIslandWindow (the task switcher), focus on RICHEDIT50W with a positive objectID (e.g. 6), and two focus events from the task switcher.
  2. The stale foreground triggers _shouldGetEvents foreground deferral for two cycles, so all of these end up in one flush.
  3. OrderedWinEventLimiter.flushEvents keeps only the last maxFocusItems (4) focus/foreground events, so the OBJID_CLIENT focus event is dropped.
  4. The remaining focus event with objectID 6 can't be resolved: AccessibleObjectFromEvent failed ... Could not instantiate an NVDAObject. It looks like a UIA event proxied to a winEvent, while NVDA handles this window via MSAA.[^1]
  5. Only the foreground event is processed, so focus lands on the window.
Hook received winEvent: EVENT_OBJECT_FOCUS, window 1248016 (RICHEDIT50W), objectID OBJID_CLIENT, childID 0, process 15320 (nvda)
Hook received winEvent: EVENT_SYSTEM_FOREGROUND, window 196674 (XamlExplorerHostIslandWindow), objectID OBJID_WINDOW, childID 0, process 7348 (explorer)
Hook received winEvent: EVENT_OBJECT_FOCUS, window 1248016 (RICHEDIT50W), objectID 6, childID 0, process 15320 (nvda)
Foreground still 1051574 (wxWindowNR). Deferring until foreground is 196674 (XamlExplorerHostIslandWindow), defer count 1
Foreground still 1051574 (wxWindowNR). Deferring until foreground is 196674 (XamlExplorerHostIslandWindow), defer count 2
Emitting winEvent EVENT_SYSTEM_FOREGROUND, window 1051574 (wxWindowNR), ...
Emitting winEvent EVENT_OBJECT_FOCUS, window 1248016 (RICHEDIT50W), objectID 6, ...
oleacc.AccessibleObjectFromEvent failed with [WinError -2147467259]. WinEvent: window 1248016 (RICHEDIT50W), objectID 6, childID 0, process 15320 (nvda)
Could not instantiate an NVDAObject for winEvent: EVENT_OBJECT_FOCUS, window 1248016 (RICHEDIT50W), objectID 6, childID 0, process 15320 (nvda)

The NVDA log viewer doesn't show the issue: its onActivate refresh delays the focus change, so the OBJID_CLIENT focus event survives the limiter.[^1]

Possible fix: in processFocusWinEvent, when a focus event with a positive objectID can't be resolved for a non-UIA window that has the focus in its thread, fall back to its OBJID_CLIENT. Tested locally with the window in a separate process: without the fix NVDA landed on the field in 2 of 17 returns, with the fix in 14 of 14. It fixes every return where any focus event of the field survives the limiter. It doesn't help when the limiter drops both focus events of the field.

[^1]: The analysis was done with the help of an AI assistant. The steps above follow from the log, but the statements marked with this footnote are interpretations and may be inaccurate.

System configuration
NVDA installed/portable/running from source

Installed and running from source.

NVDA version

Installed: alpha-58000,a4a89720 (2027.1.0.58000)
Source: source-master-732d1ce (2027.1.0.0)

Windows version

Windows 11 25H2 (AMD64) build 26200.9550

Name and version of other software in use when reproducing the issue

None.

Other information about your system
Other questions
Does the issue still occur after restarting your computer?

Yes

Have you tried any other versions of NVDA? If so, please report their behaviors

Installed and from source, hashes provided above.

If NVDA add-ons are disabled, is your problem still occurring?

Yes.

Does the issue still occur after you run the System Accessibility Repair Tool in NVDA's tools menu?

Yes

Dominant language
Python
Stars
2.7k
Forks
842
Avg merge
2d 4h
Merged PRs (30d)
56

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nvaccess/nvda

All issues in nvaccess/nvda

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.