0

I am using some keyboard shortcuts for Next/Back navigation (arrows ← →), and another for Play/Pause audio (\ key). However, the first is not working with IE, and the second not working in both Firefox nor Internet Explorer. I am not sure what is the global function to work in all browsers, or what part in the script to edit for that.

js 1 (not working with Internet Explorer):

jQuery(function( $ ) {
var keymap = {};

keymap[ 37 ] = "#prev";
keymap[ 39 ] = "#next";

$( document ).on( "keyup", function(event) {
var href,
    selector = keymap[ event.which ];
if ( selector ) {
    href = $( selector ).attr( "href" );
    if ( href ) {
        window.location = href;
    }
}
});
});

js 2 (not working with Firefox nor IE):

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

                var video = document.getElementById("myAudio");
                // \
                if ((event.which == 92) || (event.keyCode==92)) {
                    if (video.paused)
                        video.play();
                    else
                        video.pause();
                }
            });
Mike
  • 2,051
  • 4
  • 28
  • 46
  • 1
    According to MDN, `event.which` is deprecated. You should use `event.key` instead. https://developer.mozilla.org/en-US/docs/Web/Events/keypress – nemanja Dec 12 '18 at 01:51
  • I changed it now, but it it doesn't fix the issue yet.. – Mike Dec 12 '18 at 01:54
  • Found this answer. Arrows work with keydown.I've tested and it works. https://stackoverflow.com/a/5597114/8815185 – nemanja Dec 12 '18 at 02:04
  • It works yes, but still not with IE – Mike Dec 12 '18 at 02:08
  • That same question, another answer. `e = e || window.event;` https://stackoverflow.com/a/9310900/8815185. And then another answer later also mentions the use of `key` instead of keyCode`, which is what MDN is suggesting as newer best practice (if I understood correctly). If this works, I can write an official answer. – nemanja Dec 12 '18 at 02:13
  • I changed the word "keyCode" with "key", but still it doesn't work i the browsers mentioned. If there are more changes other than that please copy the script and do the necessary changes so I can replace the current js code with it and check. My problem is that the above script works well on Chrome, and the first with FF not IE, while the second doesn't work in either of them (only with Chrome). – Mike Dec 12 '18 at 02:19
  • Well, if you change it to `key`, then you need to use `ArrowLeft` instead of `37`, `ArrowRight` instead of `39` and `\ ` instead of `92`. Keycodes can be different and I'm guessing that's why they suggest using the actual key. – nemanja Dec 12 '18 at 02:22

0 Answers0