JS to determine whether a key is long pressed

  • 2021-01-14 05:41:16
  • OfStack

This article illustrates JS's method for determining whether a 1 key is long pressed. To share with you for your reference, as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script type="text/javascript">
    var lastkeyCode = -1;
    function TestKeyDown(event) {
      if (event.keyCode == lastkeyCode) {
        alert(" A key was pressed repeatedly !");
        // To deal with 
      }
      lastkeyCode = event.keyCode;
    }
  </script>
</head>
<body>
  <input type="text" value="Test" onkeydown="TestKeyDown(event);" />
</body>
</html>

More about JavaScript related content interested readers can view the site features: "JavaScript search algorithm skills summary", "JavaScript animation effects and skills summary", "JavaScript error and debugging skills summary", "JavaScript data structure and algorithm skills summary", "JavaScript eraser algorithm and skills summary" and "JavaScript mathematical operation usage summary"

I hope this article is helpful to JavaScript program design.


Related articles: