Cannot Ctrl+V in TextField with OpenFL Legacy Cpp Build

154 Views Asked by At

I am currently working on an application using the HaxeUI library. In my application, I am creating TextInput objects, which are based off of OpenFL's TextField. Unfortunately, when compiling for Windows or Neko, these fields do not allow for basic faculties like Ctrl+V, Ctrl+C, or Ctrl+A.

As a result, I felt that I could just make my own extension of the TextInput class which simply uses the KeyboardEvent.KEY_DOWN event to detect these particular functions. The below is a relevant snippet of my implementation:

class SmartTextInput extends TextInput {
    public function new() {
        super();
        this.addEventListener(KeyboardEvent.KEY_DOWN, performPress);
    }

    private function performPress(e:KeyboardEvent):Void {
        if(e.ctrlKey) {
            trace("CTRL PRESSED!");
            switch(e.keyCode) {
                case Keyboard.V: trace("PASTE!");
            }
        }
    }
}

It looks like if I press Ctrl and then V, it should print out "CTRL PRESSED!" and "PASTE!". However, I only ever get "CTRL PRESSED!", so it doesn't work. In fact, after some vigorous testing, I found that if the Ctrl button is being held, then KeyboardEvent.KEY_DOWN will not register any other keypress except the Alt and Shift keys. In other words, detecting Ctrl and V being held simultaneously is impossible unless V is pressed first; however, since conventionally Ctrl is pressed first, this doesn't work for me.

Is there a way I can register actions like Ctrl+V in a TextField in OpenFL for Windows? Or at least, is there a way I can detect the sequential key presses of Ctrl, followed by V? I've tried having Ctrl on KEY_DOWN and V on KEY_UP, but it is not responsive enough for practical use.

I am using OpenFL 3.6.0, Lime 2.9.0, and HaxeUI 1.8.17. It should be noted that HaxeUI requires OpenFL Legacy. In non-legacy OpenFL, I was able to get Ctrl+V working just fine.

2

There are 2 best solutions below

0
On BEST ANSWER

This is why they made OpenFL next. I'm not sure if it's not possible or not easy to detect those keypresses in legacy, but I highly doubt that functionality would be added at this point since you got it to work with next.

So it's kind of a "Will OpenFL add to their legacy codebase?" versus "When does the next major version of HaxeUI come out?" There are enough issues with TextFields on next that they're probably not going to also fix legacy's longstanding problems. And a new HaxeUI is coming eventually.

So... no you can't. Not yet.

1
On

After some thinking about it, I came to realize the lack of responsiveness when using KEY_UP as a workaround was the result of releasing CTRL before V. In normal applications, this is possible because the paste action happens upon key down.

To get around this, I simply applied a delay on the release of CTRL:

class SmartTextInput extends TextInput {
    public function new() {
        super();
        ctrlDown = false;
        this.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        this.addEventListener(KeyboardEvent.KEY_UP, keyUp);
    }

    private static inline var CTRL_SENSITIVITY = 100;
    private var ctrlDown:Bool;

    private function keyDown(e:KeyboardEvent):Void {
        if (e.keyCode == Keyboard.CONTROL || e.keyCode == Keyboard.COMMAND)
            ctrlDown = true;
    }

    private function keyUp(e:KeyboardEvent):Void {
        if (e.keyCode == Keyboard.CONTROL || e.keyCode == Keyboard.COMMAND)
        //  The delay here is for people who release control before releasing the letter key
            Timer.delay(function() {  ctrlDown = false; }, CTRL_SENSITIVITY);
        if (ctrlDown) {
            switch(e.keyCode) {
                case Keyboard.C: copy();
                case Keyboard.V: paste();
            }
        }
    }
}

Though not optimal, this is seems to work well enough.