question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GWT Export does not Prevent Default Key Presses

See original GitHub issue

When I compile my libGDX project to target html there are scripts to prevent default mouse operations, but not keyboard. This can be somewhat of a hassle for games where normally people use arrow keys.

I’ve been looking around and I tried multiple things (none of which worked) such as follows:

window.focus();

or

window.addEventListener("keydown", handleKeyDown(evt) {
        // Prevent space and arrow keys:
        if([32, 37, 38, 39, 40].indexOf(evt.keyCode) > -1) { evt.preventDefault(); }
});

or

handleKeyDown(evt) {
        evt.preventDefault();
}
document.getElementById('embed-html').addEventListener("keydown", handleKeyDown(evt), false);

etc.

But it doesn’t seem to change anything. Any suggestions on what I may be doing wrong?

If you need to see my code it’s here. Basically everything within that {% if page.type == "libgdx" %} statement is where my code is. And if you need the game page source-code is here, and the actual page is here.

Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
maximojrcommented, Nov 28, 2014

I had a similar problem, i needed that the TAB key change the focus among text fields, but the canvas lost the focus when TAB was pressed. To achieve the goal I put a javascritp at onkeydown of the body of the index.html:

<script type="text/javascript">
function avoidTabLoseFocus(event){
  if (event.keyCode == 9){
    event.preventDefault();
  }
}
</script>
<body onkeydown="avoidTabLoseFocus(event);">

And made my screen class implements InputProcessor, so i built the logic to control the focus change on methods keyUp e keyDown, both starting with:

if (ApplicationType.WebGL == Gdx.app.getType())

Just to be sure it won’t make side effects on other plataforms xD

It’s more a rusty workaround than a pretty solution, but I hope it helps.

0reactions
MDeimlcommented, Aug 24, 2015

Ok, found a solution myself: I had a typo in the code that went up fixing the problem(despite it throwing an error), but I don’t know why. So my full script looks like this:

   <script>
          function handleMouseDown(evt) {
            evt.preventDefault();
            evt.stopPropagation();
            evt.target.style.cursor = 'default';
          }

          function handleMouseUp(evt) {
            evt.preventDefault();
            evt.stopPropagation();
            evt.target.style.cursor = '';
          }

          function avoidLoseFocus(event){
          alert('a);
                if (event.keyCode == 65){
                    event.preventDefault();
                }
            }

          document.getElementById('embed-html').addEventListener('mousedown', handleMouseDown, false);
          document.getElementById('embed-html').addEventListener('mouseup', handleMouseUp, false);
          document.getElementsByTagName('body')[0].addEventListener('keydown', avoidLoseFocus, false);
   </script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

GWT event prevent default not working in Java - Stack Overflow
I tried a little experiment on a normal web page. It turns out, that the MOUSEDOWN event doesn't do anything in that context...
Read more >
[GWT] Documentation - Compile & Debug
When an application is running in development mode, the Java Virtual Machine (JVM) is actually executing the application code as compiled Java bytecode,...
Read more >
Smart GWT Legacy Release Notes - SmartClient
When a ListGrid shows a filterEditor, default behavior is now to discard edits when the user hides a field. This matches 9.0 behavior...
Read more >
GWT - Quick Guide - Tutorialspoint
If you do not have Java installed then you can install the Java Software ... For example, the Button widget has a default...
Read more >
Why doesn't GWT just export everything automattically
In the GWT 3.0 it'll you can use JsInterfaces etcetera which is much better than GWT exporter and built into the compiler. It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found