Module oleacc
[hide private]
[frames] | no frames]

Source Code for Module oleacc

  1  import time 
  2  from ctypes import * 
  3  from ctypes.wintypes import * 
  4  from comtypes import * 
  5  from comtypes.automation import * 
  6  import comtypes.client 
  7  import winKernel 
  8  import winUser 
  9  # Include functions from oleacc.dll in the module namespace. 
 10  m=comtypes.client.GetModule('oleacc.dll') 
 11  globals().update((key, val) for key, val in m.__dict__.iteritems() if not key.startswith("_")) 
 12   
 13  NAVDIR_MIN=0 
 14  NAVDIR_UP=1 
 15  NAVDIR_DOWN=2 
 16  NAVDIR_LEFT=3 
 17  NAVDIR_RIGHT=4 
 18  NAVDIR_NEXT=5 
 19  NAVDIR_PREVIOUS=6 
 20  NAVDIR_FIRSTCHILD=7 
 21  NAVDIR_LASTCHILD=8 
 22  NAVDIR_MAX=9 
 23   
 24  ROLE_SYSTEM_TITLEBAR=1 
 25  ROLE_SYSTEM_MENUBAR=2 
 26  ROLE_SYSTEM_SCROLLBAR=3 
 27  ROLE_SYSTEM_GRIP=4 
 28  ROLE_SYSTEM_SOUND=5 
 29  ROLE_SYSTEM_CURSOR=6 
 30  ROLE_SYSTEM_CARET=7 
 31  ROLE_SYSTEM_ALERT=8 
 32  ROLE_SYSTEM_WINDOW=9 
 33  ROLE_SYSTEM_CLIENT=10 
 34  ROLE_SYSTEM_MENUPOPUP=11 
 35  ROLE_SYSTEM_MENUITEM=12 
 36  ROLE_SYSTEM_TOOLTIP=13 
 37  ROLE_SYSTEM_APPLICATION=14 
 38  ROLE_SYSTEM_DOCUMENT=15 
 39  ROLE_SYSTEM_PANE=16 
 40  ROLE_SYSTEM_CHART=17 
 41  ROLE_SYSTEM_DIALOG=18 
 42  ROLE_SYSTEM_BORDER=19 
 43  ROLE_SYSTEM_GROUPING=20 
 44  ROLE_SYSTEM_SEPARATOR=21 
 45  ROLE_SYSTEM_TOOLBAR=22 
 46  ROLE_SYSTEM_STATUSBAR=23 
 47  ROLE_SYSTEM_TABLE=24 
 48  ROLE_SYSTEM_COLUMNHEADER=25 
 49  ROLE_SYSTEM_ROWHEADER=26 
 50  ROLE_SYSTEM_COLUMN=27 
 51  ROLE_SYSTEM_ROW=28 
 52  ROLE_SYSTEM_CELL=29 
 53  ROLE_SYSTEM_LINK=30 
 54  ROLE_SYSTEM_HELPBALLOON=31 
 55  ROLE_SYSTEM_CHARACTER=32 
 56  ROLE_SYSTEM_LIST=33 
 57  ROLE_SYSTEM_LISTITEM=34 
 58  ROLE_SYSTEM_OUTLINE=35 
 59  ROLE_SYSTEM_OUTLINEITEM=36 
 60  ROLE_SYSTEM_PAGETAB=37 
 61  ROLE_SYSTEM_PROPERTYPAGE=38 
 62  ROLE_SYSTEM_INDICATOR=39 
 63  ROLE_SYSTEM_GRAPHIC=40 
 64  ROLE_SYSTEM_STATICTEXT=41 
 65  ROLE_SYSTEM_TEXT=42 
 66  ROLE_SYSTEM_PUSHBUTTON=43 
 67  ROLE_SYSTEM_CHECKBUTTON=44 
 68  ROLE_SYSTEM_RADIOBUTTON=45 
 69  ROLE_SYSTEM_COMBOBOX=46 
 70  ROLE_SYSTEM_DROPLIST=47 
 71  ROLE_SYSTEM_PROGRESSBAR=48 
 72  ROLE_SYSTEM_DIAL=49 
 73  ROLE_SYSTEM_HOTKEYFIELD=50 
 74  ROLE_SYSTEM_SLIDER=51 
 75  ROLE_SYSTEM_SPINBUTTON=52 
 76  ROLE_SYSTEM_DIAGRAM=53 
 77  ROLE_SYSTEM_ANIMATION=54 
 78  ROLE_SYSTEM_EQUATION=55 
 79  ROLE_SYSTEM_BUTTONDROPDOWN=56 
 80  ROLE_SYSTEM_BUTTONMENU=57 
 81  ROLE_SYSTEM_BUTTONDROPDOWNGRID=58 
 82  ROLE_SYSTEM_WHITESPACE=59 
 83  ROLE_SYSTEM_PAGETABLIST=60 
 84  ROLE_SYSTEM_CLOCK=61 
 85  ROLE_SYSTEM_SPLITBUTTON=62 
 86  ROLE_SYSTEM_IPADDRESS=63 
 87  ROLE_SYSTEM_OUTLINEBUTTON=64 
 88   
 89  STATE_SYSTEM_NORMAL=0 
 90  STATE_SYSTEM_UNAVAILABLE=0x1 
 91  STATE_SYSTEM_SELECTED=0x2 
 92  STATE_SYSTEM_FOCUSED=0x4 
 93  STATE_SYSTEM_PRESSED=0x8 
 94  STATE_SYSTEM_CHECKED=0x10 
 95  STATE_SYSTEM_MIXED=0x20 
 96  STATE_SYSTEM_INDETERMINATE=STATE_SYSTEM_MIXED 
 97  STATE_SYSTEM_READONLY=0x40 
 98  STATE_SYSTEM_HOTTRACKED=0x80 
 99  STATE_SYSTEM_DEFAULT=0x100 
100  STATE_SYSTEM_EXPANDED=0x200 
101  STATE_SYSTEM_COLLAPSED=0x400 
102  STATE_SYSTEM_BUSY=0x800 
103  STATE_SYSTEM_FLOATING=0x1000 
104  STATE_SYSTEM_MARQUEED=0x2000 
105  STATE_SYSTEM_ANIMATED=0x4000 
106  STATE_SYSTEM_INVISIBLE=0x8000 
107  STATE_SYSTEM_OFFSCREEN=0x10000 
108  STATE_SYSTEM_SIZEABLE=0x20000 
109  STATE_SYSTEM_MOVEABLE=0x40000 
110  STATE_SYSTEM_SELFVOICING=0x80000 
111  STATE_SYSTEM_FOCUSABLE=0x100000 
112  STATE_SYSTEM_SELECTABLE=0x200000 
113  STATE_SYSTEM_LINKED=0x400000 
114  STATE_SYSTEM_TRAVERSED=0x800000 
115  STATE_SYSTEM_MULTISELECTABLE=0x1000000 
116  STATE_SYSTEM_EXTSELECTABLE=0x2000000 
117  STATE_SYSTEM_ALERT_LOW=0x4000000 
118  STATE_SYSTEM_ALERT_MEDIUM=0x8000000 
119  STATE_SYSTEM_ALERT_HIGH=0x10000000 
120  STATE_SYSTEM_PROTECTED=0x20000000 
121  STATE_SYSTEM_HASPOPUP=0x40000000 
122  STATE_SYSTEM_VALID=0x7fffffff 
123   
124  SELFLAG_NONE=0 
125  SELFLAG_TAKEFOCUS=1 
126  SELFLAG_TAKESELECTION=2 
127  SELFLAG_EXTENDSELECTION=4 
128  SELFLAG_ADDSELECTION=8 
129  SELFLAG_REMOVESELECTION=16 
130  SELFLAG_VALID=32 
131   
132 -def LresultFromObject(wParam,obj):
133 """ 134 returns a reference, similar to a handle, to the specified object. 135 Servers return this reference when handling WM_GETOBJECT. 136 @param wParam: the wParam value passed in with WM_GETOBJECT. 137 @type wParam: int 138 @param obj: the COM object instance you want a reference for. 139 @type obj: COMObject 140 @return: a reference to the object. 141 @rtype: int 142 """ 143 objIID=obj._iid_ 144 return oledll.oleacc.LresultFromObject(byref(objIID),wParam,obj)
145
146 -def ObjectFromLresult(res,wParam,interface):
147 """ 148 retrieves a requested interface pointer for an accessible object 149 based on a previously generated object reference. 150 @param res: the previously generated object reference. 151 @type res: int 152 @param wParam: the wParam value passed in with WM_GETOBJECT. 153 @type wParam: int 154 @param interface: the requested COM interface. 155 @type interface: comtypes COM interface 156 @return: the object. 157 @rtype: COMObject 158 """ 159 p=POINTER(interface)() 160 oledll.oleacc.ObjectFromLresult(res,byref(interface._iid_),wParam,byref(p)) 161 return p
162
163 -def CreateStdAccessibleProxy(hwnd,className,objectID,interface=IAccessible):
164 """ 165 creates an accessible object using a specific window class, with the methods and properties 166 of the specified type of system-provided user interface element. 167 @param hwnd: the handle of the window this accessible object should represent. 168 @type hwnd: int 169 @param className: the window class name to use. 170 @type className: basestring 171 @param objectID: an OBJID_* constant or custom value stating the specific object in the window. 172 @type objectID: int 173 @param interface: the requested COM interface for this object. Defaults to IAccessible. 174 @type interface: comtypes COM interface 175 @return: the created object. 176 @rtype: COMObject 177 """ 178 p=POINTER(interface)() 179 oledll.oleacc.CreateStdAccessibleProxyW(hwnd,className,objectID,byref(interface._iid_),byref(p)) 180 return p
181
182 -def CreateStdAccessibleObject(hwnd,objectID,interface=IAccessible):
183 """ 184 creates an accessible object with the methods and properties 185 of the specified type of system-provided user interface element. 186 @param hwnd: the handle of the window this accessible object should represent. 187 @type hwnd: int 188 @param objectID: an OBJID_* constant or custom value stating the specific object in the window. 189 @type objectID: int 190 @param interface: the requested COM interface for this object. Defaults to IAccessible. 191 @type interface: comtypes COM interface 192 @return: the created object. 193 @rtype: COMObject 194 """ 195 p=POINTER(interface)() 196 oledll.oleacc.CreateStdAccessibleObject(hwnd,objectID,byref(interface._iid_),byref(p)) 197 return p
198
199 -def AccessibleObjectFromWindow(hwnd,objectID,interface=IAccessible):
200 """ 201 Retreaves a COM object from the given window, with the given object ID. 202 @param hwnd: the handle of the window to retreave the object from. 203 @type hwnd: int 204 @param objectID: one of the OBJID_* constants or a custom positive value representing the specific object you want to retreave. 205 @type objectID: int 206 @param interface: the requested COM interface you wish to use on the retreaved object. 207 @type interface: comtypes COM interface 208 @return: the retreaved object. 209 @rtype: COMObject 210 """ 211 p=POINTER(interface)() 212 oledll.oleacc.AccessibleObjectFromWindow(hwnd,objectID,byref(p._iid_),byref(p)) 213 return p
214
215 -def AccessibleObjectFromWindow_safe(hwnd,objectID,interface=IAccessible,timeout=2):
216 if not hwnd: 217 raise ValueError("Invalid window") 218 wmResult=c_long() 219 res=windll.user32.SendMessageTimeoutW(hwnd,winUser.WM_GETOBJECT,0,objectID,winUser.SMTO_ABORTIFHUNG,int(timeout*1000),byref(wmResult))==0 220 if res: 221 raise OSError("WM_GETOBJECT failed") 222 if wmResult.value: 223 return ObjectFromLresult(wmResult.value,0,interface) 224 return CreateStdAccessibleObject(hwnd,objectID,interface)
225
226 -def AccessibleObjectFromEvent(hwnd,objectID,childID):
227 """ 228 Retreaves an IAccessible object from the given window, with the given object ID and child ID. 229 @param hwnd: the handle of the window to retreave the object from. 230 @type hwnd: int 231 @param objectID: one of the OBJID_* constants or a custom positive value representing the specific object you want to retreave. 232 @type objectID: int 233 @param childID: the ID of the child element you wish to retreave. 234 @type childID: int 235 @return: the retreaved object. 236 @rtype: COMObject 237 """ 238 p=POINTER(IAccessible)() 239 varChild=VARIANT() 240 oledll.oleacc.AccessibleObjectFromEvent(hwnd,objectID,childID,byref(p),byref(varChild)) 241 if varChild.vt==VT_I4: 242 childID=varChild.value 243 return (p,childID)
244
245 -def AccessibleObjectFromEvent_safe(hwnd,objectID,childID,timeout=2):
246 obj=AccessibleObjectFromWindow_safe(hwnd,objectID,timeout=timeout) 247 if not obj: 248 raise RuntimeError("AccessibleObjectFromWindow failed") 249 if childID!=0: 250 try: 251 childObj=obj.accChild(childID) 252 except COMError: 253 childObj=None 254 if childObj: 255 obj=childObj 256 childID=0 257 return (obj,childID)
258
259 -def WindowFromAccessibleObject(pacc):
260 """ 261 Retreaves the handle of the window this IAccessible object belongs to. 262 @param pacc: the IAccessible object who's window you want to fetch. 263 @type pacc: POINTER(IAccessible) 264 @return: the window handle. 265 @rtype: int 266 """ 267 hwnd=c_int() 268 oledll.oleacc.WindowFromAccessibleObject(pacc,byref(hwnd)) 269 return hwnd.value
270
271 -def AccessibleObjectFromPoint(x,y):
272 point=POINT(x,y) 273 pacc=POINTER(IAccessible)() 274 varChild=VARIANT() 275 oledll.oleacc.AccessibleObjectFromPoint(point,byref(pacc),byref(varChild)) 276 if not isinstance(varChild.value,int): 277 child=0 278 else: 279 child=varChild.value 280 return (pacc,child)
281
282 -def AccessibleChildren(pacc,iChildStart,cChildren):
283 varChildren=(VARIANT*cChildren)() 284 pcObtained=c_int() 285 oledll.oleacc.AccessibleChildren(pacc,iChildStart,cChildren,byref(varChildren),byref(pcObtained)) 286 return [x.value for x in varChildren[0:pcObtained.value]]
287
288 -def GetProcessHandleFromHwnd(windowHandle):
289 """Retreaves a process handle of the process who owns the window. 290 If Windows Vista, uses GetProcessHandleFromHwnd found in oleacc.dll which allows a client with UIAccess to open a process who is elevated. 291 if older than Windows Vista, just uses OpenProcess from user32.dll instead. 292 @param windowHandle: a window of a process you wish to retreave a process handle for 293 @type windowHandle: integer 294 @returns: a process handle with read, write and operation access 295 @rtype: integer 296 """ 297 try: 298 return oledll.oleacc.GetProcessHandleFromHwnd(windowHandle) 299 except: 300 import winKernel 301 return winKernel.openProcess(winKernel.PROCESS_VM_READ|winKernel.PROCESS_VM_WRITE|winKernel.PROCESS_VM_OPERATION,False,winUser.getWindowThreadProcessID(windowHandle)[0])
302
303 -def GetRoleText(role):
304 textLen=oledll.oleacc.GetRoleTextW(role,0,0) 305 if textLen: 306 buf=create_unicode_buffer(textLen+2) 307 oledll.oleacc.GetRoleTextW(role,buf,textLen+1) 308 return buf.value 309 else: 310 return None
311
312 -def GetStateText(state):
313 textLen=oledll.oleacc.GetStateTextW(state,0,0) 314 if textLen: 315 buf=create_unicode_buffer(textLen+2) 316 oledll.oleacc.GetStateTextW(state,buf,textLen+1) 317 return buf.value 318 else: 319 return None
320