1+ from typing import Optional , List
12import heapq
23import itertools
34
45import winUser
6+ from . import IAccessibleObjectIdentifierType
7+
58
69MAX_WINEVENTS_PER_THREAD = 10
710
@@ -75,10 +78,15 @@ def addEvent(
7578 self ._genericEventCache [(eventID , window , objectID , childID , threadID )] = next (self ._eventCounter )
7679 return True
7780
78- def flushEvents (self ):
81+ def flushEvents (
82+ self ,
83+ alwaysAllowedObjects : Optional [List [IAccessibleObjectIdentifierType ]] = None
84+ ):
7985 """Returns a list of winEvents that have been added.
8086 Due to limiting, it will not necessarily be all the winEvents that were originally added.
8187 They are definitely guaranteed to be in the correct order though.
88+ winEvents for objects listed in alwaysAllowedObjects will always be emitted,
89+ Even if the winEvent limit for that thread has been exceeded.
8290 @return Tuple[eventID,window,objectID,childID]
8391 """
8492 if self ._lastMenuEvent is not None :
@@ -88,11 +96,17 @@ def flushEvents(self):
8896 self ._genericEventCache = {}
8997 threadCounters = {}
9098 for k , v in sorted (g .items (), key = lambda item : item [1 ], reverse = True ):
99+ # Increase the event count for this thread by 1.
91100 threadCount = threadCounters .get (k [- 1 ], 0 )
92- if threadCount > MAX_WINEVENTS_PER_THREAD :
101+ threadCount += 1
102+ threadCounters [k [- 1 ]] = threadCount
103+ # Find out if this event is for an object whos events are always allowed.
104+ eventsForObjectAlwaysAllowed = alwaysAllowedObjects and k [1 :- 1 ] in alwaysAllowedObjects
105+ if threadCount > MAX_WINEVENTS_PER_THREAD and not eventsForObjectAlwaysAllowed :
106+ # Skip this event if too many events have already been emitted for this thread
107+ # and this event is not for an object whos events are always allowed.
93108 continue
94109 heapq .heappush (self ._eventHeap , (v ,) + k )
95- threadCounters [k [- 1 ]] = threadCount + 1
96110 f = self ._focusEventCache
97111 self ._focusEventCache = {}
98112 for k , v in sorted (f .items (), key = lambda item : item [1 ])[0 - self .maxFocusItems :]:
0 commit comments