InternetExplorer fireEvent, keyEvent (onscreen JScript keyboard)

H

hans.duedal

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/tutorials/javascript/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);
 
M

Martin Honnen

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);

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/d...r/dhtml/reference/properties/unselectable.asp>
e.g.
<input type="button"
value="a"
onclick="setKey(this.value);"
unselectable="on">
<input type="text" name="yourInput">
 
C

Cypres

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top