Overriding the F1 key to FF, chrome, i.e. Is F1 on ie8 / chrome on the keyboard and in FF when a key is pressed? How about <tab> in the input field?

I am creating events with names with jquery. When I use the following function with code = 112, the bool = false function, everything works fine in FF, and the F1 key is sent to my function and the event does not bubble to open firefox help in a new tab.

function bindKeyFunc(code, func, bool){
    $(document).bind('keypress.' + code, function(e){
        var c = (e.keyCode ? e.keyCode : e.which);
        //console.log(c);
        if ( code == c) {
            //e.stopPropagation();
            //e.preventDefault();
            func();
            return bool;
        }
    });
}

In chrome and ie8, my event listener does not start, and instead, the usual help appears even if I uncomment the calls to stopPropagation and preventDefault.

Similarly, when I try to take the key <tab>for my purposes, it works fine in FF, but my event does not fire in chrome or ie8, and instead the default action for the tab is valid:

    $('input#manual-total').bind('keypress.dep', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if ( code==9 ){
            $('div#formset').show();
            $(next).focus()[0].select();
            return false;
        }
    });
+3
4

. - . , IE, , . onkeydown/up IE. , - . , - F1. ; , , , .

, , " , , ". .:)

+1

Simple document.addEventListenerin keydownworked for me in Chrome.

var util = { };

document.addEventListener('keydown', function(e){

    var key = util.key[e.which];
    if( key ){
        e.preventDefault();
    }

    if( key === 'F1' ){        
      // do stuff
    }
})

util.key = { 
  9: "tab",
  13: "enter",
  16: "shift",
  18: "alt",
  27: "esc",
  33: "rePag",
  34: "avPag",
  35: "end",
  36: "home",
  37: "left",
  38: "up",
  39: "right",
  40: "down",
  112: "F1",
  113: "F2",
  114: "F3",
  115: "F4",
  116: "F5",
  117: "F6",
  118: "F7",
  119: "F8",
  120: "F9",
  121: "F10",
  122: "F11",
  123: "F12"
}
+1
source

Use keydown instead of keyup with preventDefault because chrome uses keydown if you used the keyup chome start page.

    if (e.key === "F1") {
        e.preventDefault();
        //Do Code Here
    };
0
source

Source: https://habr.com/ru/post/1718482/


All Articles