Advertisement
metalx1000

JavaScript Key Presses

Mar 17th, 2020
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <title>JavaScript Key Press</title>
  5.     <meta charset="utf-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <script>
  8.       //wait for document to load
  9.       document.addEventListener("DOMContentLoaded", function(){
  10.         let output = document.querySelector("#output");
  11.       });
  12.  
  13.       document.addEventListener('keydown', function( ev ) {
  14.         output.innerHTML += ev.key + " DOWN<br>";
  15.         //console.log(ev.key); //get keycode in console
  16.  
  17.         //clear on backspace
  18.         if(ev.key == "Backspace"){
  19.           output.innerHTML = "Cleared!<br>";
  20.         }
  21.  
  22.         //scroll to bottom
  23.         window.scrollTo(0,output.scrollHeight);
  24.       });
  25.  
  26.       document.addEventListener('keyup', function( ev ) {
  27.         output.innerHTML += ev.key + " UP<br>";
  28.         //console.log(ev.key); //get keycode in console
  29.  
  30.         //scroll to bottom
  31.         window.scrollTo(0,output.scrollHeight);
  32.       });
  33.     </script>
  34.   </head>
  35.   <body>
  36.     <div id="output" style="font-size:50px"></div>
  37.   </body>
  38. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement