Getting name of control under mouse in Windows?

Shane Clark shanesclark at hotmail.com
Tue Nov 20 11:17:32 EST 2007


> From: shanesclark at hotmail.com
> To: gagsl-py2 at yahoo.com.ar; python-list at python.org
> Subject: RE: Getting name of control under mouse in Windows?
> Date: Mon, 19 Nov 2007 17:16:13 -0500
> 
> 
> ----------------------------------------
> > To: python-list at python.org
> > From: gagsl-py2 at yahoo.com.ar
> > Subject: Re: Getting name of control under mouse in Windows?
> > Date: Mon, 19 Nov 2007 03:33:24 -0300
> > 
> > En Sun, 18 Nov 2007 21:10:18 -0300, Shane Clark   
> > escribió:
> > 
> >>> From: gagsl-py2 at yahoo.com.ar
> >>>
> >>> En Fri, 16 Nov 2007 16:16:25 -0300, Shane Clark
> >>> escribió:
> >>>
> >>>> I am trying to get my python app to output the name of the control  
> >>>> under
> >>>> the mouse each time it is clicked. Currently, I am trying to do this
> >>>> with a combination of pyhook and pyAA, but pyAA gives me "pyAA.Error:
> >>>> -2147417843" for almost any control in Microsoft Office apps, for
> >>>> example. If it matters, I am trying to retrieve the control's name  
> >>>> based
> >>>> on the mouse position coordinates.
> >>>
> >>> pywinauto does that.
> >>>
> >> Thanks for the response. Pywinauto is more or less incompatible with  
> >> Office apps so far as I can tell. According to what I have read, this is  
> >> because Office does not use standard Windows controls. For more info:  
> >> http://forums.openqa.org/message.jspa?messageID=28199
> > 
> > Ouch. In that case I think you won't gain much by knowing the control name  
> > anyway...
> > 
> > -- 
> > Gabriel Genellina
> > 
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> 
> Actually, the name of the control is all that I am after. I am working on a forensics toolkit for a professor of mine and I just want to know the name of the button that was clicked. Anyone have other suggestions?
> 
> Shane Clark
> _________________________________________________________________
> Your smile counts. The more smiles you share, the more we donate.  Join in.
> www.windowslive.com/smile?ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I got it working using comtypes. Thank you for the suggestions. Here is my code if anyone else is interested.

<code>
import pythoncom, pyHook
from threading import Timer
from ctypes import oledll, windll, byref, POINTER
from ctypes.wintypes import POINT
from comtypes.client import wrap
from comtypes.automation import VARIANT
from comtypes import IUnknown

oleacc = oledll.oleacc

def AccessibleObjectFromPoint(x, y):
   pacc = POINTER(IUnknown)()
   var = VARIANT()
   oleacc.AccessibleObjectFromPoint(POINT(x, y),
                                    byref(pacc),
                                    byref(var))
   return wrap(pacc), var.value

def ShowButton():
   pythoncom.CoInitialize()
   x,y, = GetCursorPos()
   pacc, value = AccessibleObjectFromPoint(x, y)
   print pacc.accName()
   
def GetCursorPos():
   pt = POINT()
   windll.user32.GetCursorPos(byref(pt))
   return pt.x, pt.y

def OnMouseClick(event):
   #pythoncom.CoInitialize()
   t = Timer(0.01, ShowButton)
   t.start()
   # return True to pass the event to other handlers
   return True

if __name__ == "__main__":
   #pythoncom.CoInitialize()
   # create a hook manager
   hm = pyHook.HookManager()
   # watch for left-click events
   hm.MouseLeftDown = OnMouseClick
   # set the hook
   hm.HookMouse()
   # wait forever
   pythoncom.PumpMessages()
</code>

-Shane Clark

_________________________________________________________________
Your smile counts. The more smiles you share, the more we donate.  Join in.
www.windowslive.com/smile?ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071120/f54653aa/attachment.html>


More information about the Python-list mailing list