Skip to main content
October 18, 2013
Answered

Detecting if ctrl is pressed when clicking a button?

  • October 18, 2013
  • 2 replies
  • 1561 views

Hi

What's the best way to detect if ctrl is being held down while a button on screen is being clicked?

Cheers

This topic has been closed for replies.
Correct answer Aaron Beall

MouseEvent has several handy properties to tell you if a modifier key is currently held down:

function clickHandler(e:MouseEvent){

     trace(e.ctrlKey); // Also works with Command on Mac

     trace(e.controlKey); // Windows and Mac "control" key has different meaning

     trace(e.commandKey); // Mac only

     trace(e.shiftKey);

     trace(e.altKey);

}

More here:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html

Cheers.

-Aaron

2 replies

Aaron BeallCorrect answer
Inspiring
October 18, 2013

MouseEvent has several handy properties to tell you if a modifier key is currently held down:

function clickHandler(e:MouseEvent){

     trace(e.ctrlKey); // Also works with Command on Mac

     trace(e.controlKey); // Windows and Mac "control" key has different meaning

     trace(e.commandKey); // Mac only

     trace(e.shiftKey);

     trace(e.altKey);

}

More here:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html

Cheers.

-Aaron

Ned Murphy
Legend
October 18, 2013

That is a learned-something-new-today answer Aaron...   thanks.  I was wondering how to get the mouse handler to determine the key status so I figured I need to remember that separately.

October 18, 2013

Thank you both. Those mouse events eg e.ctrlKey are bloody brilliant!!!

Ned Murphy
Legend
October 18, 2013

One way would be to use a variable that reflects the status of the control key and use that in conjunction with whatever button event handler coding you have.  Here is how you can manage that control key indication...

stage.addEventListener(KeyboardEvent.KEY_DOWN, detectKey);
stage.addEventListener(KeyboardEvent.KEY_UP, detectKey);

var ctrlDown:Boolean = false;

function detectKey(event:KeyboardEvent):void { 
     ctrlDown = event.ctrlKey;
}