You are here: Reference > JavaScript > client-side > event handling > methods > initKeyEvent (event)

initKeyEvent method (event)

Browser support:
Initializes an event object created by the createEvent method with type of 'KeyboardEvent'.
An event object created by the createEvent method with type of the 'KeyboardEvent' implements the KeyboardEvent interface.
After initialization, the event can be dispatched with the dispatchEvent method.
The initKeyEvent method is only supported by Firefox. In Google Chrome, Safari and Internet Explorer from version 9, use the initKeyboardEvent method instead. In Internet Explorer before version 9, use the createEventObject method to create a new event object and the fireEvent method to dispatch it.

Syntax:

object.initKeyEvent (eventName, bubbles, cancelable, view, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode);
You can find the related objects in the Supported by objects section below.

Parameters:

eventName
Required. String that specifies the name (type) of the event to initialize.
This parameter is case sensitive! Sets the type property of the event object. For a complete list of events, see the page for Events in JavaScript. The initKeyEvent method is appropriate for initialization of KeyboardEvent types principally, for other events, use the type-specific initialization methods.
Note: custom event names are also allowed.
Events represented by the KeyboardEvent interface:
keydown
keypress
keyup
bubbles
Required. Boolean that specifies whether the event can bubble or not. Sets the bubbles property of the event object.
One of the following values:
false
The event cannot bubble up.
true
The event can bubble up.
cancelable
Required. Boolean that specifies whether the event can be canceled or not. Sets the cancelable property of the event object.
One of the following values:
false
The event cannot be canceled.
true
The event can be canceled.
view
Required. Reference to the window object in which the event is supposed to have occurred. Sets the view property of the event object.
ctrlKey
Required. Boolean that indicates the Control key state. Sets the ctrlKey property of the event object.
One of the following values:
false
The Control key is not pressed.
true
The Control key is pressed.
altKey
Required. Boolean that indicates the Alt key state. Sets the altKey property of the event object.
One of the following values:
false
The Alt key is not pressed.
true
The Alt key is pressed.
shiftKey
Required. Boolean that indicates the Shift key state. Sets the shiftKey property of the event object.
One of the following values:
false
The Shift key is not pressed.
true
The Shift key is pressed.
metaKey
Required. Boolean that indicates the Meta key state. Sets the metaKey property of the event object.
One of the following values:
false
The Meta key is not pressed.
true
The Meta key is pressed.
keyCode
Required. Integer that specifies the Unicode key code of the key that was pressed. Sets the keyCode property of the event object.
Predefined constants are available for the possible Unicode key codes, in the scope of the KeyboardEvent interface. You can use them directly through the <a href='/lagstsiq.php/#KeyboardEvent'>KeyboardEvent</a> interface (KeyboardEvent.DOM_VK_A) or through an instance of the event object. For further details, please see the page for the <a href='/lagstsiq.php/#KeyboardEvent'>KeyboardEvent</a> interface.
charCode
Required. Integer that specifies the Unicode character code of the key that was pressed. Sets the charCode property of the event object.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the initKeyEvent method:
<head>
    <script type="text/javascript">
        function InsertChar () {
            try {
                var pressEvent = document.createEvent ("KeyboardEvent");
                pressEvent.initKeyEvent ("keypress", true, true, window, 
                                        false, false, false, false, 
                                        0, "x".charCodeAt (0));
                var input = document.getElementById ("myInput");
                input.dispatchEvent (pressEvent);
            }
            catch (e) {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body>
    <input type="text" id="myInput" value="Input field" size="20"/>
    <button onclick="InsertChar ()">Simulate pressing the 'x' key on the input.</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content