0

I am trying to capture Shift+F8 in my JQuery application & want it to propagate down. I have read many posts but somehow I am not able to catch Shift+F8 key at all. I found 1 useful lib here but it is able to find 'Shift+F2/F3/...' but not 'Shift+F8' particularly. any suggestions please & thanks in advance.

Environment : Windows & IE (11 & 8)

NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • 1. Which browser are you using? 2. Which event are you using? 3. do you have the browser console open? 4. what have you actually tried? 5. works fine here: https://jsfiddle.net/L7tk582o/ – freedomn-m Apr 24 '19 at 08:16
  • IE8? Might have difficulty finding anyone that still has a browser that Microsoft themselves abandoned over three years ago available to help you. – freedomn-m Apr 24 '19 at 08:26
  • @freedomn-m: IE11 is fine for me but need to support IE8 too. – NDestiny Apr 24 '19 at 08:58

2 Answers2

1

On any modern browser, you can use a keydown or keyup event handler like this one:

document.addEventListener("keydown", function(e) {
    if (e.key === "F8" && e.shiftKey) {
        e.preventDefault();
        // ...Shift+F8 was pressed...
    }
});

or with jQuery:

$(document).on("keydown", function(e) {
    if (e.key === "F8" && e.shiftKey) {
        e.preventDefault();
        // ...Shift+F8 was pressed...
    }
});

Whether that works may be browser-specific, because different browsers reserve different key combinations. It works on Chrome and Firefox on my copy of Linux, and on Edge on Windows 10, and IE11 on Windows 8. Note that you have to click inside the window to ensure it has focus.

You've tagged . If you really need to support that truly obsolete browser, which doesn't have addEventListener, use a version of jQuery that still supports IE8 (or the hookEvent function from this answer, which works on IE8 [or even IE6]) and then instead of using e.key, use e.which || e.keyCode and the appropriate keycode. The reason we have e.key now is that "the appropriate keycode" varies by keyboard layout. On my keyboard, F8 is 119, so:

// For obsolete browsers without `addEventListener`
hookEvent(document, "keydown", function(e) {
    if ((e.which || e.keyCode) === 119 && e.shiftKey) {
        e.preventDefault();
        // ...Shift+F8 was pressed...
    }
});

or using an older version of jQuery that supports IE8:

// For obsolete browsers
$(document).on("keydown", function(e) {
//               ^---- if it's a REALLY old version, you might need `bind` instead of `on`
    if ((e.which || e.keyCode) === 119 && e.shiftKey) {
        e.preventDefault();
        // ...Shift+F8 was pressed...
    }
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • i am working on IE11 & this code is not working on IE11. I am able to capture Shift+F2 on chrome but could not capture Shift+F8 at all. – NDestiny Apr 24 '19 at 08:56
  • @SchokokuchenBäcker did it work for Shift+F8 on chrome ? – NDestiny Apr 24 '19 at 08:57
  • @NDestiny - It does if you ensure that the body element has focus. I've removed `.body` from the above, because hooking the event on `document` directly works better on IE11. You need to be sure to click inside the window. – T.J. Crowder Apr 24 '19 at 09:07
  • @T.J.Crowder Shift+F8 in not working in chrome (73.0.3683.103) but Shift+F2 is working. nothing works in IE11. – NDestiny Apr 24 '19 at 09:14
  • @NDestiny - Both work for me. Again, you need to click inside the window. – T.J. Crowder Apr 24 '19 at 09:15
  • @T.J.Crowder Yes i have focus but not working, is it windows10 ? – NDestiny Apr 24 '19 at 09:17
  • @T.J.Crowder not working for me, I am on win10 IE11 (11.648.17134.0CO) – NDestiny Apr 24 '19 at 09:19
  • @NDestiny - As i said in the answer, I've tested this in Chrome and Firefox on Linux, on Edge in Windows 10, and on IE11 in Windows 8. It works in those environments, and I find it hard to believe it's a Windows 10 + IE11 thing. Perhaps something to do with your locale, or perhaps you haven't applied the code above correctly. Please note that it's not just a matter of the browser window having focus, you have to **click inside the window** (e.g., on the document) to give the **document** focus. – T.J. Crowder Apr 24 '19 at 09:28
  • @T.J.Crowder: Focus i mean, i have clicked in the middle of the window to have focus. – NDestiny Apr 24 '19 at 09:36
  • @NDestiny - I'm afraid I can't help any further. If you apply the code above correctly, and click in the window as you say you have, it definitely works in all environments I've tested it in, and I don't believe Windows 10 + IE11 would be different. It'll be because of some other factor. – T.J. Crowder Apr 24 '19 at 09:38
  • 1
    @T.J.Crowder Thanks for the help, I don't bother about chrome, but as you said i have tried it on Edge & Shift+F8 works great, but still no luck with IE11. Thanks for your help & i will need to look at whats is wrong with IE11. – NDestiny Apr 24 '19 at 09:43
  • 1
    I made a test with the sample code provided by T.J.Crowder in IE 11 and with IE 8 document mode. I find that his code is working fine on my side with Win 10 machine. See output here.. https://imgur.com/a/EtOb2ne I suggest you to make a test on other machine to check whether it works or not. – Deepak-MSFT Apr 25 '19 at 02:39
-1
document.onkeydown = function(evt){
    evt = evt || window.event;
    if(evt.shiftKey && evt.key == "F8"){

    }
};