R – Flex AIR using Command and Ctrl keyboard events

airapache-flexeventskeyboard shortcuts

I am writing an AIR application in Flex. The application needs to be able to handle shortcuts on both Mac and Windows. I have the shortcuts on the Windows side working, but the same application on a Mac doesnt seem to trigger the commandKey property on a Keyboard event when the user is also using another key in combination (ex. command+g doesnt work). What do I need to do to allow me to capture a shortcut on a Mac?

Best Answer

In this basic AIR example I caught both CMD and CTRL keys:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:keyDown>
    <![CDATA[
        trace('CharCode: ' + event.charCode + ' Cmd: ' + event.commandKey + ' Ctrl: ' + event.ctrlKey + ' KeyCode: ' + event.keyCode);
    ]]>
    </mx:keyDown>
    <mx:TextArea />
</mx:WindowedApplication>

I know I had really a lot problem with keyboard events in Flex and either AIR apps because of focus. In the example above you will only see proper results if you click into the Textarea. Maybe this is how it should work by default but I still find keyboard event handling pain in Flex.