1

I have this extremely basic example here: http://jsfiddle.net/arhVd/1/

<form>
  <input type="text">
  <input type="submit">
</form>

$(function () {

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

        e.preventDefault();

        $('form').submit();
    });
});

I want to ensure that when pressing F4 it does not do the built in browser function (in F4's case set focus to the URL bar. Or perhaps F3 showing the 'Find' bar.) The functionality of submitting the form still works, I just don't want the browser functionality getting in the way.

This is for an internal app where the function keys are supposed to function has HotKeys in the app.

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
aherrick
  • 19,799
  • 33
  • 112
  • 188

2 Answers2

5

This seems to work for F3 in IE8:

$(document).keydown(function(event) {
  event.preventDefault();

  if (event.keyCode == 114) { // F3
    // Remap F3 to some other key that the browser doesn't care about
    event.originalEvent.keyCode = 0;
  }
};

(based on http://www.fixya.com/support/t218276-disable_f3_function_key_from_intenet)

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
0

I don't believe you can hi-jack the user's browser.

John Giotta
  • 16,432
  • 7
  • 52
  • 82
  • Actually I know this is possible to prevent the default action of keys. – aherrick Nov 04 '10 at 18:36
  • I definitely wouldn't count on being able to do this. A simple google search indicates that different browsers process different function keys differently, and that some of them cannot be overriden (eg, F1, F5) – Dave Nov 04 '10 at 18:37
  • 1
    Also I'm not talking about overriding the keys, I just want to prevent their default action. I'm seeing it here with raw JS. I'm just looking for a simple jQuery solution with the event object jQuery passes. http://stackoverflow.com/questions/1492080/disable-f5-key-in-safari-4 – aherrick Nov 04 '10 at 18:38
  • I just tested key event arresting in IE 6 and document failed to register the keypress. It works in FF 3.6.12. – John Giotta Nov 04 '10 at 19:15
  • There is more info in the link to the disable f5 key in safari 4 SO question then there was in my answer. Pulling it down. – Dennis Burton Nov 04 '10 at 19:20
  • @aherrick - you can't do this reliably cross-browser..."I just want to prevent their default action"....and *how* is this not hijacking the keys? A user presses F1 for help, they expect help...you're violating every reasonable expectation of the user trying to prevent it otherwise. Newer browsers **actively prevent you from doing this**, and they're 100% correct in doing so. – Nick Craver Nov 04 '10 at 19:35