[jQuery入門]event.keyでキーコードを判定するサンプル

2021年6月30日

書式
$(“#selectorID").keydown(function(event)
使用例

<input id="usrId" type="text" value="" maxlength="1" autocomplete="off">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
$("#usrId").keydown(function(event) {
      if (event.key === "Alt"){
            console.log("alt key is pressed");
      }
      if (event.key === "Enter"){
            console.log("Enter key is pressed");
      }
      if (event.key === "Shift"){
            console.log("shift key is pressed");
      }
      if (event.key === "Control"){
            console.log("ctrl key is pressed");
      }
});
</script>

 

jQuery

Posted by arkgame