Help: Check if Rhino is active window (python)

My lack of basic programming skills are killing me as I am trying to figure out how to solve something I thought was a simple task…

How do I determine if Rhino (and the specific instance) is the active window/ application in focus? I use Python.

I have spent hours on the net trying to understand how to do this, but what I find I can not use… because I don’t understand it. And it hurts… :wink:

Cheers

Not really all that easy of a task. GetForegroundWindow is what is needed and that is a lower level Windows API C function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx

You would need to use ctypes for this

import ctypes
import System
from ctypes import windll

last_id = 0
for i in range(50):
    System.Threading.Thread.Sleep(50)
    hwnd = windll.user32.GetForegroundWindow()
    pid = ctypes.c_ulong()
    windll.user32.GetWindowThreadProcessId(hwnd,ctypes.byref(pid))
    p = System.Diagnostics.Process.GetProcessById(pid.value)
    if last_id != pid.value:
        print p.MainWindowTitle
        last_id = pid.value
1 Like

I wonder how I can repay you!
See, the problem is that I have looked at that same page from the link, but none of what’s in there makes enough sense for me so I can use it in any way.

Now I’ll see if I can implement this so I can release the control over the mouse if Rhino is not active.

I have tried the code now, and it works, but not reliable. For some reason if I wait for a while, when the python editor is active, and then run it, it sometimes returns a blank.

Sometimes if I run it and toggle back and forth between Rhino and another app then it returns both, as it should, and other times it only returns “the other app” and returns Rhino as a blank. Without an apparent logic to it.

Well, I’ll let it rest for a while now and look at it tomorrow.

Agan THANK YOU SO MUCH for the help!!!

Second Edit:
It seems to work great when in a plugin!
The script editor might have been the thing that caused the blank return.

The plugin now terminates the moment Rhino is no longer active.

THANK YOU!