flixel icon indicating copy to clipboard operation
flixel copied to clipboard

HaxeFlixel not detecting arrow key presses

Open Joncom opened this issue 6 years ago • 5 comments

  • Haxe version: 3.4.7
  • Flixel version: 4.5.1
  • OpenFL version: 8.6.2
  • Lime version: 7.1.1
  • Affected targets: HTML5

Code snippet reproducing the issue:

package;

import openfl.display.Sprite;
import flixel.FlxG;
import flixel.FlxGame;
import flixel.FlxState;
import flash.events.KeyboardEvent;

enum Mode {
	Broken;  // Most keys detected, but NOT arrow keys
	Working; // All keys detected, including arrow keys
}

class Main extends Sprite
{
	public static var mode : Mode = Mode.Broken;

	public function new()
	{
		super();

		if(mode == Mode.Working) {
			addListener();
		} else {
			addChild(new FlxGame(0, 0, PlayState));
		}
	}

	public static function addListener()
	{
		FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, Main.keydown);
	}

	public static function keydown(event:KeyboardEvent)
	{
		if([37, 38, 39, 40].indexOf(event.keyCode) >= 0) {
			trace("Arrow key pressed");
		} else {
			trace("Other key pressed");
		}
	}
}

class PlayState extends FlxState
{
	override public function create():Void
	{
		super.create();
		Main.addListener();
	}
}

Observed behavior: Key presses like A, B, C, Shift, etc. will trigger a message to console, but arrow keys will not. No KeyboardEvent fires for arrow keys.

Expected behavior:

Arrow key presses should trigger a console message, just like the other keys do.

Joncom avatar Nov 23 '18 09:11 Joncom

This seems to be related to the arrow keys being in FlxG.keys.preventDefaultKeys on HTML5. This is to prevent scrolling the page when those keys are pressed. I guess it also blocks other event listeners from getting the event. If the list is cleared (FlxG.keys.preventDefaultKeys = [];), it works.

Any particular reason you're using an event listener instead of checking FlxG.keys?

Gama11 avatar Nov 23 '18 10:11 Gama11

Any particular reason you're using an event listener instead of checking FlxG.keys?

Yes, because I'm porting another game engine to Haxe, and just using HaxeFlixel as a framework to patch-in things like audio, rendering, keyboard input, etc. In the future I'll probably strip out HaxeFlixel altogether, leaving just OpenFL in its place. However for now, HaxeFlixel seemed like one of the easiest to get up and running quickly.

Joncom avatar Nov 23 '18 16:11 Joncom

This also breaks arrow keys in the debug console, so this is a straight up flixel issue on HTML5. Arrow keys are need, for example, to navigate the autocomplete menu.

	function handlePreventDefaultKeys(keyCode:Int, event:KeyboardEvent):Void
	{
		var key:FlxInput<Key> = getKey(keyCode);
		if (key != null && preventDefaultKeys != null && preventDefaultKeys.indexOf(key.ID) != -1)
		{
			event.stopImmediatePropagation();
			event.stopPropagation();
			#if (html5 || android)
			event.preventDefault();
			#end
		}
	}

This seems to be doing a lot... I'm not sure why the stop(Immediate)Propagation would be useful, but no doubt that's what's causing it. They were originally in a condition (https://github.com/HaxeFlixel/flixel/blame/e0db087dda134035ab8e581c82fba2f79a88dd5b/flixel/input/FlxKeyManager.hx#L283)

JoeCreates avatar Jul 23 '19 11:07 JoeCreates

I'm not sure why the stop(Immediate)Propagation would be useful

Somebody would have to check whether or not that's necessary on Android or HTML5. I suspect it may have been needed on at least one of those with OpenFL legacy (which we technically still support...).

Gama11 avatar Jul 23 '19 11:07 Gama11

Why would they even be necessary on any platform? By this point they are just events in openfl. Essentially it's just saying nothing else in openfl can use this event now, isn't it?

If there really is something which needs to be disabled to prevent some default openfl behavior then I would expect a better way of disabling them specfically for that reason without consuming the event entirely.

On Tue, 23 Jul 2019, 12:54 Jens Fischer, [email protected] wrote:

I'm not sure why the stop(Immediate)Propagation would be useful

Somebody would have to check whether or not that's necessary on Android or HTML5. I suspect it may have been needed on at least one of those with OpenFL legacy (which we technically still support...).

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/HaxeFlixel/flixel/issues/2190?email_source=notifications&email_token=AATMJPA5XM3ZATMSCV2KEVDQA3WP7A5CNFSM4GGBGL5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2S3YZI#issuecomment-514178149, or mute the thread https://github.com/notifications/unsubscribe-auth/AATMJPAD5TJ37Y3Q35B2I4LQA3WP7ANCNFSM4GGBGL5A .

JoeCreates avatar Jul 23 '19 13:07 JoeCreates