JavaScript Advanced series 07, mouse events

Source: Internet
Author: User

Mouse events are KeyDown, Keyup, Keypress, but Keypress differs from KeyDown and Keyup if you press CTRL, SHIFT, CAPS LOCK ... Such modifier keys do not trigger the KeyPress event, but trigger KeyDown and KeyUp events, which is the difference between the KeyPress event and the KeyDown and KeyUp events. In addition, the KeyPress event is typically used to obtain user input information.

Continue to use the "JavaScript Advanced series 05, event execution time, use AddEventListener to register multiple events at the same time, event arguments" in the cross-browser event handling mechanism to trigger mouse events.

Creating Script2.js only allows you to enter uppercase and lowercase letters to register the KeyPress event method.

(function () {
    var txtbox = document.getElementById ("txtinput");
    Eventutility.addevent (Txtbox, "keypress", function (evt) {
        var code = Eventutility.getcharcode (EVT);
        In case of uppercase or lowercase letters
        if (Code >= && Code <= 90) | | (Code >= && Code <= 122)) {
            
        Else {
            Eventutility.preventdefault (EVT);
        }
    });
}());

Page section:

    <input type= "text" id= "txtinput"/>
    <script src= "eventutility.js" ></script>
    <script src= "script2.js" ></script>

Output: Enter non-uppercase and lowercase letters on the keyboard, input has no response

ASCII code corresponding to non-modifier keys:
A-z:65-90
A-Z: 97-122
0-9:48-57

In fact, using KeyDown or KeyUp events can also achieve the same effect. Modify the Script2.js as follows:

(function () {
    var txtbox = document.getElementById ("txtinput");
    Eventutility.addevent (Txtbox, "keydown", function (evt) {
        var code = Evt.keycode;
        In case of uppercase or lowercase letters
        if (Code >= && Code <= 90) | | (Code >= && Code <= 122)) {
            
        Else {
            Eventutility.preventdefault (EVT);
        }
    });
}());

Unlike the KeyPress event, the KeyUp and KeyDown events get the non-decorated keys pressed by the keyboard through the KeyCode property. In addition, by Evt.altkey, Evt.ctrlkey, Evt.shiftkey gets the ALT key pressed, the CTRL key, and the SHIFT key.

Modify the Scrip2.js to determine if the "ctrl+b" key combination is pressed.

(function () {
    Eventutility.addevent (document, "keydown", function (evt) {
        var code = Evt.keycode;
        var ctrlkey = Evt.ctrlkey;
        if (ctrlkey && Code = = = 66) {
            Alert (' Pressed the ctrl+b key combination ');
        }
    });
}());

The "JavaScript Advanced series" includes:

JavaScript Advanced Series 01, function declaration, function parameter, function closure JavaScript advanced series 02, function as parameter and application in array JavaScript advanced series 03, by hard coding, Factory mode, Constructor create JavaScript Object JavaScript Advanced series 04, function parameter number of uncertain case solution JavaScript Advanced series 05, event execution time, use AddEventListener to register multiple events for the element at the same time, Event Parameters JavaScript Advanced Series 06, event delegate JavaScript Advanced series 07, mouse events

JavaScript Advanced series 07, mouse events

Related Article

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.