views:

506

answers:

4

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 propogate to applet, thus not modifying text. The question is how to pass event to the applet ant stop further propagating?

A: 
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);
}
Kev
Note that iniKeyEvent is "Gecko-specific; the DOM 3 Events working draft uses initKeyboardEvent instead" ( https://developer.mozilla.org/en/DOM/document.createEvent )
Kev
I'm programming for IE. The Event object returned by document.createEventObject dosn't have method 'initKeyboardEvent'
kilonet
A: 
if (event.keyCode === 8)
{
    var newEvent = document.createEvent('UIEvents');
    newEvent.initUIEvent( 'keypress', true, true, window, 1 );
    newEvent.keyCode = 8;
    document.getElementById('myjavaapplet').dispatchEvent(newEvent);
}
Kev
according to IE8 JS debugger event object has only following methods: getAttribute, setAttribute and removeAttribute. I've tried setting attributes listed in initUIEvent but it did't work(
kilonet
Oh, wow. So much for standards.
Kev
A: 

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.

kilonet
I wonder what method sendkeys uses...
Kev
It uses some sophisticated manipulations with hidden textarea to simulate key press
kilonet
Wow, okay, well, I'm glad you figured it out. IE really frustrates me sometimes...
Kev
A: 
// 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);
}
Kev
I've already tried this - it did't work also
kilonet
Did it error or just not doing anything?
Kev
It did't do anything
kilonet