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:
XUL:
-------------------------------------
<textbox id="testbox"/>
JS (triggered on button command);
-------------------------------------
var test_el = document.getElementById("testbox");
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 65, 0); //65 is character 'a'
var canceled = !test_el.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
A similar example with MouseEvents (click) triggering a checkbox works fine for me, but I can't get the KeyboardEvent working (entering text in the textbox). Any help is greatly appreciated.
Michael Hofer wrote:
> A similar example with MouseEvents (click) triggering a checkbox works > fine for me, but I can't get the KeyboardEvent working (entering text > in the textbox).
>> A similar example with MouseEvents (click) triggering a checkbox works
>> fine for me, but I can't get the KeyboardEvent working (entering text
>> in the textbox).
> Have you set focus to the textbox?
No, I didn't. But I tried it now (focus before dispatchEvent) and it makes no difference (the element receives the focus, but still the keypress shows no effect).
Also, on the working example with the MouseEvent, i don't need to focus the checkbox (the event is dispatched directly to the element).
> 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:
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.
Michael Hofer wrote:
> Also, on the working example with the MouseEvent, i don't need to > focus the checkbox (the event is dispatched directly to the element).
No, but the whole point of focus is to indicate where keyboard events should be dispatched to. Anyway, as per your other post, I see you managed to work out that character events need a character code to work properly, and that XUL textboxes are actually an XBL wrapper around HTML input fields.
I am not by any means trying to go off topic, but since I seen in your code textbox is there any way of get a string or any other info into a textbox by chance? I was looking to get a vbox that pops up with textbox display some info. Too many people say forget that and do it in java which I hate.