How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?

前端 未结 8 1865
忘了有多久
忘了有多久 2020-11-27 04:03

Here is my code:

function pauseSound() {
    var pauseSound = document.getElementById(\"backgroundMusic\");
    pauseSound.pause(); 
}

I wo

相关标签:
8条回答
  • 2020-11-27 04:45

    This worked for me

    document.onkeyup=function(e){
      var e = e || window.event;
      if(e.which == 37) {
        $("#prev").click()
      }else if(e.which == 39){
        $("#next").click()
      }
    }
    
    0 讨论(0)
  • 2020-11-27 04:46
    //For single key: Short cut key for 'Z'
    document.onkeypress = function (e) {
        var evt = window.event || e;
        switch (evt.keyCode) {
            case 90:  
                // Call your method Here
                break;
        }
    }
    
    //For combine keys like Alt+P
    document.onkeyup = function (e) {
        var evt = window.event || e;   
            if (evt.keyCode == 80 && evt.altKey) {
                // Call Your method here   
            }
        }
    }
        //ensure if short cut keys are case sensitive.
        //    If its not case sensitive then
        //check with the evt.keyCode values for both upper case and lower case. ......
    
    0 讨论(0)
  • 2020-11-27 04:48

    Solution:

    var activeKeys = [];
    
    //determine operating system
    var os = false;
    window.addEventListener('load', function() {
      var userAgent = navigator.appVersion;
      if (userAgent.indexOf("Win") != -1) os = "windows";
      if (userAgent.indexOf("Mac") != -1) os = "osx";
      if (userAgent.indexOf("X11") != -1) os = "unix";
      if (userAgent.indexOf("Linux") != -1) os = "linux";
    });
    
    window.addEventListener('keydown', function(e) {
      if (activeKeys.indexOf(e.which) == -1) {
        activeKeys.push(e.which);
      }
    
      if (os == 'osx') {
    
      } else {
        //use indexOf function to check for keys being pressed IE
        if (activeKeys.indexOf(17) != -1 && activeKeys.indexOf(86) != -1) {
          console.log('you are trying to paste with control+v keys');
        }
        /*
          the control and v keys (for paste)
          if(activeKeys.indexOf(17) != -1 && activeKeys.indexOf(86) != -1){
            command and v keys are being pressed
          }
        */
      }
    });
    
    window.addEventListener('keyup', function(e) {
      var result = activeKeys.indexOf(e.which);
      if (result != -1) {
        activeKeys.splice(result, 1);
      }
    });

    Explanation: I ran into this same problem and came up with my own solution. e.metaKey didn't seem to work with the keyup event in Chrome and Safari. However, I'm not sure if it was specific to my application since I had other algorithms blocking some key events and I may have mistakenly blocked the meta key.

    This algorithm monitors for keys going down and then adds them to a list of keys that are currently being pressed. When released, the key is removed from the list. Check for simultaneous keys in the list by using indexOf to find key codes in the array.

    0 讨论(0)
  • 2020-11-27 04:50

    These appear to all be using the deprecated keyCode and which properties. Here is a non-deprecated version using jQuery to wire up the event:

    $("body").on("keyup", function (e) {
        if(e.ctrlKey && e.key == 'x')
            pauseSound();
        else if(e.ctrlKey && e.key == 't')
            playSound();
    })
    

    Note: ctrl + t may already be assigned to opening a new browser tab

    0 讨论(0)
  • 2020-11-27 04:52

    an event handler for the document's keyup event seems like an appropriate solution

    // define a handler
    function doc_keyUp(e) {
    
        // this would test for whichever key is 40 and the ctrl key at the same time
        if (e.ctrlKey && e.keyCode == 40) {
            // call your function to do the thing
            pauseSound();
        }
    }
    // register the handler 
    document.addEventListener('keyup', doc_keyUp, false);
    
    0 讨论(0)
  • 2020-11-27 04:55

    Catch the key code and then call your function. This example catches the ESC key and calls your function:

    function getKey(key) {
        if ( key == null ) {
            keycode = event.keyCode;
        // To Mozilla
        } else {
            keycode = key.keyCode;
        }
        // Return the key in lower case form    
        if (keycode ==27){
            //alert(keycode);
            pauseSound();
            return false;
        }
        //return String.fromCharCode(keycode).toLowerCase();
    }
    $(document).ready( function (){
        $(document).keydown(function (eventObj){
            //alert("Keydown: The key is: "+getKey(eventObj));
            getKey(eventObj);
        });
    });
    

    You'll need JQUERY for this example.

    0 讨论(0)
提交回复
热议问题