Skip to content

How to add events on keys #16

Answered by getlost01
Hasheditz asked this question in Q&A
Discussion options

You must be logged in to vote

Here is an example of how you can set up keybinding using the keyup event in JavaScript:

You can also use event.key to check what key has been pressed.

document.addEventListener('keyup', function(event) {
    if (event.key === 'a') {
        // Do something when the 'a' key is pressed
    } else if (event.key === 'b') {
        // Do something when the 'b' key is pressed
    }
});

This code will also listen for the keyup event on the entire document, and check the event.key property to see which key was pressed.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Castonati
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants