Keyboard Events - Keydown/Keyup
UI Events: Keyboard Events
What is a keyboard event in JavaScript?
View Answer:
document.addEventListener('keydown', function(event) {
console.log(event.keyCode);
});
What are the three main types of keyboard events?
View Answer:
// keydown event
document.addEventListener('keydown', function(event) {
// Get the key that was pressed
var key = event.keyCode;
// Do something with the key
if (key == 65) {
// The A key was pressed
}
});
// keypress event
document.addEventListener('keypress', function(event) {
// Get the key that was pressed
var key = event.keyCode;
var character = event.key;
// Do something with the key
if (character == 'a') {
// The letter a was pressed
}
});
// keyup event
document.addEventListener('keyup', function(event) {
// Get the key that was released
var key = event.keyCode;
// Do something with the key
if (key == 65) {
// The A key was released
}
});
These are just a few examples of how to use keydown , keypress , and keyup events in JavaScript. There are many other things that you can do with these events, so be creative and experiment!
How does 'keydown' event differ from 'keyup'?
View Answer:
document.onkeydown = function(event) {
console.log('Key pressed:', event.key);
}
document.onkeyup = function(event) {
console.log('Key released:', event.key);
}
What is the purpose of 'event.keyCode' in keyboard events?
View Answer:
It should be noted that event.keyCode is deprecated and should no longer be used, but you may come across in older code.
How does the 'keypress' event differ from 'keydown' and 'keyup'?
View Answer:
document.onkeydown = function(event) {
console.log('Key down:', event.key);
}
document.onkeypress = function(event) {
console.log('Key press:', event.key);
}
document.onkeyup = function(event) {
console.log('Key up:', event.key);
}
Why has 'event.keyCode' been deprecated?
View Answer:
What is the purpose of 'event.preventDefault()' in keyboard events?
View Answer:
Can you capture a keyboard event on any HTML element?
View Answer:
// Global event listener on the document
document.onkeydown = function(event) {
console.log('Key down:', event.key);
}
// Making a <div> focusable and capturing an event
let div = document.getElementById('myDiv');
div.setAttribute('tabindex', '0');
div.onkeydown = function(event) {
console.log('Key down in div:', event.key);
}
What is the use of 'event.repeat' property in keyboard events?
View Answer:
document.onkeydown = function(event) {
if (event.repeat) {
console.log('Key down (repeating):', event.key);
} else {
console.log('Key down:', event.key);
}
}
In this example, when a key is held down and starts repeating, the 'keydown' event logs a message indicating that the key is repeating. If the key is just pressed once and not held down, it logs a normal 'Key down' message.
What are modifier keys in keyboard events?
View Answer:
How can you check if a modifier key was pressed during a keyboard event?
View Answer:
document.onkeydown = function(event) {
if (event.shiftKey) {
console.log('Shift key was pressed');
}
if (event.ctrlKey) {
console.log('Ctrl key was pressed');
}
if (event.altKey) {
console.log('Alt key was pressed');
}
if (event.metaKey) {
console.log('Meta key (e.g., Command key on macOS) was pressed');
}
}
In this example, the event object's properties are checked to determine if the Shift, Ctrl, Alt, or Meta (Command) key was pressed during the 'keydown' event. The corresponding message is logged to the console accordingly.
What is a 'hotkey'?
View Answer:
document.onkeydown = function(event) {
if (event.ctrlKey && event.key === 's') {
event.preventDefault(); // Prevent the default browser save action
console.log('Save action triggered');
}
if (event.altKey && event.key === 'F4') {
event.preventDefault(); // Prevent the default browser close action
console.log('Application close action triggered');
}
}
In this example, if the user presses Ctrl+S, the 'Save action triggered' message is logged to the console. Similarly, if the user presses Alt+F4, the 'Application close action triggered' message is logged. The event.ctrlKey
and event.altKey
properties are used to check if the respective modifier keys are pressed, and event.key
is used to check the specific key that was pressed.
Can you stop a keyboard event from propagating?
View Answer:
What is the order of execution for 'keydown', 'keypress', and 'keyup' events?
View Answer:
When should you use keyboard events during user interaction?
View Answer:
What do the event.code and event.key properties do?
View Answer:
For example, the same key Z can be suppressed with or without Shift. This behavior results in two distinct characters: lowercase z and uppercase Z.
Key | event.key | event.code |
---|---|---|
Z | z (lowercase) | KeyZ |
Shift+Z | Z (uppercase) | KeyZ |
Is case sensitivity and the correct formatting crucial when dealing with 'event.code' in JavaScript? If so, how should it be formatted?
View Answer:
document.onkeydown = function(event) {
if (event.code === 'KeyA') {
console.log('Key A pressed');
}
}
In this example, the event.code
is compared to the string 'KeyA'
, using uppercase letters as specified by the standard format. This ensures accurate detection of the specific key.
What happens when a key does not have a letter-based character?
View Answer:
Example:
Key | event.key | event.code |
---|---|---|
F1 | F1 | F1 |
Backspace | Backspace | Backspace |
Shift | Shift | ShiftRight or ShiftLeft |