A simple JavaScript function to handle key events and display the pressed key and its key code dynamically in the HTML.
This project provides a straightforward JavaScript function (checkKeycode
) that listens for keyboard events and updates the content of an HTML element with the class "keycode" to display the pressed key and its corresponding key code. The code is written using modern JavaScript syntax and is well-documented for ease of use.
- Dynamic display of pressed key and key code.
- Support for modern browsers using the 'key' property and a fallback for older browsers using 'keyCode'.
- Additional handling for the space key to display "Space" instead of an empty string.
View the demo on CodePen.
-
Include the JavaScript file in your HTML:
<script src="path/to/script.js"></script>
-
Ensure that you have an HTML element with the class "keycode" in your document:
<div class="keycode"></div>
-
Add an event listener to call the
checkKeycode
function on keyboard events:document.addEventListener("keydown", checkKeycode);
-
Clone the repository:
git clone https://github.com/Karl-Horning/key-codes-keyboard-event-handler.git
-
Copy the
script.js
file into your project. -
Include the script in your HTML file as mentioned in the usage section.
Contributions are welcome! Please follow these guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure that the project still works.
- Create a pull request with a clear description of your changes.
This project is licensed under the MIT License.
- Font: Alfa Slab One.
- Background Animation: Manuel Pinto's Pure CSS Gradient Background Animation.
For any inquiries or support related to this project, please contact Karl Horning using any of the links in the 'Author' section.