Capturing a Cmd-C keyboard event (or Ctrl-C) from a modular Flex application in a browser or AIR

It seems impossible to capture a keyboard event, usually used for copying when a Flex application is launched in a browser or as an AIR application, presumably because the browser or OS intercepts it in the first place.

Is there a way to tell the browser or OS to have the event pass?

For example, in AdvancedDataGrid, I set the keyUp event for 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);
        }

At startup, I can never get the release of the "C" key, and also by pressing the command key (which appears as KeyboardEvent.ctrlKey). I get the following trace results:

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

, , , , "C" .

- ?

"C" ( ) ?

?

+3
4

, , , ( Mac) control-c, control-v .. , , ( ) , , ctrlKey ( , ctrlKey Mac), charCode 0. , .

+2

, , , ctrl-c event.ctrlKey && event.keyCode = Keyboard.C (... event.charCode == 67), , charCode keyCode 3. charCode, ctrl-c 3 ASCII, keyCode, , . ( ctrl combo ASCII).

Flex : https://bugs.adobe.com/jira/browse/FP-375

0

:

    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;
        }
    }
0

, . , Cmd + A, :

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

Therefore, every time you get keyCode 15 down and then up and the next shot is reduced, you can assume that the user pressed a key combination. My implementation ends as follows:

    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!
            }
        }
    }
0
source

Source: https://habr.com/ru/post/1696563/


All Articles