Copy link to clipboard
Copied
Hi
What's the best way to detect if ctrl is being held down while a button on screen is being clicked?
Cheers
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
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you both. Those mouse events eg e.ctrlKey are bloody brilliant!!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now