Mouse click without timer?

How we can define mouse left or right click with GhPython without using timer?
If possible with using external dll

The lowest level of doing this is to call winapi commands (part of the user32.dll). But as always, doing these kind of things is hacky and a bad choice. But if there is no other option, this at least always works. In Python, probaly not working for IronPython, there is the pyautogui package, which does exactly this on windows. With Ironpython I guess you’ll need to use inbuild ctypes module. There should be enough examples on google.

Thanks Tom
I use an addon from food4rhino, i don’t find any solution work with GhPython

Hi @seghierkhaled,

What are you trying to do?

– Dale

Hi @dale
I want use click to detect active viewport (also wheel if possible to detect zoom)
In this video i used Mouse Rat addon.


If you want to know what the active view is, use ViewTable.ActiveView.

import scriptcontext as sc

view = sc.doc.Views.ActiveView

If you want to know when the active view changes, then create a RhinoView.SetActive event handler

import Rhino
import scriptcontext as sc

class MyEventHandler:
    def __init__(self):
        Rhino.Display.RhinoView.SetActive += self.OnSetActiveView
    def RemoveEvents(self):        
        Rhino.Display.RhinoView.SetActive -= self.OnSetActiveView
    def OnSetActiveView(self, sender, e):

# Toggle handler on and off
key = 'MyEventHandler'
if sc.sticky.has_key(key):
    handler = sc.sticky[key]
    if handler:
        handler = None
    handler = MyEventHandler()
    sc.sticky[key] = handler

Does this help?

– Dale

1 Like

Thank you very much @dale , it work excellent in Rhino.
How we can make it work in Grasshopper too?

Geometry pipeline detect immediately when we add points or other geometries, how we can use the same method to detect mouse clicks?

Hi @seghierkhaled

This is a possible way to make this work:

import Rhino

import scriptcontext as sc

key = 'MyViewEventInfo'

class MyViewEventInfo:
    def __init__(self, component):
        self.component = component
        Rhino.Display.RhinoView.SetActive += self.OnSetActiveViewHandler
    def Release(self):
        Rhino.Display.RhinoView.SetActive -= self.OnSetActiveViewHandler
    def OnSetActiveViewHandler(self, sender, e):

if sc.sticky.has_key(key):
    if not on:
        handler = sc.sticky[key]
        if handler: handler.Release()
        del sc.sticky[key]
    handler = MyViewEventInfo(ghenv.Component)
    sc.sticky[key] = handler

view = sc.doc.Views.ActiveView
a = (view.ActiveViewport.Name)

Please note that nothing happens “immediately”. It’s a matter of making it happen “subsequently” (in a queue) and requesting a new solution after that. (5.2 KB)


Thank you very much @piac , works great