GWT backend for libGDX enabling Java game development for web browsers through JavaScript compilation
—
The GWT backend provides comprehensive input handling for mouse, keyboard, touch, and mobile device sensors through DOM event integration. It supports both desktop and mobile input patterns with web-specific optimizations.
public interface GwtInput extends Input {
// Inherits all standard Input interface methods
// Plus web-specific extensions
}public class DefaultGwtInput extends AbstractInput implements GwtInput {
// Keyboard input
public boolean isKeyPressed(int key);
public boolean isKeyJustPressed(int key);
// Mouse input
public int getX();
public int getY();
public int getX(int pointer);
public int getY(int pointer);
public boolean isTouched();
public boolean isTouched(int pointer);
public boolean justTouched();
public boolean isButtonPressed(int button);
public boolean isButtonJustPressed(int button);
// Mouse wheel and movement
public int getDeltaX();
public int getDeltaY();
public int getDeltaX(int pointer);
public int getDeltaY(int pointer);
// Touch and multi-touch
public float getPressure();
public float getPressure(int pointer);
// Text input
public void getTextInput(TextInputListener listener, String title, String text, String hint);
public void getTextInput(TextInputListener listener, String title, String text, String hint, OnscreenKeyboardType type);
// Virtual keyboard
public void setOnscreenKeyboardVisible(boolean visible);
public void setOnscreenKeyboardVisible(boolean visible, OnscreenKeyboardType type);
// Input processors
public void setInputProcessor(InputProcessor processor);
public InputProcessor getInputProcessor();
// Accelerometer and sensors
public float getAccelerometerX();
public float getAccelerometerY();
public float getAccelerometerZ();
public boolean isPeripheralAvailable(Peripheral peripheral);
// Gyroscope
public float getGyroscopeX();
public float getGyroscopeY();
public float getGyroscopeZ();
// Compass
public float getAzimuth();
public float getPitch();
public float getRoll();
// Rotation vector
public void getRotationMatrix(float[] matrix);
// Haptic feedback (not supported on web)
public void vibrate(int milliseconds);
public void vibrate(long[] pattern, int repeat);
public void cancelVibrate();
// Mouse cursor control
public void setCursorCatched(boolean catched);
public boolean isCursorCatched();
public void setCursorPosition(int x, int y);
// Input multiplexing
public long getCurrentEventTime();
}public class GwtSensor extends JavaScriptObject {
// Base class for browser-based sensors
protected GwtSensor();
}public class GwtAccelerometer extends GwtSensor {
// Device accelerometer access through browser APIs
public static native boolean isSupported();
public static native GwtAccelerometer getInstance();
public final native float getX();
public final native float getY();
public final native float getZ();
}public class GwtGyroscope extends GwtSensor {
// Device gyroscope access through browser APIs
public static native boolean isSupported();
public static native GwtGyroscope getInstance();
public final native float getAlpha(); // Z-axis rotation
public final native float getBeta(); // X-axis rotation
public final native float getGamma(); // Y-axis rotation
}The GWT backend provides web-compatible text input through browser dialogs:
// Basic text input
Gdx.input.getTextInput(new TextInputListener() {
@Override
public void input(String text) {
// Handle user input
System.out.println("User entered: " + text);
}
@Override
public void canceled() {
// Handle input cancellation
System.out.println("Input canceled");
}
}, "Enter Name", "", "Your name here");
// Numeric input
Gdx.input.getTextInput(new TextInputListener() {
@Override
public void input(String text) {
try {
int score = Integer.parseInt(text);
// Handle numeric input
} catch (NumberFormatException e) {
// Handle invalid input
}
}
@Override
public void canceled() {
// Handle cancellation
}
}, "High Score", "", "Enter score", OnscreenKeyboardType.NumberPad);public class InputGame implements ApplicationListener {
private InputProcessor inputProcessor;
@Override
public void create() {
inputProcessor = new InputAdapter() {
@Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.SPACE:
// Handle spacebar press
return true;
case Input.Keys.ESCAPE:
// Handle escape key
return true;
}
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// Handle mouse/touch press
System.out.println("Touch at: " + screenX + ", " + screenY);
return true;
}
@Override
public boolean scrolled(float amountX, float amountY) {
// Handle mouse wheel scroll
System.out.println("Scrolled: " + amountX + ", " + amountY);
return true;
}
};
Gdx.input.setInputProcessor(inputProcessor);
}
@Override
public void render() {
// Poll-based input checking
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
// Move left
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
// Move right
}
if (Gdx.input.isTouched()) {
int x = Gdx.input.getX();
int y = Gdx.input.getY();
// Handle continuous touch/drag
}
}
}public class MultiTouchGame implements ApplicationListener {
private static final int MAX_TOUCHES = 10;
@Override
public void render() {
// Check multiple touch points
for (int i = 0; i < MAX_TOUCHES; i++) {
if (Gdx.input.isTouched(i)) {
int x = Gdx.input.getX(i);
int y = Gdx.input.getY(i);
float pressure = Gdx.input.getPressure(i);
// Handle individual touch point
handleTouch(i, x, y, pressure);
}
}
}
private void handleTouch(int pointer, int x, int y, float pressure) {
// Handle specific touch point
}
}public class SensorGame implements ApplicationListener {
private boolean accelerometerAvailable;
private boolean gyroscopeAvailable;
@Override
public void create() {
// Check sensor availability
accelerometerAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
gyroscopeAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Gyroscope);
System.out.println("Accelerometer: " + accelerometerAvailable);
System.out.println("Gyroscope: " + gyroscopeAvailable);
}
@Override
public void render() {
if (accelerometerAvailable) {
float accelX = Gdx.input.getAccelerometerX();
float accelY = Gdx.input.getAccelerometerY();
float accelZ = Gdx.input.getAccelerometerZ();
// Use accelerometer for tilt controls
handleTilt(accelX, accelY, accelZ);
}
if (gyroscopeAvailable) {
float gyroX = Gdx.input.getGyroscopeX();
float gyroY = Gdx.input.getGyroscopeY();
float gyroZ = Gdx.input.getGyroscopeZ();
// Use gyroscope for rotation controls
handleRotation(gyroX, gyroY, gyroZ);
}
}
private void handleTilt(float x, float y, float z) {
// Implement tilt-based controls
}
private void handleRotation(float x, float y, float z) {
// Implement rotation-based controls
}
}public class KeyboardGame implements ApplicationListener {
@Override
public void create() {
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.W:
case Input.Keys.UP:
// Move up
return true;
case Input.Keys.S:
case Input.Keys.DOWN:
// Move down
return true;
case Input.Keys.A:
case Input.Keys.LEFT:
// Move left
return true;
case Input.Keys.D:
case Input.Keys.RIGHT:
// Move right
return true;
case Input.Keys.ENTER:
// Confirm action
return true;
case Input.Keys.ESCAPE:
// Cancel/menu
return true;
}
return false;
}
});
}
}public class MouseGame implements ApplicationListener {
@Override
public void create() {
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
switch (button) {
case Input.Buttons.LEFT:
// Primary action (left click or touch)
handlePrimaryAction(screenX, screenY);
return true;
case Input.Buttons.RIGHT:
// Secondary action (right click)
handleSecondaryAction(screenX, screenY);
return true;
case Input.Buttons.MIDDLE:
// Middle mouse button
handleMiddleAction(screenX, screenY);
return true;
}
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// Handle mouse movement (no buttons pressed)
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// Handle drag operations
return true;
}
});
}
private void handlePrimaryAction(int x, int y) {
// Left click or touch handling
}
private void handleSecondaryAction(int x, int y) {
// Right click handling (context menu, etc.)
}
private void handleMiddleAction(int x, int y) {
// Middle mouse button handling
}
}// Some input features have web-specific behavior:
// Cursor catching (pointer lock) requires user permission
public void enablePointerLock() {
if (!Gdx.input.isCursorCatched()) {
// Must be called in response to user interaction
Gdx.input.setCursorCatched(true);
}
}
// Vibration is not supported in browsers
public void tryVibrate() {
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Vibrator)) {
Gdx.input.vibrate(100); // Will not work on web
}
}
// Some key events may be captured by browser
// (F11 for fullscreen, Ctrl+R for refresh, etc.)// Input events in GWT backend follow this order:
// 1. InputProcessor methods (if set)
// 2. Input polling methods (isKeyPressed, isTouched, etc.)
public class InputOrderExample implements ApplicationListener {
@Override
public void create() {
// Event-based input (immediate response)
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyDown(int keycode) {
// Handles key press immediately
return true; // Return true to consume event
}
});
}
@Override
public void render() {
// Poll-based input (checked each frame)
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
// Checked continuously while key is held
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-badlogicgames-gdx--gdx-backend-gwt