Is there a way to handle events with two simultaneous keys (Ctrl + other key)?

Hi I got this code

stage.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyDownHandler (e:KeyboardEvent):void {

trace (
"Key code: " + e.keyCode + "
" +
"Ctrl status: " + e.ctrlKey + "
" +
"Key location: " + e.keyLocation + "
" +
"Shift key: " + e.shiftKey + "
");
}

function keyUpHandler (e:KeyboardEvent):void {

trace ( "Key code: " + e.keyCode);
}

and it works but now I need to use two simultaneous keys for example [COLOR=“Blue”]Ctrl + S[/COLOR], is there a way to do that using actionscript 3?

Mine is probably not the most elegant approach, but I would do this by setting a boolean to true when CTRL is pressed, and then to false when it’s released. Then set up a function to execute when S is pressed, but that function will only execute if the CTRL boolean == true.

I did that but it doesn’t seems to work, look.

var ControlIsPressed:Boolean;

stage.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyDownHandler (e:KeyboardEvent):void {

if (e.ctrlKey){
	ControlIsPressed = true;
}

if (ControlIsPressed){
	if (e.keyCode == 83) {
		trace("lalala");
	}
}

}

function keyUpHandler (e:KeyboardEvent):void {
if (e.ctrlKey){
ControlIsPressed = false;
}

trace(“keyUpHandler
” +
"Key code: " + e.keyCode + “
”) ;
}

I’m by no means an expert on the topic, but can you really detect the ctrl key?


stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyUpHandler (e:KeyboardEvent):void {
	trace(e);
}

If I press ctrl, nothing traces. All other keys report an event.

You actually can trace the control key (it works for me with that exact same code) - but more importantly, the KeyboardEvent class includes a ctrlKey boolean.

Try this:


stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyUpHandler (e:KeyboardEvent):void 
{
    if (e.ctrlKey && e.keyCode == 83)
	{
		trace("Ctrl + S pushed.");
	}
	else
	{
		trace(e.keyCode)
	}
}

Not tracing anything on OSX : )

Hah. You know my feelings on OS X and Flash. :fight:

Maybe I should move the event listener one pixel to the left… :huh:

try converting it to an int :stuck_out_tongue_winking_eye: