Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>JavaScript Key Press</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script>
- //wait for document to load
- document.addEventListener("DOMContentLoaded", function(){
- let output = document.querySelector("#output");
- });
- document.addEventListener('keydown', function( ev ) {
- output.innerHTML += ev.key + " DOWN<br>";
- //console.log(ev.key); //get keycode in console
- //clear on backspace
- if(ev.key == "Backspace"){
- output.innerHTML = "Cleared!<br>";
- }
- //scroll to bottom
- window.scrollTo(0,output.scrollHeight);
- });
- document.addEventListener('keyup', function( ev ) {
- output.innerHTML += ev.key + " UP<br>";
- //console.log(ev.key); //get keycode in console
- //scroll to bottom
- window.scrollTo(0,output.scrollHeight);
- });
- </script>
- </head>
- <body>
- <div id="output" style="font-size:50px"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement