Detect multiple presses of the same key
I'm trying to detect how many times a key is pressed. I'm trying to set up an old phone type of keyboard, so if I press 2 once I get "A", if I press 2 twice I get "B" and if I press 2 a third time I get "C".
This is what I have so far, but it's not working properly. The console shows no errors, but if I press 2 twice it alerts A twice instead of B, if i press 2 three times it alerts A three times instead of C.
- var abcKey = 0;
$(document).on('keyup', function( e ){
if(e.which==50){ // Keyboard number 2
if(abcKey===2){
setTimeout(function(){
alert("C");
abcKey = 0;
}, 1000);
}
else if(abcKey===1){
setTimeout(function(){
alert("B");
abcKey = 0;
}, 1000);
}
else if(abcKey===0){
setTimeout(function(){
alert("A");
abcKey = 0;
}, 1000);
}
}
});
Hopefully it's something easy to fix, I've working on it for quite a while and can't figure out the problem.