Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding shortcut keys in Firefox and Chrome

I have a script that is supposed to open a section of a web page, and save changes on Ctrl + n and Ctrl + s respectively. I got it working in IE, but it doesn't seem to work in Firefox and Chrome. Any ideas?

My override function.

function prevent(e)
{
try{e.stopPropagation();}catch(ex){}
try{e.preventDefault()}catch(ex){}
try{if (e.preventDefault)
        e.preventDefault();
    else {
        e.cancelBubble = true;
        e.returnValue = false;
        e.keyCode = 0;
    }}  catch(ex){}
}
like image 625
wolfcall Avatar asked Apr 09 '13 20:04

wolfcall


People also ask

How do I override a Firefox shortcut?

All Replies (2) You would have to use an autoconfig. cfg file to disable a built-in keyboard shortcut. You can use the Browser Toolbox to find the ID of the shortcut you want to disable or override.

What is override keyboard shortcuts?

Override or define the keyboard shortcut Override or define a new keyboard shortcut behavior instead of preventing the keyboard shortcut. For example, Ctrl + S keyboard shortcut saves the document in SFDT format by default, and there is no behavior for Ctrl + Alt + S .

Can you change Chrome keyboard shortcuts?

Customize Chrome with these tips Just go to the 'Shortcuts' dialog in the Customize menu. Open a new tab. At the bottom right, click Customize. Click Shortcuts.

How do I customize Firefox shortcuts?

In your New Tab page, click the Personalize button. Under Shortcuts, select the number of Shortcut rows you want to see from the drop-down menu.


1 Answers

I have seen the same issue. Some browsers will not allow you to capture certain shortcuts. Look at this https://stackoverflow.com/a/7296303/1366887

Some key combinations are resticted in Chrome 4, but not in Chrome 3. Look here: https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-bugs/Ntc1byZXHfU

Here is the Javascript:

$(window).keydown(function(event) {
  if(event.ctrlKey && event.keyCode == 84) { 
    console.log("Hey! Ctrl+T event captured!");
    event.preventDefault(); 
  }
  if(event.ctrlKey && event.keyCode == 83) { 
    console.log("Hey! Ctrl+S event captured!");
    event.preventDefault(); 
  }
});

I have used this numerous times, and it has worked greatly.

Here is another rescource you can take a look at: http://unixpapa.com/js/key.html

Without Jquery:

onkeydown = function(e){
  if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)){
    e.preventDefault();
    //your saving code
  }
}

Here is a JSFIDDLE of it working.

like image 103
Hunter Mitchell Avatar answered Sep 27 '22 21:09

Hunter Mitchell