0

I want to disable CTRL + S in the browser, so I can save my game, but it doesn't work.

The code looks like this right now:

document.addEventListener("keydown", function(event) {
  if (event.ctrlKey && event.which == 83) { // ctrl + s
    event.preventDefault();
    saveGame();
  }
}, false);

The code for saveGame() is the following:

function saveGame() {
  var gameSave = {
    score: score,
    clickingPower: clickingPower,
    costGoku: costGoku,
    gokus: gokus,
    costVerbalase: costVerbalase,
    verbalases: verbalases,
    costSkyview: costSkyview,
    skyviews: skyviews
  };
  localStorage.setItem("gameSave", JSON.stringify(gameSave));
}

The saveGame() function works perfectly fine, but I still wanted to provide it so you can see what it does for clarification. When I press CTRL + S on my website it still wants to save the page and is not saving the game.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
umbreonben
  • 33
  • 4

2 Answers2

0

You need to stop the propagating, you can either do it with event.stopPropagation(); or with return false;

document.addEventListener("keydown", function(event) {
            if (event.ctrlKey && event.which == 83) { // ctrl + s
                event.preventDefault();
                event.stopPropagation();
                saveGame();

        
            }
        }, false);

Or with return false;

document.addEventListener("keydown", function(event) {
        if (event.ctrlKey && event.which == 83) { // ctrl + s
            event.preventDefault();
            saveGame();
            return false;
    
        }
    }, false);

document.addEventListener("keydown", function(event) {
  if (event.ctrlKey && event.which == 83) { // ctrl + s
    event.preventDefault();
    event.stopPropagation();
    console.log("saveGame()");


  }
}, false);
Ram Segev
  • 2,563
  • 2
  • 12
  • 24
0

I literally just copied your code , into a plain html file, and it works just great.

<html>
  <head>
  </head>
  <body>

    <script>

      document.addEventListener("keydown", function (event) {

        console.log("keydown");
        console.log(event);

        if (event.ctrlKey && event.which == 83) { // ctrl + s
            event.preventDefault();
            console.log("CTRL + S pressed !")
            saveGame()
        }

      }, true);

      function saveGame() {
        var gameSave = {
            score: 1,
            clickingPower: 2,
            costGoku: 3,
            gokus: 4,
            costVerbalase: 5,
            verbalases: 6,
            costSkyview: 7,
            skyviews: 8
        };
        localStorage.setItem("gameSave", JSON.stringify(gameSave));
        console.log("game saved !")
      }

    </script>

  </body>
</html>

I suggest doing the same on your end and see what happens. If it works here and not in the other project, you need to include more information, like what environment your in, how did you setup your project, what browser you are using, what OS and so on.

turbopasi
  • 3,327
  • 2
  • 16
  • 43