Skip to content
Advertisement

Execute code whenever the space bar is pressed?

I am making this kind of a game but it isn’t really a game so basically I want this to run every time I hit space but it doesn’t work no matter what I try so I would be really thankful if somebody could have helped me out on this.

JavaScript

Advertisement

Answer

You need to put the if statement in a while loop. But ALSO be sure to have some kind of exit code. Below, I used the keypress esc to stop the while loop:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement