JS get keyboard pressed key value Event.keycode,event.charcode,event.which compatibility

Source: Internet
Author: User

JS get keyboard pressed key values are Event.keycode,event.charcode and Event.which

which

Google Chrome is compatible with Event.keycode,event.charcode and Event.which.

Firefox browser to event.keycode part of the key value, such as the next key (37,38,39,40), enter key (34), PgUp (PgDn), and other parts of the effective, the number keys, the letter key is invalid.

Event.which is also part of the key value valid, such as the letter key, number keys, enter key, backspace key and so on, on the next key, PgUp (), PgDn (34) key is invalid.

Event.charcode is also valid for some key values, such as the letter key, the number key, the BACKSPACE key and so on, on the ENTER key, the next key, PgUp (), PgDn (34) key is invalid.

In Internet Explorer, IE8 and the following browsers are not valid for Event.charcode, Event.keycode and Event.which are available for most key values, but few can be obtained

Network View said Event.which will Event.keycode and Event.charcode standardized. But there is a difference in reality.

So it's generally written in a compatible way:

e.keycode| | e.which| | E.charcode;

jquery API Address: http://api.jquery.com/event.which/

The following example shows how a query restricts text boxes to enter only numbers and decimal points

$(function(){        /*JQuery limit text box can only enter numbers*/        $(". Numtext "). KeyUp (function(){            $( This). Val ($ ( This). Val (). replace (/[^0-9]/g, ")); }). bind ("Paste",function(){//ctr+v Event Handling$( This). Val ($ ( This). Val (). replace (/[^0-9]/g, ")); }). css ("Ime-mode", "disabled");//CSS Settings ime not available        /*JQuery limit text box can only enter numbers and decimal points*/        $(". Numdectext "). KeyUp (function(){            $( This). Val ($ ( This). Val (). replace (/[^0-9.] /g, ")); }). bind ("Paste",function(){//ctr+v Event Handling$( This). Val ($ ( This). Val (). replace (/[^0-9.] /g, ")); }). css ("Ime-mode", "disabled");//CSS Settings ime not available});

The KeyCode table is as follows:

JS get keyboard pressed key value Event.keycode,event.charcode,event.which compatibility

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.