-1

Im trying to make a keycode to redirect users to new page in javascript, but my code is not working. Does anyone have a solution?

if (keyCode == 17&&65) {
        window.location = "somerandompage.html";
    }
canary
  • 1
  • 1
  • @Samathingamajig Nope, that expression is equivalent to `(keyCode == 17) && 65`, also `17 && 65` evaluates to 65 not 17. –  Jun 01 '21 at 19:43
  • 1
    It seems like OP wants it so that pressing `Ctrl+a` to redirect – Samathingamajig Jun 01 '21 at 19:43
  • Does this answer your question? [Detect Ctrl + A in keyup event](https://stackoverflow.com/questions/31912784/detect-ctrl-a-in-keyup-event) – xGeo Jun 01 '21 at 19:45
  • OP, is this all you have? Or is this inside a window keydown listener? –  Jun 01 '21 at 19:47
  • Does this answer your question? [how detect CTRL+q in javascript](https://stackoverflow.com/questions/37510126/how-detect-ctrlq-in-javascript) –  Jun 01 '21 at 19:49

1 Answers1

0

It appears that you want it so that pressing Ctrl+a will redirect, which can be done by checking that a is pressed, and the Ctrl key is already pressed

window.addEventListener("keydown", event => {
  if (event.keyCode === 65 && event.ctrlKey) {
    alert("Ctrl+a pressed");
    window.location = "somerandompage.html";
  }
});
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
  • @Samathingamajig Please check for duplicates before answering (https://stackoverflow.com/questions/37510126/how-detect-ctrlq-in-javascript) –  Jun 01 '21 at 19:49