SlideShare a Scribd company logo
libGDX: 
User 
Input 
Jussi 
Pohjolainen 
Tampere 
University 
of 
Applied 
Sciences
TOUCH 
+ 
KEYBOARD
Event 
vs 
Polling 
• Polling 
– Do 
something 
on 
each 
frame, 
really 
fast 
– Was 
mouse 
clicked? 
Was 
mouse 
clicked? 
Was 
mouse 
clicked? 
– Good 
for 
arcade 
games 
• Event 
– Do 
something 
when 
event 
handles 
– NoIfy 
when 
mouse 
was 
clicked 
– UI 
elements 
such 
as 
buJons 
• Mouse 
/ 
touch 
/ 
keyboard 
can 
be 
received 
either 
polling 
or 
event 
handling
public class MyInputProcessor implements InputProcessor { 
@Override 
public boolean keyDown (int keycode) { 
return false; 
} 
@Override 
public boolean keyUp (int keycode) { 
return false; 
} 
@Override 
public boolean keyTyped (char character) { 
return false; 
} 
@Override 
public boolean touchDown (int x, int y, int pointer, int button) { 
return false; 
} 
@Override 
public boolean touchUp (int x, int y, int pointer, int button) { 
return false; 
} 
@Override 
public boolean touchDragged (int x, int y, int pointer) { 
return false; 
} 
@Override 
public boolean mouseMoved (int x, int y) { 
return false; 
} 
@Override 
public boolean scrolled (int amount) { 
return false; 
} 
} 
MyInputProcessor inputProcessor = new MyInputProcessor(); 
Gdx.input.setInputProcessor(inputProcessor);
Polling 
Touch 
/ 
Keyboard 
• For 
most 
arcade 
games, 
polling 
is 
good 
• MulItouch 
is 
supported! 
– boolean firstFingerTouching = Gdx.input.isTouched(0); 
– boolean secondFingerTouching = Gdx.input.isTouched(1); 
– int firstX = Gdx.input.getX(); 
– int firstY = Gdx.input.getY(); 
– int secondX = Gdx.input.getX(1); 
– int secondY = Gdx.input.getY(1); 
• Keyboard 
– boolean isAPressed = Gdx.input.isKeyPressed(Keys.A);
ACCELEROMETER
Accelerometer 
• An 
accelerometer 
measures 
the 
acceleraIon 
of 
a 
device 
on 
three 
axes 
• From 
this 
acceleraIon 
one 
can 
derive 
the 
Ilt 
or 
orientaIon 
of 
the 
device. 
– Phones: 
portrait 
default 
– Tablet: 
landscape 
default 
• LibGDX 
shows 
accelerometer 
readings 
always 
as 
in 
the 
image
Accelerometer 
y 
(0,0) 
Accelerometer 
x 
y 
increments 
x 
increments
Accelerometer 
Readings 
• Accelerometer 
readings 
can 
be 
accessed 
– float accelX = Gdx.input.getAccelerometerX(); 
– float accelY = Gdx.input.getAccelerometerY(); 
– float accelZ = Gdx.input.getAccelerometerZ(); 
• When 
moving 
and 
if 
in 
landscape 
mode 
in 
android, 
noIce 
X 
vs 
Y! 
– speedX += Gdx.input.getAccelerometerY();
GESTURES
GestureDetector 
• TouchDown 
• LongPress 
• Tap 
• Pan 
– Useful 
for 
camera 
panning 
in 
2D 
• PanStop 
• Fling 
• Zoom 
– Camera 
zooming 
• Pinch 
– Camera 
zooming 
+ 
rotaIon
StarIng 
to 
Listen 
to 
Gestures 
• Really 
easy 
– Gdx.input.setInputProcessor(new 
GestureDetector(new MyGestureListener())); 
• MyGestureListener 
is 
some 
class 
that 
implements 
GestureListener
public class MyGestureListener implements GestureListener { 
@Override 
public boolean touchDown(float x, float y, int pointer, int button) {} 
@Override 
public boolean tap(float x, float y, int count, int button) {} 
@Override 
public boolean longPress(float x, float y) {} 
@Override 
public boolean fling(float velocityX, float velocityY, int button) {} 
@Override 
public boolean pan(float x, float y, float deltaX, float deltaY) {} 
@Override 
public boolean panStop(float x, float y, int pointer, int button) {} 
@Override 
public boolean zoom (float originalDistance, float currentDistance){} 
@Override 
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, 
Vector2 firstPointer, Vector2 secondPointer){} 
}
SIMPLE 
TEXT 
INPUT
User 
input 
• Desktop 
– Swing 
dialog 
• Android 
– Android 
dialog 
• Use 
TextInputListener 
– MyTextInputListener listener = new MyTextInputListener(); 
– Gdx.input.getTextInput(listener, "Dialog Title", "Initial 
Textfield Value")
public class MyTextInputListener implements TextInputListener { 
@Override 
public void input (String text) { 
} 
@Override 
public void canceled () { 
} 
}
MISC
• Vibra 
– Gdx.input.vibrate(2000); 
– Remember 
to 
add 
permissions 
• Compass 
– float azimuth = Gdx.input.getAzimuth(); 
– float pitch = Gdx.input.getPitch(); 
– float roll = Gdx.input.getRoll();

More Related Content

PDF
libGDX: User Input and Frame by Frame Animation
PDF
libGDX: Scene2D
PDF
Implementing a Simple Game using libGDX
PDF
A split screen-viable UI event system - Unite Copenhagen 2019
PDF
Box2D and libGDX
PDF
Creating Games for Asha - platform
PDF
libGDX: Tiled Maps
PPTX
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
libGDX: User Input and Frame by Frame Animation
libGDX: Scene2D
Implementing a Simple Game using libGDX
A split screen-viable UI event system - Unite Copenhagen 2019
Box2D and libGDX
Creating Games for Asha - platform
libGDX: Tiled Maps
Building a turn-based game prototype using ECS - Unite Copenhagen 2019

What's hot (20)

PPT
Input and Interaction
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
7java Events
PPTX
Cross-scene references: A shock to the system - Unite Copenhagen 2019
PDF
Game Development using SDL and the PDK
PDF
Unity遊戲程式設計 - 2D Platformer遊戲
PDF
Game Development with SDL and Perl
PDF
Technical Deep Dive into the New Prefab System
PDF
Cross-platform game engine development with SDL 2.0
ODP
SDL2 Game Development VT Code Camp 2013
PDF
Unity 13 space shooter game
PPTX
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
PDF
Unity遊戲程式設計(15) 實作Space shooter遊戲
PPTX
Soc research
PPTX
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
PDF
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
PPTX
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
PDF
HoloLens Programming Tutorial: AirTap & Spatial Mapping
PDF
Sequence diagrams
KEY
Introduction to Game Programming Tutorial
Input and Interaction
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
7java Events
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Game Development using SDL and the PDK
Unity遊戲程式設計 - 2D Platformer遊戲
Game Development with SDL and Perl
Technical Deep Dive into the New Prefab System
Cross-platform game engine development with SDL 2.0
SDL2 Game Development VT Code Camp 2013
Unity 13 space shooter game
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
Unity遊戲程式設計(15) 實作Space shooter遊戲
Soc research
Unity asset workflows for Film and Animation pipelines - Unite Copenhagen 2019
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
HoloLens Programming Tutorial: AirTap & Spatial Mapping
Sequence diagrams
Introduction to Game Programming Tutorial
Ad

Similar to libGDX: User Input (20)

PDF
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
PPTX
PPTX
Chapter 03 game input
DOC
Scientific calculator in c
PPTX
Mobile Application Development class 005
PPT
Working with Callbacks
PPTX
B_27_TicTacToe_ILV_TSG powerpoint presentation
PPTX
Unix signals
PDF
662305 LAB12
PDF
Multi Touch presentation
PPTX
Md2010 jl-wp7-sl-game-dev
PPTX
Module 3 Session2 of Computer Graphics.pptx
PDF
efy-articles
PDF
Tech day ngobrol santai tensorflow
PPT
grahics
PPTX
Game Development for Nokia Asha Devices with Java ME #2
PDF
Real life XNA
PDF
Of class2
DOC
BingoConsoleApp
PDF
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Chapter 03 game input
Scientific calculator in c
Mobile Application Development class 005
Working with Callbacks
B_27_TicTacToe_ILV_TSG powerpoint presentation
Unix signals
662305 LAB12
Multi Touch presentation
Md2010 jl-wp7-sl-game-dev
Module 3 Session2 of Computer Graphics.pptx
efy-articles
Tech day ngobrol santai tensorflow
grahics
Game Development for Nokia Asha Devices with Java ME #2
Real life XNA
Of class2
BingoConsoleApp
Ad

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
libGDX: Screens, Fonts and Preferences
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
PDF
Intro to PhoneGap
PDF
Quick Intro to JQuery and JQuery Mobile
PDF
JavaScript Inheritance
PDF
JS OO and Closures
PDF
Short intro to ECMAScript
PDF
Building Web Services
Moved to Speakerdeck
Java Web Services
libGDX: Screens, Fonts and Preferences
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Building Android games using LibGDX
Android Threading
Intro to Asha UI
Intro to Java ME and Asha Platform
Intro to PhoneGap
Quick Intro to JQuery and JQuery Mobile
JavaScript Inheritance
JS OO and Closures
Short intro to ECMAScript
Building Web Services

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

libGDX: User Input

  • 1. libGDX: User Input Jussi Pohjolainen Tampere University of Applied Sciences
  • 3. Event vs Polling • Polling – Do something on each frame, really fast – Was mouse clicked? Was mouse clicked? Was mouse clicked? – Good for arcade games • Event – Do something when event handles – NoIfy when mouse was clicked – UI elements such as buJons • Mouse / touch / keyboard can be received either polling or event handling
  • 4. public class MyInputProcessor implements InputProcessor { @Override public boolean keyDown (int keycode) { return false; } @Override public boolean keyUp (int keycode) { return false; } @Override public boolean keyTyped (char character) { return false; } @Override public boolean touchDown (int x, int y, int pointer, int button) { return false; } @Override public boolean touchUp (int x, int y, int pointer, int button) { return false; } @Override public boolean touchDragged (int x, int y, int pointer) { return false; } @Override public boolean mouseMoved (int x, int y) { return false; } @Override public boolean scrolled (int amount) { return false; } } MyInputProcessor inputProcessor = new MyInputProcessor(); Gdx.input.setInputProcessor(inputProcessor);
  • 5. Polling Touch / Keyboard • For most arcade games, polling is good • MulItouch is supported! – boolean firstFingerTouching = Gdx.input.isTouched(0); – boolean secondFingerTouching = Gdx.input.isTouched(1); – int firstX = Gdx.input.getX(); – int firstY = Gdx.input.getY(); – int secondX = Gdx.input.getX(1); – int secondY = Gdx.input.getY(1); • Keyboard – boolean isAPressed = Gdx.input.isKeyPressed(Keys.A);
  • 7. Accelerometer • An accelerometer measures the acceleraIon of a device on three axes • From this acceleraIon one can derive the Ilt or orientaIon of the device. – Phones: portrait default – Tablet: landscape default • LibGDX shows accelerometer readings always as in the image
  • 8. Accelerometer y (0,0) Accelerometer x y increments x increments
  • 9. Accelerometer Readings • Accelerometer readings can be accessed – float accelX = Gdx.input.getAccelerometerX(); – float accelY = Gdx.input.getAccelerometerY(); – float accelZ = Gdx.input.getAccelerometerZ(); • When moving and if in landscape mode in android, noIce X vs Y! – speedX += Gdx.input.getAccelerometerY();
  • 11. GestureDetector • TouchDown • LongPress • Tap • Pan – Useful for camera panning in 2D • PanStop • Fling • Zoom – Camera zooming • Pinch – Camera zooming + rotaIon
  • 12. StarIng to Listen to Gestures • Really easy – Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener())); • MyGestureListener is some class that implements GestureListener
  • 13. public class MyGestureListener implements GestureListener { @Override public boolean touchDown(float x, float y, int pointer, int button) {} @Override public boolean tap(float x, float y, int count, int button) {} @Override public boolean longPress(float x, float y) {} @Override public boolean fling(float velocityX, float velocityY, int button) {} @Override public boolean pan(float x, float y, float deltaX, float deltaY) {} @Override public boolean panStop(float x, float y, int pointer, int button) {} @Override public boolean zoom (float originalDistance, float currentDistance){} @Override public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){} }
  • 15. User input • Desktop – Swing dialog • Android – Android dialog • Use TextInputListener – MyTextInputListener listener = new MyTextInputListener(); – Gdx.input.getTextInput(listener, "Dialog Title", "Initial Textfield Value")
  • 16. public class MyTextInputListener implements TextInputListener { @Override public void input (String text) { } @Override public void canceled () { } }
  • 17. MISC
  • 18. • Vibra – Gdx.input.vibrate(2000); – Remember to add permissions • Compass – float azimuth = Gdx.input.getAzimuth(); – float pitch = Gdx.input.getPitch(); – float roll = Gdx.input.getRoll();