[python-win32] FireEvent : comtypes work but win32com fails, why?

June Kim juneaftn at gmail.com
Wed Jun 25 06:58:36 CEST 2008


IE's FireEvent works with a single argument, for example,
FireEvent("onKeyUp"). However, it doesn't work with two arguments :
FireEvent("onKeyUp",event).

But It works without a problem with comtypes.

See the following example to reproduce and compare:


file: c:/temp/javascripttest/onkeyup.html

<html>
<head>
<title>onkeyup</title>
<script language="javascript">
function showmeevent() {
  document.getElementById("here").innerHTML+="[+] "+event.keyCode +"<br/>";
  document.lastKeyCode=event.keyCode;
}
</script>
</head>
</html>
<input id="inp" onkeyup="showmeevent()"/>
<div id="here"></div>
</body>
<body>


Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from win32com.client import DispatchEx
>>> ie=DispatchEx('InternetExplorer.Application')
>>> ie.Visible=True
>>> ie.Visible=False
>>> ie.Navigate('c:/temp/javascripttest/onkeyup.html')
>>> ie.Visible=True
>>> e=ie.Document.CreateEventObject()
>>> e.keyCode=65
>>> ie.Document.all.inp.FireEvent("onkeyup",e)
True
>>> ie.Document.lastKeyCode
0

On the html document displayed on the browser I see "0" is appended.
The event object that I created with new keyCode didn't get passed.

Now with comtypes:

>>> from comtypes.client import CreateObject
>>> ie2=CreateObject("InternetExplorer.Application")
>>> ie2.Visible=True
>>> ie2.Navigate('c:/temp/javascripttest/onkeyup.html')
0
>>> e2=ie2.Document.CreateEventObject()
>>> e2.keyCode=65
>>> ie2.Document.all.inp.FireEvent("onkeyup",e2)
True
>>> ie2.Document.lastKeyCode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\comtypes\__init__.py", line 216, in __geta
ttr__
    raise AttributeError(name)
AttributeError: lastKeyCode

Accessing lastKeyCode from ie2.Document wasn't possible but the event
obejct is passed successfully -- I see 65 appended to the document.


In summary, there are one big problem and one minor problem. The first
is about pywin32's inability to pass FireEvent's second argument. The
second is about comtypes inability to access Document's attributes.


More information about the python-win32 mailing list