Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a piece of code that detects when certain keys are pressed then triggers a function, but the function repeats after triggered, with any key pressed. I've tried to reset the array at the end of the function, but this stops all keys from triggering the function in the first place.

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;
    }
 }


How can I stop the function repeating with any keys pressed after the function has run? Any help is greatly appreciated!

What I have tried:

I've tried to reset the array at the end of the function with "key[];", but this stops all keys from triggering the function in the first place.
Posted
Updated 6-Jul-16 7:54am

1 solution

Make 2 changes:
1. Change
map[e.keyCode]

to
key[e.keyCode]

2. Change
key[];

to
key = [];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900