emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Issues with javascript handling glut events

Open AlexPerrot opened this issue 12 years ago • 7 comments

I'm having issues with the way javascript events are processed to be passed to the glut callbacks defined in the C++ code.

You can see in library_glut.js that the javascript listeners are attached to the window :

    window.addEventListener("keydown", GLUT.onKeydown, true);
    window.addEventListener("keyup", GLUT.onKeyup, true);
    if (isTouchDevice) {
      window.addEventListener("touchmove", GLUT.onMousemove, true);
      window.addEventListener("touchstart", GLUT.onMouseButtonDown, true);
      window.addEventListener("touchend", GLUT.onMouseButtonUp, true);
    } else {
      window.addEventListener("mousemove", GLUT.onMousemove, true);
      window.addEventListener("mousedown", GLUT.onMouseButtonDown, true);
      window.addEventListener("mouseup", GLUT.onMouseButtonUp, true);
      // IE9, Chrome, Safari, Opera
      window.addEventListener("mousewheel", GLUT.onMouseWheel, true);
      // Firefox
      window.addEventListener("DOMMouseScroll", GLUT.onMouseWheel, true);
    }

This causes several issues : First, the mousewheel listeners prevent the page from scrolling, even when the mouse is not directly over the canvas. Another issue arises when the canvas is not dispayed (i'm using jquery mobile's page system) : the mousewheel events are fired with coordinates inside the canvas (in my case resulting in a zoomin/zoomout while the canvas is not displayed). These two issues are easily fixed by attaching the mousewheel listeners directly to the canvas.

The biggest issue however is about the keyboard events. They are always processed and transformed to glut events, even when the focus is in a text input somewhere else on the page. This is very annoying, since it prevents the user from typing anything into the input field.

To conclude, it seems reasonable to me that the javascript events should only be passed to glut if they target the canvas, not other elements in the page.

AlexPerrot avatar Jan 08 '14 14:01 AlexPerrot

In SDL we listen to mouse events only on the canvas, which makes sense, I agree we should change that in glut.

However, I am not sure I agree about keyboard events, it's been useful to not force people to click on the canvas to play keyboard-only games, for example.

We could do this as an option, though, how about that?

kripken avatar Jan 10 '14 20:01 kripken

I'm OK with doing an option for that.

However, I'm wondering if there's a more elegant way to do this. Is it possible to listen to keyboard events on the window and only calling the glut callbacks (and preventDefault()) if the event meets some criteria (I was thinking about a target check, but maybe you propose something else ?) ?

AlexPerrot avatar Jan 13 '14 08:01 AlexPerrot

Hmm, not sure what sort of criteria you mean? If we check the target, wouldn't that be the same as putting the event handler on the target?

kripken avatar Feb 11 '14 18:02 kripken

If the handler is attached to the canvas, you need to give it a tab index and focus it in order to fire keyboard events. On the other hand, if the handler is attached to the window, the event wil always fire, but you could let it through if the target is a text field, for exemple (not sure the canvas can be a target if it has no tab index). In other words, there would be a black list of target types for which glut events are not fired. This should maintain the current behaviour, while enabling keyboard interaction with other elements in the page. The tricky part is to define which html elements should be in this list.

Anyway, is it possible to replace emscripten event handlers with some --post-js code ?

AlexPerrot avatar Feb 12 '14 12:02 AlexPerrot

Event handlers are set up in the sdl/glut/glew etc. code, so those would need to be modified. Or made so they check parameters in how they work. Perhaps we should indeed make it optional as to what we listen for events on. The defaults in SDL seem reasonable, but I guess they won't be optimal for everything.

kripken avatar Feb 12 '14 19:02 kripken

Closing this, since this is super old. I'm not using glut anymore, I handle the context creation and event listeners myself, it is much more flexible and simple.

AlexPerrot avatar Nov 07 '16 13:11 AlexPerrot

Had a comment, but 'Module.doNotCaptureKeyboard = true' worked for my case (before I had been using it wrong). Now I don't know how to delete my comment

dwn avatar Jan 27 '21 06:01 dwn