JavaScript KeyBoard Events


1. keyup: This event occurs when the user releases a key on the keyboard.

Syntax:

    <element onkeyup=”functionName()” >


Example: The following example converts the input letter into uppercase letter when the key is released.

    <input type=”text” onkeyup=”myScript” id=”name” />
    <script>
        function myScript()
        {
            var x = document.getElementById(“name”);
            x.value = x.value.toUpperCase();
        }
    </script>    


2. keydown: This event occurs when the user is pressing a key on the keyboard whether it produces a character value or not.

Syntax:

    <element onkeydown=”functionName()” >


Example:

    <input type=”text” onkeydown=”myScript” />
    <script>
        function myScript()
        {
            alert(“You pressed a key from your keyboard”);
        }
    </script>    



3. keypress: This event occurs when the user presses a key on the keyboard.

This event doesn’t work for some keys such as ALT, CTRL, SHIFT, ESC etc.

Syntax:

    <element onkeypress=”functionName()” >


Example:

    <input type=”text” onkeypress=”myScript” />
    <script>
        function myScript()
        {
            alert(“You pressed a key from your keyboard”);
        }
    </script>