-1

I need to set some event on cmd + y keys. It works, but always open history tab in chrome. How to prevent open history tab on mac using js in this case?

IWProgrammer
  • 155
  • 1
  • 9
  • Does this answer your question? [Jquery, Disable Browser Shortcut keys](https://stackoverflow.com/questions/23492793/jquery-disable-browser-shortcut-keys) – Justinas Apr 06 '23 at 09:43
  • No, preventDefault() not working – IWProgrammer Apr 06 '23 at 09:44
  • Also stopPropagation – IWProgrammer Apr 06 '23 at 09:44
  • 1
    Please update your question with the code you're working with. Otherwise it's impossible to figure out what you're trying to do. See [How to Ask](https://stackoverflow.com/help/how-to-ask) and [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) for more info. – Reyno Apr 06 '23 at 09:45

1 Answers1

1

You can use e.preventDefault() to disable default action of key as action of .onkeydown

document.onkeydown = function(e) {
  e.preventDefault();
  if (e.metaKey && e.key == 'y') {
    console.log('Cmd + Y pressed');
  }
}
<div>Mac: Press ⌘ + <kbd>Y</kbd></div>
<div>Windows: Press  + <kbd>Y</kbd></div>
Jordy
  • 1,802
  • 2
  • 6
  • 25