特定のフィールドだけでなく、ページ全体のキャップス ロック キーの状態を監視する capslockstate と呼ばれる jQuery プラグインがあります。
Caps Lock キーの状態を照会するか、状態の変化に反応するイベント リスナーを定義できます。
このプラグインは、英語以外のキーボードでの作業、Caps Lock キー自体の使用の監視、アルファベット以外の文字が入力された場合に状態を忘れないなど、ここでの他の提案よりも検出と状態管理の優れた仕事をします.
2 つのデモがあり、1 つは基本的なイベント バインディングを示し、もう1 つはパスワード フィールドにフォーカスがある場合にのみ警告を示します。
例えば
$(document).ready(function() {
/*
* Bind to capslockstate events and update display based on state
*/
$(window).bind("capsOn", function(event) {
$("#statetext").html("on");
});
$(window).bind("capsOff", function(event) {
$("#statetext").html("off");
});
$(window).bind("capsUnknown", function(event) {
$("#statetext").html("unknown");
});
/*
* Additional event notifying there has been a change, but not the state
*/
$(window).bind("capsChanged", function(event) {
$("#changetext").html("changed").show().fadeOut();
});
/*
* Initialize the capslockstate plugin.
* Monitoring is happening at the window level.
*/
$(window).capslockstate();
// Call the "state" method to retreive the state at page load
var initialState = $(window).capslockstate("state");
$("#statetext").html(initialState);});
と
$(document).ready(function() {
/*
* Bind to capslockstate events and update display based on state
*/
$(window).bind("capsOn", function(event) {
if ($("#Passwd:focus").length > 0) {
$("#capsWarning").show();
}
});
$(window).bind("capsOff capsUnknown", function(event) {
$("#capsWarning").hide();
});
$("#Passwd").bind("focusout", function(event) {
$("#capsWarning").hide();
});
$("#Passwd").bind("focusin", function(event) {
if ($(window).capslockstate("state") === true) {
$("#capsWarning").show();
}
});
/*
* Initialize the capslockstate plugin.
* Monitoring is happening at the window level.
*/
$(window).capslockstate();});
プラグインのコードはGitHub で表示できます。