code: def attach_event_handler(canvas, handler=close_on_esc_or_middlemouse):
      
      if getattr(canvas, "_py_event_dispatcher_attached", None):
          return
  
      event_dispatcher = C.TPyDispatcherProcessedEvent(handler)
      canvas.Connect("ProcessedEvent(int,int,int,TObject*)",
                     "TPyDispatcherProcessedEvent", event_dispatcher,
                     "Dispatch(int,int,int,TObject*)")
  
      # Attach a handler only once to each canvas, and keep the dispatcher alive
      canvas._py_event_dispatcher_attached = event_dispatcher
docs: Attach a handler function to the ProcessedEvent slot, defaulting to
      closing when middle mouse is clicked or escape is pressed
  
      Note that escape only works if the pad has focus, which in ROOT-land means
      the mouse has to be over the canvas area.