Re: Programmatically typing into a textbox (KeyboardEvent)

Michael Hofer <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xpfe
Message-ID <[email protected]>
Am 14.02.12 18:05, schrieb Michael Hofer:
> I want to programmatically type into a xul textbox (xulrunner 2.0).
> While the following code does trigger the "not canceled"-alert, it
> doesn't change the contents of the textbox.

First of all: It's not a xulrunner 2.0 thing (bug). I get the same 
behaviour in Firefox 10.0.1 (via Real-Time XUL Editor from "Developer 
Assistent" addon).

But I finally found the solution:

First, there's a problem with my use of initKeyEvent. For sending the 
letter "A", you need to put the character code in the last parameter, 
not the second last. So the correct call would be:

evt.initKeyEvent("keypress", true, true, null, false, false, false, 
false, 0, 65);

If you wanted to send a non printable character (like backspace) 
instead, you would do it the other way around:

evt.initKeyEvent("keypress", true, true, null, false, false, false, 
false, 8, 0);


While this worked now for an html input field, it still wasn't with a 
xul textbox. But since the XUL textbox contains an html input and makes 
it available through the 'inputField' property, I'm dispatching the 
event directly on the inputField now. That finally does the trick.

See http://pastebin.com/Lf7kj3VR for a working full code example.

regards,
Michael
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.