keydown code example
Example 1: jQuery Arrow Keys Binding Examples
<html>
<head>
<title>jQuery Arrow Keys | Bind Arrow Keys using jQuery</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>
<body>
Hit the arrows keys on your keyboard to see the alert!
<script type="text/javascript">
$(document).keydown(function(e){
var keyCode = e.keyCode || e.which;
if (keyCode == 38)
{
alert( "Up arrow key hit." );
return false;
}
if (keyCode == 40)
{
alert( "Down arrow key hit." );
return false;
}
if (keyCode == 37)
{
alert( "Left arrow key hit." );
return false;
}
if (keyCode == 39)
{
alert( "Right arrow key hit." );
return false;
}
});
</script>
</body>
</html>
Example 2: javascript keydown
document.addEventListener("keydown", function(event) {
if (event.keyCode == 32) {
console.log("Space was pressed!");
}
});
Example 3: keydown events
eventTarget.addEventListener("keydown", event => {
if (event.isComposing || event.keyCode === 229) {
return;
}
// do something
});
Example 4: keydown events
document.addEventListener('keydown', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}
Example 5: what is keyup and keydown
Keydown and keyup. The keydown events happens when a key is pressed down, and then keyup – when it's released
Example 6: event listener for keydown
eventTarget.addEventListener("keydown", event => {
if (event.isComposing || event.keyCode === 229) {
return;
}
// do something
});