0
# Input Handling
1
2
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.
3
4
## Core Input Interfaces
5
6
### GwtInput { .api }
7
8
```java
9
public interface GwtInput extends Input {
10
// Inherits all standard Input interface methods
11
// Plus web-specific extensions
12
}
13
```
14
15
### DefaultGwtInput { .api }
16
17
```java
18
public class DefaultGwtInput extends AbstractInput implements GwtInput {
19
// Keyboard input
20
public boolean isKeyPressed(int key);
21
public boolean isKeyJustPressed(int key);
22
23
// Mouse input
24
public int getX();
25
public int getY();
26
public int getX(int pointer);
27
public int getY(int pointer);
28
public boolean isTouched();
29
public boolean isTouched(int pointer);
30
public boolean justTouched();
31
public boolean isButtonPressed(int button);
32
public boolean isButtonJustPressed(int button);
33
34
// Mouse wheel and movement
35
public int getDeltaX();
36
public int getDeltaY();
37
public int getDeltaX(int pointer);
38
public int getDeltaY(int pointer);
39
40
// Touch and multi-touch
41
public float getPressure();
42
public float getPressure(int pointer);
43
44
// Text input
45
public void getTextInput(TextInputListener listener, String title, String text, String hint);
46
public void getTextInput(TextInputListener listener, String title, String text, String hint, OnscreenKeyboardType type);
47
48
// Virtual keyboard
49
public void setOnscreenKeyboardVisible(boolean visible);
50
public void setOnscreenKeyboardVisible(boolean visible, OnscreenKeyboardType type);
51
52
// Input processors
53
public void setInputProcessor(InputProcessor processor);
54
public InputProcessor getInputProcessor();
55
56
// Accelerometer and sensors
57
public float getAccelerometerX();
58
public float getAccelerometerY();
59
public float getAccelerometerZ();
60
public boolean isPeripheralAvailable(Peripheral peripheral);
61
62
// Gyroscope
63
public float getGyroscopeX();
64
public float getGyroscopeY();
65
public float getGyroscopeZ();
66
67
// Compass
68
public float getAzimuth();
69
public float getPitch();
70
public float getRoll();
71
72
// Rotation vector
73
public void getRotationMatrix(float[] matrix);
74
75
// Haptic feedback (not supported on web)
76
public void vibrate(int milliseconds);
77
public void vibrate(long[] pattern, int repeat);
78
public void cancelVibrate();
79
80
// Mouse cursor control
81
public void setCursorCatched(boolean catched);
82
public boolean isCursorCatched();
83
public void setCursorPosition(int x, int y);
84
85
// Input multiplexing
86
public long getCurrentEventTime();
87
}
88
```
89
90
## Sensor Support Classes
91
92
### GwtSensor { .api }
93
94
```java
95
public class GwtSensor extends JavaScriptObject {
96
// Base class for browser-based sensors
97
protected GwtSensor();
98
}
99
```
100
101
### GwtAccelerometer { .api }
102
103
```java
104
public class GwtAccelerometer extends GwtSensor {
105
// Device accelerometer access through browser APIs
106
public static native boolean isSupported();
107
public static native GwtAccelerometer getInstance();
108
109
public final native float getX();
110
public final native float getY();
111
public final native float getZ();
112
}
113
```
114
115
### GwtGyroscope { .api }
116
117
```java
118
public class GwtGyroscope extends GwtSensor {
119
// Device gyroscope access through browser APIs
120
public static native boolean isSupported();
121
public static native GwtGyroscope getInstance();
122
123
public final native float getAlpha(); // Z-axis rotation
124
public final native float getBeta(); // X-axis rotation
125
public final native float getGamma(); // Y-axis rotation
126
}
127
```
128
129
## Text Input Support
130
131
### Text Input Dialog
132
133
The GWT backend provides web-compatible text input through browser dialogs:
134
135
```java
136
// Basic text input
137
Gdx.input.getTextInput(new TextInputListener() {
138
@Override
139
public void input(String text) {
140
// Handle user input
141
System.out.println("User entered: " + text);
142
}
143
144
@Override
145
public void canceled() {
146
// Handle input cancellation
147
System.out.println("Input canceled");
148
}
149
}, "Enter Name", "", "Your name here");
150
151
// Numeric input
152
Gdx.input.getTextInput(new TextInputListener() {
153
@Override
154
public void input(String text) {
155
try {
156
int score = Integer.parseInt(text);
157
// Handle numeric input
158
} catch (NumberFormatException e) {
159
// Handle invalid input
160
}
161
}
162
163
@Override
164
public void canceled() {
165
// Handle cancellation
166
}
167
}, "High Score", "", "Enter score", OnscreenKeyboardType.NumberPad);
168
```
169
170
## Usage Examples
171
172
### Basic Input Handling
173
174
```java
175
public class InputGame implements ApplicationListener {
176
private InputProcessor inputProcessor;
177
178
@Override
179
public void create() {
180
inputProcessor = new InputAdapter() {
181
@Override
182
public boolean keyDown(int keycode) {
183
switch (keycode) {
184
case Input.Keys.SPACE:
185
// Handle spacebar press
186
return true;
187
case Input.Keys.ESCAPE:
188
// Handle escape key
189
return true;
190
}
191
return false;
192
}
193
194
@Override
195
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
196
// Handle mouse/touch press
197
System.out.println("Touch at: " + screenX + ", " + screenY);
198
return true;
199
}
200
201
@Override
202
public boolean scrolled(float amountX, float amountY) {
203
// Handle mouse wheel scroll
204
System.out.println("Scrolled: " + amountX + ", " + amountY);
205
return true;
206
}
207
};
208
209
Gdx.input.setInputProcessor(inputProcessor);
210
}
211
212
@Override
213
public void render() {
214
// Poll-based input checking
215
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
216
// Move left
217
}
218
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
219
// Move right
220
}
221
222
if (Gdx.input.isTouched()) {
223
int x = Gdx.input.getX();
224
int y = Gdx.input.getY();
225
// Handle continuous touch/drag
226
}
227
}
228
}
229
```
230
231
### Multi-Touch Support
232
233
```java
234
public class MultiTouchGame implements ApplicationListener {
235
private static final int MAX_TOUCHES = 10;
236
237
@Override
238
public void render() {
239
// Check multiple touch points
240
for (int i = 0; i < MAX_TOUCHES; i++) {
241
if (Gdx.input.isTouched(i)) {
242
int x = Gdx.input.getX(i);
243
int y = Gdx.input.getY(i);
244
float pressure = Gdx.input.getPressure(i);
245
246
// Handle individual touch point
247
handleTouch(i, x, y, pressure);
248
}
249
}
250
}
251
252
private void handleTouch(int pointer, int x, int y, float pressure) {
253
// Handle specific touch point
254
}
255
}
256
```
257
258
### Mobile Sensor Input
259
260
```java
261
public class SensorGame implements ApplicationListener {
262
private boolean accelerometerAvailable;
263
private boolean gyroscopeAvailable;
264
265
@Override
266
public void create() {
267
// Check sensor availability
268
accelerometerAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
269
gyroscopeAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Gyroscope);
270
271
System.out.println("Accelerometer: " + accelerometerAvailable);
272
System.out.println("Gyroscope: " + gyroscopeAvailable);
273
}
274
275
@Override
276
public void render() {
277
if (accelerometerAvailable) {
278
float accelX = Gdx.input.getAccelerometerX();
279
float accelY = Gdx.input.getAccelerometerY();
280
float accelZ = Gdx.input.getAccelerometerZ();
281
282
// Use accelerometer for tilt controls
283
handleTilt(accelX, accelY, accelZ);
284
}
285
286
if (gyroscopeAvailable) {
287
float gyroX = Gdx.input.getGyroscopeX();
288
float gyroY = Gdx.input.getGyroscopeY();
289
float gyroZ = Gdx.input.getGyroscopeZ();
290
291
// Use gyroscope for rotation controls
292
handleRotation(gyroX, gyroY, gyroZ);
293
}
294
}
295
296
private void handleTilt(float x, float y, float z) {
297
// Implement tilt-based controls
298
}
299
300
private void handleRotation(float x, float y, float z) {
301
// Implement rotation-based controls
302
}
303
}
304
```
305
306
### Keyboard Input with Key Codes
307
308
```java
309
public class KeyboardGame implements ApplicationListener {
310
@Override
311
public void create() {
312
Gdx.input.setInputProcessor(new InputAdapter() {
313
@Override
314
public boolean keyDown(int keycode) {
315
switch (keycode) {
316
case Input.Keys.W:
317
case Input.Keys.UP:
318
// Move up
319
return true;
320
case Input.Keys.S:
321
case Input.Keys.DOWN:
322
// Move down
323
return true;
324
case Input.Keys.A:
325
case Input.Keys.LEFT:
326
// Move left
327
return true;
328
case Input.Keys.D:
329
case Input.Keys.RIGHT:
330
// Move right
331
return true;
332
case Input.Keys.ENTER:
333
// Confirm action
334
return true;
335
case Input.Keys.ESCAPE:
336
// Cancel/menu
337
return true;
338
}
339
return false;
340
}
341
});
342
}
343
}
344
```
345
346
### Mouse Button Handling
347
348
```java
349
public class MouseGame implements ApplicationListener {
350
@Override
351
public void create() {
352
Gdx.input.setInputProcessor(new InputAdapter() {
353
@Override
354
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
355
switch (button) {
356
case Input.Buttons.LEFT:
357
// Primary action (left click or touch)
358
handlePrimaryAction(screenX, screenY);
359
return true;
360
case Input.Buttons.RIGHT:
361
// Secondary action (right click)
362
handleSecondaryAction(screenX, screenY);
363
return true;
364
case Input.Buttons.MIDDLE:
365
// Middle mouse button
366
handleMiddleAction(screenX, screenY);
367
return true;
368
}
369
return false;
370
}
371
372
@Override
373
public boolean mouseMoved(int screenX, int screenY) {
374
// Handle mouse movement (no buttons pressed)
375
return true;
376
}
377
378
@Override
379
public boolean touchDragged(int screenX, int screenY, int pointer) {
380
// Handle drag operations
381
return true;
382
}
383
});
384
}
385
386
private void handlePrimaryAction(int x, int y) {
387
// Left click or touch handling
388
}
389
390
private void handleSecondaryAction(int x, int y) {
391
// Right click handling (context menu, etc.)
392
}
393
394
private void handleMiddleAction(int x, int y) {
395
// Middle mouse button handling
396
}
397
}
398
```
399
400
## Web-Specific Input Considerations
401
402
### Browser Input Limitations
403
404
```java
405
// Some input features have web-specific behavior:
406
407
// Cursor catching (pointer lock) requires user permission
408
public void enablePointerLock() {
409
if (!Gdx.input.isCursorCatched()) {
410
// Must be called in response to user interaction
411
Gdx.input.setCursorCatched(true);
412
}
413
}
414
415
// Vibration is not supported in browsers
416
public void tryVibrate() {
417
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Vibrator)) {
418
Gdx.input.vibrate(100); // Will not work on web
419
}
420
}
421
422
// Some key events may be captured by browser
423
// (F11 for fullscreen, Ctrl+R for refresh, etc.)
424
```
425
426
### Input Event Handling Order
427
428
```java
429
// Input events in GWT backend follow this order:
430
// 1. InputProcessor methods (if set)
431
// 2. Input polling methods (isKeyPressed, isTouched, etc.)
432
433
public class InputOrderExample implements ApplicationListener {
434
@Override
435
public void create() {
436
// Event-based input (immediate response)
437
Gdx.input.setInputProcessor(new InputAdapter() {
438
@Override
439
public boolean keyDown(int keycode) {
440
// Handles key press immediately
441
return true; // Return true to consume event
442
}
443
});
444
}
445
446
@Override
447
public void render() {
448
// Poll-based input (checked each frame)
449
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
450
// Checked continuously while key is held
451
}
452
}
453
}
454
```