Capturing Cmd-C (or Ctrl-C) keyboard event from mo

2019-03-22 02:30发布

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.

Is there a way to tell the browser or OS to let the event through?

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

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

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

            if (!event.ctrlKey && 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);
        }

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:

charCode: 0
char: 
keyCode: 17
ctrlKey: false
altKey: false
shiftKey: false

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.

Has anyone successfully implemented standard copy and paste keyboard handling?

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?

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

标签: flex air
4条回答
Emotional °昔
2楼-- · 2019-03-22 02:39

Another incredibly annoying thing that I just realized is that ctrl-c can't be captured by event.ctrlKey && event.keyCode = Keyboard.C (or ...event.charCode == 67), instead you have to test for charCode or keyCode being 3. It kind of makes sense for charCode since ctrl-c is 3 in the ASCII table, but it doesn't make sense for keyCode, 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).

Edit Found a bug in the Flex bug system about this: https://bugs.adobe.com/jira/browse/FP-375

查看更多
Explosion°爆炸
3楼-- · 2019-03-22 02:40

For me, the following works:

    private var _ctrlHoldFlag:Boolean = false; 

    // Do something if CTRL was held down and C was pressed
    // Otherwise release the ctrl flag if it was pressed
    public function onKey_Up(event:KeyboardEvent):void {  
        var keycode_c:uint = 67;

        if (_ctrlHoldFlag && event.keyCode == keycode_c)
        {
            //do whatever you need on CTRL-C
        }

        if (event.ctrlKey)
        {
            _ctrlHoldFlag = false;
        }
    }

    // Track ctrl key down press 
    public function onKey_Down(event:KeyboardEvent):void
    {
        if (event.ctrlKey)
        {
            _ctrlHoldFlag = true;
        }
    }
查看更多
Explosion°爆炸
4楼-- · 2019-03-22 02:59

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  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.

查看更多
小情绪 Triste *
5楼-- · 2019-03-22 02:59

I've found one workaround to this one based on the capture sequence. When you press Cmd+A, for example, the sequence is:

  • type: KEY_DOWN, keyCode 15
  • type: KEY_UP, keyCode 15
  • type: KEY_DOWN, keyCode 65

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:

    protected var lastKeys:Array;
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler, false, 0, true);
    this.stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler, false, 0, true);

    private function getCmdKey(ev:KeyboardEvent):Boolean {
        this.lastKeys.push(ev);
        this.lastKeys = this.lastKeys.splice(Math.max(0, this.lastKeys.length-3), 3);

        if (this.lastKeys.length < 3) return false;

        if (ev.keyCode != 15 && ev.type == KeyboardEvent.KEY_UP) {
            var firstKey:KeyboardEvent = this.lastKeys[0] as KeyboardEvent;
            var secondKey:KeyboardEvent = this.lastKeys[1] as KeyboardEvent;

            if (firstKey.keyCode == 15 && firstKey.type == KeyboardEvent.KEY_DOWN &&
                secondKey.keyCode == 15 && secondKey.type == KeyboardEvent.KEY_UP) {
                    return true;
            }
        }

        return false;
    }

    private function keyHandler(ev:KeyboardEvent):void {
        var cmdKey:Boolean = this.getCmdKey(ev.clone() as KeyboardEvent);
        var ctrlKey:Boolean = ev.ctrlKey || cmdKey;

        if (ctrlKey) {
            if (ev.keyCode == 65) { 
                // ctrl + "a"-- select all!
            }
        }
    }
查看更多
登录 后发表回答