0

I need a piece of code that detects key presses then performs a function. The code I've got works, but after the function has been triggered, whichever key is pressed, the function will run again.

var key = [];
onkeydown = onkeyup = function(e){
    e = e || event;
    map[e.keyCode] = e.type == 'keydown';
    if(key[17] && key[16] && key[65]){ //CTRL+SHIFT+A
        alert('Enabled');
        enableControls();
        key[];
        return false;
    }
 }

I've set the array to nothing at the end with 'key[];', but this stops the whole function from even running when triggered in the first place. How can I run the function once when the keys are pressed but stop this bug that repeats the function regardless of which keys are pressed?

  • remove the alert and it will work, the alert is preventing the keyup to trigger – juvian Jul 06 '16 at 17:43
  • ^^ + `key[];` is a syntax error, should be `key = [];` See also, [how to detect ctrl and shift keys](http://stackoverflow.com/questions/18316509/detect-ctrl-and-shift-key-without-keydown-event/18317134#18317134). Though the question is about click, the answer stands for keydown/up as well. – Teemu Jul 06 '16 at 18:00

0 Answers0