<p>It seems that it is impossible to capture the keyboard event normally used for copy when running a Flex application in the browser or as an AIR app, presumably because the browser or OS is intercepting it first.</p>

<p>Is there a way to tell the browser or OS to let the event through?</p>

<p>For example, on an AdvancedDataGrid I have set the keyUp event to handleCaseListKeyUp(event), which calls the following function:</p>

<pre><code>        private function handleCaseListKeyUp(event:KeyboardEvent):void
        {
            var char:String = String.fromCharCode(event.charCode).toUpperCase();

            if (event.ctrlKey &amp;&amp; char == "C")
            {
                trace("Ctrl-C");
                copyCasesToClipboard();
                return;
            }

            if (!event.ctrlKey &amp;&amp; char == "C")
            {
                trace("C");
                copyCasesToClipboard();
                return;
            }

            // Didn't match event to capture, just drop out.
            trace("charCode: " + event.charCode);
            trace("char: " + char);
            trace("keyCode: " + event.keyCode);
            trace("ctrlKey: " + event.ctrlKey);
            trace("altKey: " + event.altKey);
            trace("shiftKey: " + event.shiftKey);
        }
</code></pre>

<p>When run, I can never get the release of the "C" key while also pressing the command key (which shows up as KeyboardEvent.ctrlKey). I get the following trace results:</p>

<pre><code>charCode: 0
char: 
keyCode: 17
ctrlKey: false
altKey: false
shiftKey: false
</code></pre>

<p>As you can see, the only event I can capture is the release of the command key, the release of the "C" key while holding the command key isn't even sent.</p>

<p>Has anyone successfully implemented standard copy and paste keyboard handling?</p>

<p>Am I destined to just use the "C" key on it's own (as shown in the code example) or make a copy button available?</p>

<p>Or do I need to create the listener manually at a higher level and pass the event down into my modular application's guts?</p>

[{'answer_id': 32086, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 3, 'selected': True, 'text': "<p>I did a test where I listened for key up events on the stage and noticed that (on my Mac) I could capture control-c, control-v, etc. just fine, but anything involving command (the \uf8ff key) wasn't captured until I released the command key, and then ctrlKey was false (even though the docs says that ctrlKey should be true for the command key on the Mac), and the charCode was 0. Pretty useless, in short.</p>\n"}, {'answer_id': 48427, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 0, 'selected': False, 'text': '<p>Another incredibly annoying thing that I just realized is that ctrl-c can\'t be captured by <code>event.ctrlKey &amp;&amp; event.keyCode = Keyboard.C</code> (or ...<code>event.charCode == 67</code>), instead you have to test for <code>charCode</code> or <code>keyCode</code>\xa0being <code>3</code>. It kind of makes sense for <code>charCode</code> since ctrl-c is <code>3</code> in the ASCII table, but it doesn\'t make sense for <code>keyCode</code>, which is supposed to represent the key on the keyboard, not the typed character. The same goes for all other key combos (because every ctrl combo has an ASCII equivalent).</p>\n\n<p><em>Edit</em> Found a bug in the Flex bug system about this: <a href="https://bugs.adobe.com/jira/browse/FP-375" rel="nofollow noreferrer">https://bugs.adobe.com/jira/browse/FP-375</a></p>\n'}, {'answer_id': 426982, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>For me, the following works:</p>\n\n<pre><code>    private var _ctrlHoldFlag:Boolean = false; \n\n    // Do something if CTRL was held down and C was pressed\n    // Otherwise release the ctrl flag if it was pressed\n    public function onKey_Up(event:KeyboardEvent):void {  \n        var keycode_c:uint = 67;\n\n        if (_ctrlHoldFlag &amp;&amp; event.keyCode == keycode_c)\n        {\n            //do whatever you need on CTRL-C\n        }\n\n        if (event.ctrlKey)\n        {\n            _ctrlHoldFlag = false;\n        }\n    }\n\n    // Track ctrl key down press \n    public function onKey_Down(event:KeyboardEvent):void\n    {\n        if (event.ctrlKey)\n        {\n            _ctrlHoldFlag = true;\n        }\n    }\n</code></pre>\n'}, {'answer_id': 5759982, 'author': 'Pedro Andrade', 'author_id': 715451, 'author_profile': 'https://Stackoverflow.com/users/715451', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve found one workaround to this one based on the capture sequence. When you press Cmd+A, for example, the sequence is:</p>\n\n<ul>\n<li>type: KEY_DOWN, keyCode 15</li>\n<li>type: KEY_UP, keyCode 15</li>\n<li>type: KEY_DOWN, keyCode 65</li>\n</ul>\n\n<p>So everytime you get keyCode 15 down and then up and the next capture is down you can assume that the user pressed the key combination. My implementation end up like this:</p>\n\n<pre><code>    protected var lastKeys:Array;\n    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler, false, 0, true);\n    this.stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler, false, 0, true);\n\n    private function getCmdKey(ev:KeyboardEvent):Boolean {\n        this.lastKeys.push(ev);\n        this.lastKeys = this.lastKeys.splice(Math.max(0, this.lastKeys.length-3), 3);\n\n        if (this.lastKeys.length &lt; 3) return false;\n\n        if (ev.keyCode != 15 &amp;&amp; ev.type == KeyboardEvent.KEY_UP) {\n            var firstKey:KeyboardEvent = this.lastKeys[0] as KeyboardEvent;\n            var secondKey:KeyboardEvent = this.lastKeys[1] as KeyboardEvent;\n\n            if (firstKey.keyCode == 15 &amp;&amp; firstKey.type == KeyboardEvent.KEY_DOWN &amp;&amp;\n                secondKey.keyCode == 15 &amp;&amp; secondKey.type == KeyboardEvent.KEY_UP) {\n                    return true;\n            }\n        }\n\n        return false;\n    }\n\n    private function keyHandler(ev:KeyboardEvent):void {\n        var cmdKey:Boolean = this.getCmdKey(ev.clone() as KeyboardEvent);\n        var ctrlKey:Boolean = ev.ctrlKey || cmdKey;\n\n        if (ctrlKey) {\n            if (ev.keyCode == 65) { \n                // ctrl + "a"-- select all!\n            }\n        }\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31849', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3023/']