jquery implements the keyboard listener program code

Source: Internet
Author: User

Example

The code is as follows Copy Code
<script src= "jquery-1.11.0.min.js" type= "Text/javascript" ></script>


<script type= "Text/javascript" >


 


//Get keyboard ASCII code;


//$ (document). KeyDown (function (event) {


//Alert (Console.log (Event.keycode));


//alert (Event.keycode);


        //});


 


$ (document). KeyDown (function (event) {


Judge when Event.keycode is 37 o'clock (that is, left side key), execute function to_left ();


//Judge when Event.keycode is 39 o'clock (that is, right side key), execute function to_right ();


if (Event.keycode = = 37) {


alert (' left ');


} else if (Event.keycode = 39) {


alert (' right ');


            }


else if (Event.keycode = 27) {


Alert (' Esc key ');


            }


else if (Event.keycode = 13) {


alert (' Enter ');


            }


else {


alert (' The ASCII code of this button, is ' + Event.keycode ');


            }


        });


</script>


<!--


The KeyDown event is triggered when the keyboard is pressed.

The
KeyUp event triggers when the key is released, which is the event that you press the keyboard up

The
KeyPress event triggers when the keystroke is hit, which we can understand as pressing and lifting the same button


-->

Binding events in the application more monitoring, such as we use the upper and lower pages to implement the keyboard by the right and left, the following collation of the similar examples.


The logic of implementing the effect is relatively simple, that is, the Slidedown () method,
JS Code Collection Code

  code is as follows copy code
//shows a given element and hides all Others  &nbs P;&NBSP
Function showviakeypress (element_id)     
{    
     $ (". Container"). CSS ("display", "none");     
    $ (element_id). Slidedown ("slow");     
}    
    
//Shows Proper DIV depending on link ' href '     
function Showvialink (array)     
{    
    Array.each (function (i)     
     {       
        $ (this). Click (function ()     
        {    
            var target = $ (this). attr ("href");     
             $ (". Container"). CSS ("display", "none");     
             $ (target). Slidedown ("slow");     
        });     
   });     
}    




and the keyboard key to monitor the use of the KeyPress () method, in fact, there is no difficulty, but we rarely use the button on the page to monitor, this example is more novel, it is worth our reference, if necessary, can be used in the project.
JS Code Collection Code

The code is as follows Copy Code
$ (document). KeyPress (function (e)


    {    


switch (E.which)


        {    


//user presses the "a"


case 97:showviakeypress ("#home");


Break


                            


//user presses the "s" Key


case 115:showviakeypress ("#about");


break;





//user presses the "D" key


case 100:showviakeypress ("#contact");


break;


                            


//user presses the "F" key


Case 102:showviakeypress ("#awards");


break;


                            


//user presses the "G" Key


case 103:showviakeypress ("#links");


        }    


    });

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.