Reputation: 1823
I have a Java applet on web page that allows editing text. When user tries to modify text, pressing backspace button, browser forwards to previous page. If filter backspace pressing like:
var a = function () {
var event = window.event;
if (event.keyCode == 8){
alert('backspace');
return false;
}
}
document.onkeydown = a;
then backspace doesn't propagate to applet, thus not modifying text. The question is how to pass event to the applet and stop further propagating?
Upvotes: 1
Views: 2997
Reputation: 16341
// assumes IE8, but you could write detection code based on if (document.createEventObject)
if (event.keyCode === 8)
{
var newEvent = document.createEventObject();
newEvent.keyCode = 8;
document.getElementById('myjavaapplet').fireEvent('onkeypress',newEvent);
}
Upvotes: 0
Reputation: 1823
For now I find solution in using jQuery sendkeys plugin. Since using jQuery in our project is not an option I'll try to rewrite it in plain javascript.
Upvotes: 1
Reputation: 16341
if (event.keyCode === 8)
{
var newEvent = document.createEvent('UIEvents');
newEvent.initUIEvent( 'keypress', true, true, window, 1 );
newEvent.keyCode = 8;
document.getElementById('myjavaapplet').dispatchEvent(newEvent);
}
Upvotes: 0
Reputation: 16341
if (event.keyCode === 8)
{
var newEvent = document.createEvent('KeyboardEvent');
newEvent.initKeyEvent(
"keypress", // in DOMString typeArg,
true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
false, // in boolean ctrlKeyArg,
false, // in boolean altKeyArg,
false, // in boolean shiftKeyArg,
false, // in boolean metaKeyArg,
8, // in unsigned long keyCodeArg,
0); // in unsigned long charCodeArg);
document.getElementById('myjavaapplet').dispatchEvent(newEvent);
}
Upvotes: 0