Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Programmatically typing into a textbox (KeyboardEvent)

156 views
Skip to first unread message

Michael Hofer

unread,
Feb 14, 2012, 12:05:55 PM2/14/12
to dev-te...@lists.mozilla.org
Hello,

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.

regards,
Michael

Neil

unread,
Feb 14, 2012, 12:37:12 PM2/14/12
to
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).

Have you set focus to the textbox?

--
Warning: May contain traces of nuts.

Michael Hofer

unread,
Feb 14, 2012, 2:28:41 PM2/14/12
to dev-te...@lists.mozilla.org
Am 14.02.12 18:37, schrieb Neil:
> 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).
>
> 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).

thanks,
Michael

Michael Hofer

unread,
Feb 15, 2012, 5:54:51 AM2/15/12
to dev-te...@lists.mozilla.org
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

Neil

unread,
Feb 15, 2012, 8:29:25 AM2/15/12
to
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.

mrbunnylamakins

unread,
Mar 19, 2012, 5:15:33 PM3/19/12
to dev-te...@lists.mozilla.org
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.

0 new messages