0

I'm practicing on keyboard events and I have the first four keys working properly but I cant make it work on the space tab. What it needs to do is to call the drawMine function at its given coordinates. What am I missing? Please see code below:

function keyDown(e) {
if (e.key == "ArrowDown" && truckY <= 300) {
    truckY += 50;
} else if (e.key == "ArrowUp" && truckY >= 50) {
    truckY -= 50;
} else if (e.key == "ArrowRight" && truckX <= 300) {
    truckX += 50;
} else if (e.key == "ArrowLeft" && truckX >= 50) {
    truckX -= 50;

} else if (e.key == " ") {
    let mineX = 110;
   let mineY = 110;
    drawMine(mineX, mineY);
} 
Baby Shark
  • 11
  • 2
  • Does this answer your question? [Java KeyPressed - Can't Detect If Spacebar is Being Pressed If Other Keys Are Too](https://stackoverflow.com/questions/41201835/java-keypressed-cant-detect-if-spacebar-is-being-pressed-if-other-keys-are-to) – mert dökümcü Apr 09 '21 at 18:03

1 Answers1

1

From the few tags on the question, it's unclear what is the real context, but I assume that it's JavaScript running in a browser. Which one is it?

According to Félix in his answer https://stackoverflow.com/a/6199224/232943 , you may have to do the following condition if you're testing in Internet Explorer 9 or Firefox < 37:

e.key === ' ' || e.key === 'Spacebar'

To confirm what's going on, I suggest to set a break-point in your debugger (or using a console.log) to see which e.key value is generated when you press the spacebar on your keyboard.

Delapouite
  • 9,469
  • 5
  • 36
  • 41