This article tells the whole story
"The secret life of GetWindowText"
http://blogs.msdn.com/oldnewthing/archive/2003/08/21/54675.aspx
Appended is a script that is a fairly straight-forward conversion of the
code in the above article to Python. Needs ctypes and comtypes.
Python 2.5 already includes ctypes; comtypes can be installed
with 'easy_install comtypes'.
Thomas
import sys, time
from ctypes import windll, oledll, WinError, byref, POINTER
from ctypes.wintypes import POINT
from comtypes import COMError
from comtypes.automation import VARIANT
from comtypes.client import GetModule
# create wrapper for the oleacc.dll type library
GetModule("oleacc.dll")
# import the interface we need from the wrapper
from comtypes.gen.Accessibility import IAccessible
def GetCursorPos():
"Return the cursor coordinates"
pt = POINT()
if not windll.user32.GetCursorPos(byref(pt)):
raise WinError()
return pt.x, pt.y
def AccessibleObjectFromPoint(x, y):
"Return an accessible object and an index. See MSDN for details."
pacc = POINTER(IAccessible)()
var = VARIANT()
oledll.oleacc.AccessibleObjectFromPoint(POINT(x, y), byref(pacc), byref(var))
return pacc, var
if __name__ == "__main__":
while 1:
time.sleep(1)
x, y = GetCursorPos()
try:
pacc, index = AccessibleObjectFromPoint(x, y)
name = pacc.accName[index]
except (WindowsError, COMError), details:
print details
continue
if name is not None:
print "===", (x, y), "=" * 60
print name.encode(sys.stdout.encoding, "backslashreplace")