JS KeyCode Event Response

Source: Internet
Author: User

<script language= "JavaScript" >function KeyEvent () {if (event.keycode==13) alert ("You hit Enter");} Document.onkeydown = keyevent;//Register Document.onkeydown Event
</script>

When the user presses the ENTER key, the KeyEvent () function is executed. Can do a carriage return to submit the form, or other events!

KeyCode is the code that returns the key to the KeyDown what KeyUp event occurred, and the Unicode character of the KeyPress event, and if you want to use a key combination, you can take advantage of event.ctrlkey,event.shiftkey,event. Altkey determines whether the CTRL key, the SHIFT key, and the ALT key are pressed.

Common KeyCode
Key Code Key Code
Left 37 Up 38
Right 39 Down 40
1 49 2 50
3 51 A 65
Shift 16 Ctrl 17
Alt 18 Enter 13

Example:

<! DOCTYPE HTML>
< HTML >
< Head >
    < title > keyboard events keycode</title>
</ Head >
< Body >
    left&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;up&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;right&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;down<BR  />
    CTRL + Up&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + down<br/> 
    CTRL + 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + 2&nbsp;&nbsp;&nbsp;& Nbsp;&nbsp;&nbsp;ctrl + 3<br/>
    A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + a&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;ctrl + SHIFT + a<br/>
    < Script type="Text/javascript">
        Document.onkeydown = function (event) {
            Event = Event | | window.event;
            Bctrlkey = Event.ctrlkey;
            Bshiftkey = Event.shiftkey;
            Switch (event.keycode) {
                Case 37:
                    Alert ("left");
                    Break
                Case 38:
                    if (Bctrlkey) {
                        Alert ("Ctrl+up");
                        Break
                    }
                    Alert ("Up");
                    Break
                Case 39:
                    Alert ("right");
                    Break
                Case 40:
                    if (Bctrlkey) {
                        Alert ("Ctrl+down");
                        Break
                    }
                    Alert ("Down");
                    Break
                Case 49:
                    Bctrlkey && (Alert ("ctrl+1"));
                    Break
                Case 50:
                    Bctrlkey && (Alert ("ctrl+2"));
                    Break
                Case 51:
                    Bctrlkey && (Alert ("ctrl+3"));
                    Break
                Case 65:
                    if (Bctrlkey) {
                        if (Bshiftkey) {
                            Alert ("Custom");
                            Break
                        }
                        Alert ("Select All");
                        Break
                    }
                    Alert ("a");
                    Break
                Case 67:
                    Bctrlkey && (Alert ("Copy"));
                    Break
                Case 86:
                    Bctrlkey && (Alert ("paste"));
                    Break
            }
            return false;
        }
    </ Script >
</ Body >
</ HTML >

---------------------------------------------------------------------------------------------

<script language= "javascript" >
     function KeyEvent () {
     if (event.keycode==13)
     Alert ("#$%#%#^^%");
     }
     document. onkeydown = KeyEvent;
</script>
The Event.keycode value for 37,38,39,40 corresponds to the pressed direction key divided by left, top, right, bottom
"Javascript:if (Event.keycode < 45 | | Event.keycode >) Event.returnvalue = false;");
if (event.keycode==13) on behalf of the press is the carriage return keyboard
"javascript:if (Event.keycode > && event.keycode <) Event.returnvalue = false;");

----------------------------------------------------------------------------------------------

< reference: Http://lfsp.btwlo.com/keycode.html & http://www.cnblogs.com/xingqiba/archive/2012/06/12/2546148.html & http://www.cnblogs.com/kuikui/archive/2012/02/09/2343645.html>

JS KeyCode Event Response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.