1

I'd like to implement hotkeys with this behavior:

Hold Alt, press A, release A, press 1, release 1, release Alt

Then execute some logic bound to Alt-A1 hotkey.

I thought that I could track all the keys between keydown and keyup of Alt. But unfortunately, I can't catch keyup event for Alt in the end.

Strangely enough, when a press Alt only, I catch both keydown and keyup event. But when I press something else while holding Alt (even Alt-A, for intsance), it seems like some other logic takes place and suppresses keyup event for Alt.

Is it some IE specific issue (I tried with IE8 and IE11)? Because I can catch it in Chrome, for example. What approach would you recommend here?

UPD: Actually there's a bug closed as not reproducible by The Internet Explorer Team: https://connect.microsoft.com/IE/feedback/details/807606/ie-does-not-catch-keyup-for-alt-button-after-pressing-key-combination

var keys = [];
document.body.innerHTML = "Keys currently pressed: "
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = e.keyCode;
        var keysArray = getNumberArray(keys);
        document.body.innerHTML = "Keys currently pressed:" + keysArray;
        if(keysArray.toString() == "18,65"){
            document.body.innerHTML += " Did it!"
        }
    },
false);

window.addEventListener('keyup',
    function(e){
        keys[e.keyCode] = false;
        document.body.innerHTML = "Keys currently pressed: " + getNumberArray(keys);
    },
false);

function getNumberArray(arr){
    var newArr = new Array();
    for(var i = 0; i < arr.length; i++){
        if(typeof arr[i] == "number"){
            newArr[newArr.length] = arr[i];
        }
    }
    return newArr;
}
  • 1
    Put the code in the question, not a jsfiddle. Stackoverflow has snipplets – epascarello Feb 28 '17 at 14:06
  • 2
    Do you realize the event object tells you if a alt, cmd, ctrl key is pressed? – epascarello Feb 28 '17 at 14:12
  • Yep, I realize it. I tried to implement it by checking all the keys after getting keydown for Alt for event.altKey == true condition. But then I realized that I need to wait for another key that would be event.altKey == false to start executing the logic. Not the best solution, I think. – Aleksei Loginov Feb 28 '17 at 14:16
  • Just found a bug closed as not reproducible by The Internet Explorer Team: https://connect.microsoft.com/IE/feedback/details/807606/ie-does-not-catch-keyup-for-alt-button-after-pressing-key-combination – Aleksei Loginov Feb 28 '17 at 15:06
  • 1
    The Alt/Altgr keys are used by IE to navigate the menu and accesskey attribute values on web pages. With the IE menu visible press either key... you will see focus go to the IE menu.... IE and webkit fire keydown, keypress, keyup in order. Place validation in the keypress handler. – Rob Parsons Feb 28 '17 at 21:17
  • @RobParsons Good advice. But for some reason I can catch only keydown and keyup events, but not keypress. If I change listener in the snippet from "keydown" to "keypress", I have troubles catching keypress event. – Aleksei Loginov Mar 01 '17 at 08:13
  • the window object is at the top of event bubbles.... the common design pattern is to assign keyboard event handlers to an input element or the body element (document). – Rob Parsons Mar 01 '17 at 19:48
  • 1
    please see a previous question http://stackoverflow.com/questions/2103385/javascript-event-sequence which refers to a great article http://unixpapa.com/js/key.html. The alt key does not fire the keypress event. Honestly you are best off not using scripted keyboard shortcuts for Alt key combinations and function keys. – Rob Parsons Mar 01 '17 at 20:00

0 Answers0