如何判断CAPS锁是否在使用JavaScript?
onkeypress
如何判断CAPS锁是否在使用JavaScript?
onkeypress
function isCapslock(e){ e = (e) ? e : window.event; var charCode = false; if (e.which) { charCode = e.which; } else if (e.keyCode) { charCode = e.keyCode; } var shifton = false; if (e.shiftKey) { shifton = e.shiftKey; } else if (e.modifiers) { shifton = !!(e.modifiers & 4); } if (charCode >= 97 && charCode <= 122 && shifton) { return true; } if (charCode >= 65 && charCode <= 90 && !shifton) { return true; } return false;}
$('#example').keypress(function(e) { var s = String.fromCharCode( e.which ); if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) { alert('caps is on'); }});
s.toLowerCase() !== s
KeyboardEvent
getModifierState
passwordField.addEventListener( 'keydown', function( event ) { var caps = event.getModifierState && event.getModifierState( 'CapsLock' ); console.log( caps ); // true when you press the keyboard CapsLock key});
举报