Search code examples
javascriptgoogle-chromepreventdefault

Shortcut Key commands in Chrome not preventing default action


So I am writing a shortcut library and for the most part it works, except for the issue I found right off the bat is in Chrome (haven't tested other browsers since I'm on a chrome book) is that when pressing ctrl+n it creates a new browser window. Basically in jist my code checks if the current key selection is defined and if it is preventDefault and run the exec function of that command.

ie.

if(joinedKeys in commands.cmd)
    e.preventDefault();
    commands.cmd[joinedKeys].exec();

I've even tried just doing this-

document.addEventListener("keydown",function(e){
    e.preventDefault();
});
//as well as window.addEvent...

Neither work. Any suggestions as to stop the default action of the browsers?


Solution

  • See https://stackoverflow.com/a/7296303/5298696

    In Chrome4, certain control key combinations have been reserved for browser usage only and can no longer be intercepted by the client side JavaScript in the web page. These restrictions did not exist in Chrome3 and are inconsistent with both Firefox3/3.5 and IE7/8 (on Windows).