Set hotkey for script execution

As the last topic with the same name has been closed, I post that here for the records.
Here a way to add a hotkey to execute a python function.
Basically you bind and register your hotkey to the main window (title == “Pcbnew”)

Context : using the nice plugin WireIt :
KiCad 5.1.10

def findPcbnewWindow():
                """Find the window for the PCBNEW application."""
                windows = wx.GetTopLevelWindows()
                pcbnew = [w for w in windows if "Pcbnew" in w.GetTitle()]
                if len(pcbnew) != 1:
                    raise Exception("Cannot find pcbnew window from title matching!")
                return pcbnew[0]

// In your ActionPlugin
def Run(self):
                mainFrame = findPcbnewWindow()
                wire_it_button = wx.NewId() // use as toolbar button as well
                accel_tbl = wx.AcceleratorTable([(wx.ACCEL_SHIFT,  ord('<'), wire_it_button )])
                mainFrame.Bind(wx.EVT_TOOL, wire_it_callback, id=wire_it_button)
1 Like