floooh / oryol

A small, portable and extensible C++ 3D coding framework
MIT License
2k stars 200 forks source link

Emscripten - Input default events aren't ignored in Chrome (but are ignored correctly in Firefox) #312

Closed Ohmnivore closed 6 years ago

Ohmnivore commented 6 years ago

I'm using Windows 10. The issue can be reproduced using this sample: http://floooh.github.io/oryol/asmjs/TestInput.html

In Firefox if I press Ctrl and s nothing special happens.

In Chrome this will pop open a "Save As" dialog, which is unwanted behavior from my perspective.

My control scheme uses the Ctrl key so this is an inconvenience.

Ohmnivore commented 6 years ago

Another example, the up and down arrow keys will scroll the page in Chrome but not in Firefox.

https://fouramgames.com/demo/battle/BattleApp.html

Ohmnivore commented 6 years ago

Adding the following snippet to the page fixed the ctrl+s and the arrow keys issues for me:

document.addEventListener('keydown', (event) => {
    event.preventDefault();
});

However, ctrl-w actually closes the tab in Chrome and Firefox irrespective of preventDefault() and they have no intent of changing that. It's best to just not use ctrl then.

floooh commented 6 years ago

Btw, I'm having a similar problem in Chrome on Windows in my Tiny Emulators: https://floooh.github.io/tiny8bit/, where Chrome intercepts the F1 key and shows the Help. I'll have a look if this fix of yours also fixes the problem I have there.

Thanks!

Ohmnivore commented 6 years ago

Np!

Btw I can't reproduce the F1 Help issue you mention on https://floooh.github.io/tiny8bit/atom.html and https://floooh.github.io/tiny8bit/c64.html for example. The function keys are well contained inside the tab, no funky behavior.

I've tried on Windows 10 on versions 68.0.3440.106 (Official Build) (64-bit) and 69.0.3497.92 (Official Build) (64-bit) (latest as of today).

floooh commented 6 years ago

I might just have had this fixed when you tested ;) At least I found out now what the problem is, it's about whether the key events are marked as 'consumed' by the event handler or not. If an event is consumed, the browser will ignore the event. Unfortunately I cannot simply mark all key events as 'consumed', since then I wouldn't get 'cooked' character key events (basically all the alpha-numeric stuff).

So what I'm doing now in sokol_app.h is a big switch case, I need to add the same to Oryol (in fact, I'll do this now) :)

https://github.com/floooh/sokol/blob/ae4d3a36d22690e7e1e369b31a94b1ceab245794/sokol_app.h#L1857-L1920

floooh commented 6 years ago

Ok, it's in this commit: 064560193adb58b117328a78f224557a48af285b

This fixes problems like the arrows keys scrolling the page (but I'm not sure about the Ctrl key, you'd need to check this).

Your global preventDefault() fix is still necessary if you don't use the Input module (as then Oryol completely ignores any browser input events).