Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

capture a mouse click and its location

Is it possible to have an example of code that capture a mouse click and its location (ideally in Python but ruby is also fine).

For instance: on click, print “You have clicked at the location X,Y of the design”

My main target is to make a small script that can ask you to click somewhere and set this new point as a new origin for the design. If it is a function that already exists, also please let me know.

Thanks a lot!


  • edited November 2018

    Hi Jonathan,

    you can capture mouse events in a "plugin". Below is a small piece of code illustrating how this works. Basically you create a plugin to implement a new mode. If selecting this mode by clicking on the respective tool bar entry, you can capture mouse events until the mode becomes inactive again.

    Plugins are slightly clumsy currently. For example, there is no method to activate a mode from your code. Hence, the only way to integrate your mode is through toolbar buttons. When I'm debugging the code, I have to execute the macro twice before the code becomes effective - I did not debug this yet as it's just a little bit annoying.

    And please note the workaround for bug 191.


    # Register this macro as "autorun" to enable the plugin
    # First thing is to implement a "plugin factory". This 
    # object is asked to provide a plugin for each view
    class SetOriginOnMouseClickPluginFactory(pya.PluginFactory):
      def __init__(self):
        super(SetOriginOnMouseClickPluginFactory, self).__init__()
        pya.MainWindow.instance()  # (workaround for bug 191)
        self.register(-1000, "set_origin", "Cell Origin")
      def create_plugin(self, manager, root, view):
        return SetOriginOnMouseClickPlugin()
      # Keeps the singleton instance
      instance = None
    # Create and store the singleton instance
    SetOriginOnMouseClickPluginFactory.instance = SetOriginOnMouseClickPluginFactory()
    # Second thing is the actual plugin
    class SetOriginOnMouseClickPlugin(pya.Plugin):
      def __init__(self):
        super(SetOriginOnMouseClickPlugin, self).__init__()
      def activated(self):
        pya.MainWindow.instance().message("Click on point to set the origin", 10000)
      def deactivated(self):
        pya.MainWindow.instance().message("", 0)
      def mouse_click_event(self, p, buttons, prio):
        # This is where everything happens
        if prio:
          print("Mouse clicked at " + str(p))
          return True
        return False
Sign In or Register to comment.