Skip to content

Commit ec92a87

Browse files
committed
Disable UIA text change events outside Windows Console and Terminal
1 parent 737f7e1 commit ec92a87

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

‎source/UIAHandler/__init__.py‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
"Shell_SystemDialog", # Various dialogs in Windows 10 Settings app
100100
]
101101

102+
textChangeUIAClassNames = (
103+
"ConsoleWindowClass",
104+
"TermControl",
105+
"TermControl2"
106+
)
107+
102108
NVDAUnitsToUIAUnits: Dict[str, int] = {
103109
textInfos.UNIT_CHARACTER: UIA.TextUnit_Character,
104110
textInfos.UNIT_WORD: UIA.TextUnit_Word,
@@ -206,11 +212,9 @@
206212
autoSelectDetectionAvailable = False
207213
if winVersion.getWinVer() >= winVersion.WIN10:
208214
UIAEventIdsToNVDAEventNames.update({
209-
UIA.UIA_Text_TextChangedEventId: "textChange",
210215
UIA.UIA_Text_TextSelectionChangedEventId: "caret",
211216
})
212217
localEventHandlerGroupUIAEventIds.update({
213-
UIA.UIA_Text_TextChangedEventId,
214218
UIA.UIA_Text_TextSelectionChangedEventId,
215219
})
216220
autoSelectDetectionAvailable = True
@@ -421,21 +425,41 @@ def _registerGlobalEventHandlers(self):
421425
def _createLocalEventHandlerGroup(self):
422426
if isinstance(self.clientObject, UIA.IUIAutomation6):
423427
self.localEventHandlerGroup = self.clientObject.CreateEventHandlerGroup()
428+
self.localEventHandlerGroupWithTextChanges = self.clientObject.CreateEventHandlerGroup()
424429
else:
425430
self.localEventHandlerGroup = utils.FakeEventHandlerGroup(self.clientObject)
431+
self.localEventHandlerGroupWithTextChanges = utils.FakeEventHandlerGroup(self.clientObject)
426432
self.localEventHandlerGroup.AddPropertyChangedEventHandler(
427433
UIA.TreeScope_Ancestors | UIA.TreeScope_Element,
428434
self.baseCacheRequest,
429435
self,
430436
*self.clientObject.IntSafeArrayToNativeArray(localEventHandlerGroupUIAPropertyIds)
431437
)
438+
self.localEventHandlerGroupWithTextChanges.AddPropertyChangedEventHandler(
439+
UIA.TreeScope_Ancestors | UIA.TreeScope_Element,
440+
self.baseCacheRequest,
441+
self,
442+
*self.clientObject.IntSafeArrayToNativeArray(localEventHandlerGroupUIAPropertyIds)
443+
)
432444
for eventId in localEventHandlerGroupUIAEventIds:
433445
self.localEventHandlerGroup.AddAutomationEventHandler(
434446
eventId,
435447
UIA.TreeScope_Ancestors | UIA.TreeScope_Element,
436448
self.baseCacheRequest,
437449
self
438450
)
451+
self.localEventHandlerGroupWithTextChanges.AddAutomationEventHandler(
452+
eventId,
453+
UIA.TreeScope_Ancestors | UIA.TreeScope_Element,
454+
self.baseCacheRequest,
455+
self
456+
)
457+
self.localEventHandlerGroupWithTextChanges.AddAutomationEventHandler(
458+
UIA.UIA_Text_TextChangedEventId,
459+
UIA.TreeScope_Ancestors | UIA.TreeScope_Element,
460+
self.baseCacheRequest,
461+
self
462+
)
439463

440464
def addEventHandlerGroup(self, element, eventHandlerGroup):
441465
if isinstance(eventHandlerGroup, UIA.IUIAutomationEventHandlerGroup):
@@ -466,7 +490,12 @@ def func():
466490
if not isStillFocus:
467491
return
468492
try:
469-
self.addEventHandlerGroup(element, self.localEventHandlerGroup)
493+
group = (
494+
self.localEventHandlerGroupWithTextChanges
495+
if element.currentClassName in textChangeUIAClassNames
496+
else self.localEventHandlerGroup
497+
)
498+
self.addEventHandlerGroup(element, group)
470499
except COMError:
471500
log.error("Could not register for UIA events for element", exc_info=True)
472501
else:
@@ -500,7 +529,15 @@ def IUIAutomationEventHandler_HandleAutomationEvent(self,sender,eventID):
500529
if _isDebug():
501530
log.debug("HandleAutomationEvent: Ignored MenuOpenedEvent while focus event pending")
502531
return
503-
NVDAEventName=UIAEventIdsToNVDAEventNames.get(eventID,None)
532+
if eventID == UIA.UIA_Text_TextChangedEventId:
533+
if sender.currentClassName in textChangeUIAClassNames:
534+
NVDAEventName = "textChange"
535+
else:
536+
if _isDebug():
537+
log.debugWarning(f"HandleAutomationEvent: Dropping unused textChange event")
538+
return
539+
else:
540+
NVDAEventName = UIAEventIdsToNVDAEventNames.get(eventID, None)
504541
if not NVDAEventName:
505542
if _isDebug():
506543
log.debugWarning(f"HandleAutomationEvent: Don't know how to handle event {eventID}")

0 commit comments

Comments
 (0)