Send generated KeyEvents to a <input type="text">

3 views
Skip to first unread message

Schoetz

unread,
Dec 5, 2003, 4:38:39 AM12/5/03
to
Hello

I'd like to send my own generated key events to a <input type="text">.
Acutally this is no problem, with keys like DOM_VK_LEFT. The cursor
goes one char left, but I want to write characters to the text-input.

Until now I have the following working code:


var e = document.createEvent('KeyEvents');
e.initKeyEvent("keypress",true,true,null,false,false,false,false,e.DOM_VK_LEFT,0);

control.dispatchEvent(e);


Does anyone know how I must init the KeyEvent to generate some new
Characters at the actual Cursorposition in the Textfield?

Regards schoetz

Benjamin Smedberg

unread,
Dec 5, 2003, 8:18:40 AM12/5/03
to
Schoetz wrote:

> Does anyone know how I must init the KeyEvent to generate some new
> Characters at the actual Cursorposition in the Textfield?

see the constants defined here:
http://lxr.mozilla.org/mozilla/source/dom/public/idl/events/nsIDOMKeyEvent.idl#87

for ordinary letters, the ASCII code is used.

--BDS

Schoetz

unread,
Dec 5, 2003, 2:59:39 PM12/5/03
to
I already tried this:

var e = document.createEvent('KeyEvents');

e.initKeyEvent("keypress",true,true,null,false,false,false,false,e.DOM_VK_A,0);

control.dispatchEvent(e);

But it doesn't seems to work. :(

Any other ideas?

Thx
schoetz

Benjamin Smedberg wrote:

> Schoetz wrote:
>
>> Does anyone know how I must init the KeyEvent to generate some new
>> Characters at the actual Cursorposition in the Textfield?
>
> see the constants defined here:
> http://lxr.mozilla.org/mozilla/source/dom/public/idl/events

Martin Honnen

unread,
Dec 5, 2003, 2:06:23 PM12/5/03
to

Schoetz wrote:

You can simply manipulate the selection if needed
http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130
--

Martin Honnen
http://JavaScript.FAQTs.com/

Boris Zbarsky

unread,
Dec 5, 2003, 1:58:34 PM12/5/03
to
Schoetz wrote:
> var e = document.createEvent('KeyEvents');
> e.initKeyEvent("keypress",true,true,null,false,false,false,false,e.DOM_VK_A,0);

Those last two args are the keycode and charcode. The way key events
work in Mozilla is that printable char events have a charcode;
nonprintable ones have a keycode. So you want to be passing "0,
e.DOM_VK_A" for those two args, not "e.DOM_VK_A,0", as far as I can tell.

-Boris

Schoetz

unread,
Dec 5, 2003, 3:50:01 PM12/5/03
to
I already know this solution, but there is one thing I
can't accept. If the text is wider than the textfield, it
will also show the beginning of the text and not the
current cursorposition.

I hope I get it work the other way.

If there is a way to fix this well with the "selection-way"
this is of course also welcome!

Thx
schoetz

Schoetz

unread,
Dec 5, 2003, 5:11:43 PM12/5/03
to
It doesn't seems to work :(

Does anybody know a documentation for this function. Exept the W3C docu.

schoetz

manuel

unread,
Dec 8, 2003, 4:14:14 AM12/8/03
to
Hey it works!!!!

Many thanx for this tip. I do not know why there was a problem with
this code when I tried it at home. But now at work it works fine.

Here is my code, if there is anybody looking for it:

e.initKeyEvent("keypress",true,true,null,false,false,false,false,0,e.DOM_VK_A);

schoetz

Boris Zbarsky geschrieben:

> -Boris


##-----------------------------------------------##
Artikel geschrieben mit MyNewsGroups :)
http://mynewsgroups.sourceforge.net
Share your knowledge in... netscape.public.mozilla.dom
##-----------------------------------------------##

Reply all
Reply to author
Forward
0 new messages