openfl / lime

A foundational Haxe framework for cross-platform development
https://lime.openfl.org/
MIT License
753 stars 365 forks source link

[HTML5] KEY_UP event fails to fire when Command is held #1705

Open Geokureli opened 1 year ago

Geokureli commented 1 year ago

example:

var field = new TextField();
field.text = "Nothing happening";
field.textColor = 0xFFffffff;
field.x = 100;
field.y = 100;
addChild(field);

addEventListener(Event.ADDED_TO_STAGE,
    function addedToStage(?_)
    {
        stage.addEventListener(KeyboardEvent.KEY_UP,
            function (e)
            {
                if (e.keyCode == FlxKey.C)
                    field.text = "C released";
            }
        );

        stage.addEventListener(KeyboardEvent.KEY_DOWN,
            function (e)
            {
                if (e.keyCode == FlxKey.C)
                    field.text = e.ctrlKey ? "Cmd + C pressed" : "C pressed";
            }
        );
    }
);

when I press Cmd+C the test says "Cmd + C pressed", but on release the text does not change until I press and release C again. If I press Cmd+C and release Cmd before C it shows "C released", as expected

Note: Even though I'm using FlxKey.C, I haven't instantiated a FlxGame, or called any flixel utilities that would eat up events with preventDefault or something

joshtynjala commented 1 year ago

Since Cmd+C is the keyboard shortcut for copy, I wonder if the browser simply isn't dispatching an event to JS when that command is triggered by the user.

Does it make a difference if you call e.preventDefault() in the KEY_DOWN listener? That may stop the browser from copying, which could result in KEY_UP getting dispatched.

Geokureli commented 1 year ago

Does it make a difference if you call e.preventDefault() in the KEY_DOWN listener? That may stop the browser from copying, which could result in KEY_UP getting dispatched.

the up event is still not* firing after trying:

stage.addEventListener(KeyboardEvent.KEY_DOWN,
    function (e)
    {
        if (e.keyCode == C)
        {
            if (e.ctrlKey)
            {
                field.text = "Cmd + C pressed";
                e.preventDefault();
            }
            else
            {
                field.text = "C pressed";
            }
        }
    }
);
player-03 commented 1 year ago

Does this happen with keys other than C (or V)?

Geokureli commented 1 year ago

this seems to happen with all keys, but i can now confirm that that e.preventDefault does prevent the normal action of the key combo, except cmd + w and cmd + q (close the window). notably, cmd + h normally hides the active window, and that is prevented. I assume hide, just like W or Q are system commands and not browser commands