views:

263

answers:

1

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.)

+2  A: 

Add return false; after alert(kCode); That should do it most of the time.

May also have to add:

evt.preventDefault();
and/or
evt.stopPropagation();

These two functions work on most FF editions, but I'm not sure about the Mac. Also, usually, your func will fire after the editor script. But, if it fires before, it could disable that cursor jump.

Brock Adams
evt.preventDefault(); got it. It does, indeed disable the cursor jump as well, but I'm okay with that. I'd rather have to do that manually than risk loosing data. Full code is: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; // see if it's left or right arrow key if(kCode == 37 || kCode == 39) { // prevent the default from firing if it is. evt.preventDefault(); } }},false);
anotherAlan