code: def close_on_esc_or_middlemouse(event, x, y, obj):
      
      #print "Event handler called:", args
  
      if (event == ROOT.kButton2Down
              # User pressed middle mouse
          or (event == ROOT.kMouseMotion and
              x == y == 0 and
              # User pressed escape. Yes. Don't ask me why kMouseMotion.
              ROOT.gROOT.IsEscaped())):
  
          # Defer the close because otherwise root segfaults when it tries to
          # run gPad->Modified()
          obj._py_closetimer = ROOT.TTimer()
          obj._py_closetimer.Connect("Timeout()", "TCanvas", obj, "Close()")
          # Single shot after 10ms
          obj._py_closetimer.Start(10, ROOT.kTRUE)
docs: Closes canvases when escape is pressed or the canvas area is clicked with
      the middle mouse button. (ROOT requires that the mouse is over the canvas
      area itself before sending signals of any kind.)