'Pause menu in JavaScript?
I'm trying to make a pause menu for a game me and my friends are making, I was wondering if js has a simple way to make a keypress function that pauses the game? are they any? thank you!
Solution 1:[1]
Try adding an event listener with the key you want to have the game pause:
// adds an event listener to the document that listens for key presses
document.addEventListener("keydown", function(e) {
// if desired key pressed, pause
// Replace Escape with the key you want to execute the pause function
if (e.key === "Escape") {
pause();
// else do nothing
} else {}
});
To find the key code for the key you want to execute the function, I recommend using keycode.info.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |