Cancel page forward/back hotkeys in Firefox with Greasemonkey

Posted by Stimulating Pixels on Stack Overflow See other posts from Stack Overflow or by Stimulating Pixels
Published on 2010-05-30T02:03:08Z Indexed on 2010/05/30 2:12 UTC
Read the original article Hit count: 313

Filed under:
|
|

First the background:

In Firefox 3.6.3 on Mac OS X 10.5.8 when entering text into a standard the hotkey combination of Command+LeftArrow and Command+RightArrow jump the cursor to the start/end of the current line, respectively. However, when using CKEditor, FCKEditor and YUI Editor, Firefox does not seem to completely recognize that it's a text area. Instead, it drops back to the default function for those hotkeys which is to move back/forward in the browser history. After this occurs, the text in the editor is also cleared when you return to the page making it very easy to loose whatever is being worked on.

I'm attempting to write a greasemonkey script that I can use to capture the events and prevent the page forward/back jumps from being executed. So far, I've been able to see the events with the following used as a .user.js script in GreaseMonkey:

document.addEventListener('keypress', function (evt) {
// grab the meta key
var isCmd = evt.metaKey;

// check to see if it is pressed
if(isCmd)
{
  // if so, grab the key code;
  var kCode = evt.keyCode;

  if(kCode == 37 || kCode == 39)
  {
    alert(kCode);
  }
}

}, false );

When installed/enabled, pressing command+left|right arrow key pops an alert with the respective code, but as soon as the dialog box is closed, the browser executes the page forward/back move. I tried setting a new code with evt.keyCode = 0, but that didn't work.

So, the question is, can this Greasemonkey script be updated so that it prevents the back/forward page moves?

(NOTE: I'm open to other solutions as well. Doesn't have to be Greasemonkey, that's just the direction I've tried. The real goal is to be able to disable the forward/back hotkey functionality.)

© Stack Overflow or respective owner

Related posts about firefox

Related posts about greasemonkey