Maksim Kerbel
Maksim Kerbel

Reputation: 41

How to prevent default Shortcut alt+b by Firefox and IE via JS?

I need to implement a shortcut alt+b, calling a function. My problem is, that everytime i press this shortcut, a Firefox and an IE open a Menu-Bar "Edit". Is there any solution to prevent this default behavior? Or maybe it is possible to close this menu-bar after calling a function?

What i have tried but without success

     $(document).keydown(function(e) {

         if (e.keyCode == 18 || e.which==18)
         {
            e.preventDefault(); 
         }           
      });

Upvotes: 3

Views: 2136

Answers (2)

Halcyon
Halcyon

Reputation: 57693

This seems to work for me:

$(document).keydown(function(e) {

    // check for alt+b
    if (e.keyCode == 66 && e.altKey === true) {
        e.preventDefault(); 
    }
});

You need to check the press of b, not alt.

Upvotes: 0

Try this, it will only run the console.log() if you hit Alt+B. Replace the console with anything you want.

$(window).keydown(function(event) {
  if(event.altKey && event.keyCode == 66) { 
    event.preventDefault(); 
    console.log("Hey! alt+B event captured!");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions