Context.window_manager.modal_handler_add(self) not working on other windows

This is the shortest example I could think of to demonstrate my issue.

Based on multiple searches this is not a new, nor unique issue. But I have yet to find a proper explanation and solution for it.

To see it in action:

  1. Save the code snippet as .py file
  2. Open console (Window->Toggle System Console)
  3. Install the add-on (Edit->Preferences…->Add-ons->Install…->[the_saved.py file])
  4. Search for ‘event’ in the search box.
  5. Enable the ‘Window Manager: Listen for events on arbitrary windows’ add-on - Keep the Preferences window open
  6. Move mouse over both the main window (with the 3D scene in it) and the Preferences window. Notice that nothing happens in the console.
  7. Press the ‘Event Listener’ button in the add-on Preferences.
  8. Move mouse around again. Notice that the console fills with timestamps. But only when mouse moves over Preferences window.
  9. Disable and reenable the add-on. (to clear the modal handler)
  10. Press ‘Event Listener’ in the new ‘Event Listener’ menu next to ‘Help’
  11. Move mouse around again. Notice that the console fills again. But now only when mouse is over the main window.
  12. Click the ‘Event Listener’ button in the add-on Preferences. Now the console write the time when the mouse is over either window.
  13. Close the Preferences window and reopen it. Now it doesn’t get events anymore. But the main window still does. (Click the button in the add-on and events will flow again for both windows)

I fail to understand why ‘context.window_manager.modal_handler_add(self)’ or ‘bpy.data.window_managers[0].modal_handler_add(self)’ only registers the modal operator for the window that received the input (a button click in this example. Doing an F3 search and invoking the operator from there has the same effect. Only the window the mouse cursor was over when invoking the operator triggers the modal function). As I understand it there is only one WindowManager. Why is it behaving differently based on what window is active?

And is there a way to register the modal operator for all windows? Well, any window for that matter. Also windows that aren’t opened yet. I mean, the modal is registered on the window manager. Wouldn’t it know to also call the modal operator on new windows? I haven’t told it to only listen on one specific window…

bl_info = {
    'name': 'Listen for events on arbitrary windows',
    'author': 'Peter',
    'version': (0, 1),
    'blender': (3, 0, 0),
    'category': 'Window Manager',
    'description': 'NA',
    'warning': "I can't get this to work properly",
}

import bpy, datetime

class WM_OT_event_listener(bpy.types.Operator):
    """Event Listener"""
    bl_idname = "wm.event_listener"
    bl_label = "Event Listener"

    def modal(self, context: bpy.types.Context, event: bpy.types.Event):
        # Print time to console so we can see that events get passed when we move the mouse
        print(datetime.datetime.now())

        # But don't do anything with them
        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        # This is how the modal operator is added in official documentation
        #context.window_manager.modal_handler_add(self)

        # To see if not using the provided context would make a difference I tried this instead.
        # It did not make a difference...
        bpy.data.window_managers[0].modal_handler_add(self)

        # Keep the modal operator running.
        return {'RUNNING_MODAL'}

# Put an easy to use button inside the add-on preferences window
class EventListenerPreferences(bpy.types.AddonPreferences):
    bl_idname = __name__

    def draw(self, context):
        layout = self.layout
        layout.operator("wm.event_listener")

# Put an easy to use button in a new menu in the top bar
class TOPBAR_MT_event_listener(bpy.types.Menu):
    bl_label = "Event Listener"

    def draw(self, context):
        layout = self.layout
        layout.operator("wm.event_listener")

    def menu_draw(self, context):
        self.layout.menu("TOPBAR_MT_event_listener")

def register():
    # Register the modal operator class
    bpy.utils.register_class(WM_OT_event_listener)
    # Register the add-on preference class (only used to show the easy to use button inside the add-on)
    bpy.utils.register_class(EventListenerPreferences)
    # Register the menu class (only used to make the easy to use button in the top bar)
    bpy.utils.register_class(TOPBAR_MT_event_listener)
    bpy.types.TOPBAR_MT_editor_menus.append(TOPBAR_MT_event_listener.menu_draw)

def unregister():
    bpy.utils.unregister_class(WM_OT_event_listener)
    bpy.utils.unregister_class(EventListenerPreferences)
    bpy.types.TOPBAR_MT_editor_menus.remove(TOPBAR_MT_event_listener.menu_draw)
    bpy.utils.unregister_class(TOPBAR_MT_event_listener)