By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,830 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,830 IT Pros & Developers. It's quick & easy.

InternetExplorer fireEvent, keyEvent (onscreen JScript keyboard)

P: n/a
The Gecko DOM reference gave me the idea that an onscreen keyboard I
was doing should use Key Events, so the user may for instance place a
letter anywhere, and such. While this can be handled using a number of
hacks, I really want to use events, since they seem like the "right"
way to do an onscreen keyboard.

Just fire the key events associated with the button on screen right?
With Firefox this worked great, and so did it for IE, BUT, IE does fire
the events, and I can capture them as well, but no letters appear in
the textarea, since this is handled otherwise.

I use this tutorial for reference:
http://www.howtocreate.co.uk/tutoria...ript/domevents

Today I am forced to do it this way:

--- snip ---
if( document.createEvent ) {
if( window.KeyEvent ) {
var evObj = document.createEvent('KeyEvents');
evObj.initKeyEvent( 'keypress', true, true, window, false, false,
false, false, 0, key.charCodeAt(0) );
} else {
var evObj = document.createEvent('UIEvents');
evObj.initUIEvent( 'keyup', true, true, window, 1 );
evObj.keyCode = key.charCodeAt(0);
}
this.input.dispatchEvent(evObj);
} else if( document.createEventObject ) {
this.input.value += key;
}
--- snip ---

the IE code would go something like this:
---
var evObj = document.createEventObject();
evObj.keyCode = key.charCodeAt(0);
evObj.repeat = false;
evObj.returnValue = true;
this.input.fireEvent('onkeypress',evObj);
---

Just appending the character works, but is not optimal. Does anyone has
a solution for this IE problem?

Aug 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


ha*********@gmail.com wrote:

the IE code would go something like this:
---
var evObj = document.createEventObject();
evObj.keyCode = key.charCodeAt(0);
evObj.repeat = false;
evObj.returnValue = true;
this.input.fireEvent('onkeypress',evObj);
---

Just appending the character works, but is not optimal. Does anyone has
a solution for this IE problem?
IE allows you to manipulate the selection so instead of doing
this.input.value += key;
you could try e.g.

var range = document.selection.createRange();
if (range.parentElement() === this.input) {
range.text = key;
}

That would then insert the key string or character at the caret
respectively if there is a text selected it would replace the selected
text with the key string or character.
The problem however is that any element being clicked to trigger the
above code might change the selection so often you then need the
unselectable attribute
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/unselectable.asp>
e.g.
<input type="button"
value="a"
onclick="setKey(this.value);"
unselectable="on">
<input type="text" name="yourInput">

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 19 '06 #2

P: n/a
Martin Honnen skrev:
you could try e.g.

var range = document.selection.createRange();
if (range.parentElement() === this.input) {
range.text = key;
}

That would then insert the key string or character at the caret
respectively if there is a text selected it would replace the selected
text with the key string or character.
The problem however is that any element being clicked to trigger the
above code might change the selection so often you then need the
unselectable attribute
Thank you! It is not quite what I hoped for, but working better ever
the less. I where again really hoping for a relatively unknown IE
"hack" that would allow one to trigger real key events.

Thank you for your answer.

Aug 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.