Hi there,
I have been playing around with the key class in flash as I want to use the keyboard to control a game. I noticed however that it seems to have problems with pressing of multiple keys. Using the code below do the following.
[LIST=1]
[]Press and hold the space bar(the onKeyPress event is continulously triggered)
[]Whilst that key is still held down press another key(triggers onKeyPress and onkeyUp)
[*]Now release the original key(Spacebar) and you will see that the onKeyUp event is not triggered.[/LIST]
Basically I want the user to be able to hold down a key to control movement whilst pressing other keys to shoot etc…
Any help would be appreciated
here is the code…
var globalPressListener=new Object();
Key.addListener(globalPressListener);
globalPressListener.onKeyDown=handleKeyPress;
var globalReleaseListener=new Object();
Key.addListener(globalReleaseListener);
globalReleaseListener.onKeyUp=handleKeyUp;
//=====================================================
//=====================================================
function handleKeyPress(){
var keyPressed:String=getKeyPressed();
trace('keyPressed = '+keyPressed);
}
//=====================================================
function handleKeyUp(){
var keyReleased:String=getKeyPressed();
trace('keyReleased = '+keyReleased);
}
//=====================================================
function getKeyPressed():String {
var theKey:String;
switch (Key.getCode()) {
case Key.BACKSPACE :
theKey = "BACKSPACE";
break;
case Key.SPACE :
theKey = "SPACE";
break;
case Key.LEFT :
theKey = "LEFT";
break;
case Key.RIGHT :
theKey = "RIGHT";
break;
case Key.UP :
theKey = "UP";
break;
case Key.DOWN :
theKey = "DOWN";
break;
default :
theKey = String.fromCharCode(Key.getAscii());
}
return theKey;
}