Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Detecting if ctrl is pressed when clicking a button?

Guest
Oct 18, 2013 Oct 18, 2013

Hi

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

Cheers

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Oct 18, 2013 Oct 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

Translate
LEGEND ,
Oct 18, 2013 Oct 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;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 18, 2013 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 18, 2013 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 18, 2013 Oct 18, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines