Hi,
I am trying to create a fake keydown event for a Dashboard widget I am creating. I am having extreme trouble trying to figure this out. Basically, I cannot seem to create the proper event structure. I want to create an event that simulates the user pressing the "return" key.
Below is a quote from a page that seems to get me most of the way, though Safari does not seem to implement the initUIEvent routine:
From http://www.howtocreate.co.uk/tutorials/javascript/domevents
I am trying to create a fake keydown event for a Dashboard widget I am creating. I am having extreme trouble trying to figure this out. Basically, I cannot seem to create the proper event structure. I want to create an event that simulates the user pressing the "return" key.
Below is a quote from a page that seems to get me most of the way, though Safari does not seem to implement the initUIEvent routine:
From http://www.howtocreate.co.uk/tutorials/javascript/domevents
Key events are a little harder, because Mozilla/Firefox does not implement the standard correctly, and does not allow you to create key events using generic UIEvents. Instead, it provides a proprietary module 'KeyEvents', and initialisation method 'initKeyEvent'. The proprietary initKeyEvent method expects the following parameters: 'type', bubbles, cancelable, windowObject, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode. You can use if(window.KeyEvent) to detect support for the KeyEvents module. Browsers that expect you to use UIEvents do not have a specific initialisation function for key events, so some properties must be added manually:
Code:var fireOnThis = document.getElementById('someID'); if( window.KeyEvent ) { var evObj = document.createEvent('KeyEvents'); evObj.initKeyEvent( 'keyup', true, true, window, false, false, false, false, 13, 0 ); } else { var evObj = document.createEvent('UIEvents'); evObj.initUIEvent( 'keyup', true, true, window, 1 ); evObj.keyCode = 13; } fireOnThis.dispatchEvent(evObj);