Skip to content

Instantly share code, notes, and snippets.

@bng44270
Created August 26, 2022 13:41
Show Gist options
  • Save bng44270/136280b6fa54adb6a107716efae3b9a7 to your computer and use it in GitHub Desktop.
Save bng44270/136280b6fa54adb6a107716efae3b9a7 to your computer and use it in GitHub Desktop.
Get JavaScript keycodes including Shift, Control and Alt combos
<html>
<head>
<title>Key Code Tester</title>
</head>
<body>
This is an interactive way to view JavaScript keyup and keydown keyCodes including the shift, alt, and ctrl modifier codes.<br/><br/>
Key Codes:<br/>
<div style="margin-left:50px;height:200px;width:200px;overflow:auto;" id="codeview"></div>
<script type="text/javascript">
document.body.onload = function() {
document.addEventListener('keyup',(e) => {
//Exclude shift, alt, and ctrl
if ([16,17,18].indexOf(e.keyCode) == -1) {
var mod = [];
if (e.ctrlKey) mod.push("ctrl=17");
if (e.altKey) mod.push("alt=18");
if (e.shiftKey) mod.push("shift=16");
document.getElementById('codeview').innerHTML = ((mod.length > 0) ? "(" + mod.join(",") + ") " : "") + e.key.toString() + '=&gt;' + e.keyCode.toString() + '<br/>' + document.getElementById('codeview').innerHTML;
}
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment