[python-win32] better way to get current process's name

Tim Roberts timr at probo.com
Thu Feb 22 18:59:32 CET 2007


Kelie wrote:
> Thank you Tim and Mark.  After trying what was suggested by you (both
> work), I realized it is not what I was looking for.  What I wanted to
> do is: for every 0.1 second, check the active application or current
> application, or active window? (I don't know what is the correct
> term.) if it is AutoCAD (meaning user is using AutoCAD), turn on the
> CapsLock, if it is Microsoft Word (meaning user is using Word), turn
> off the CapsLock.
>   

The concept of "current application" is a rather tricky thing to nail
down.  I typically have a dozen or more applications running all at
once.  And,when your code is running, your code *IS* the active process.

What you want to know is which application has the keyboard focus.  Tim
G's proposal is a good one: you can try installing a Windows hook and
watching for WM_ACTIVATE messages.  However, this is filled with
problems.  That's only going to give you the window handle of the
top-level parent window for the window getting focus.  It is up to you
to figure out which application that is.  You can make an educated guess
by fetching and parsing the name (GetWindowText).  However,  "always on
top" windows and modal dialogs also confuse this issue greatly, because
they grab keyboard focus without going through WM_ACTIVATE.

I've done a couple of apps that try to hook in to Windows at this level,
and it's darned tricky.  Windows was not designed for it.

>From a usability point of view, what happens if you should WANT CapsLock
turned on in Word?

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list