-
Notifications
You must be signed in to change notification settings - Fork 0
Polling
Polling refers to checking the current state of an input device, e.g. is a specific key pressed, where is the first finger on the screen and so on. It's a quick and easy way to process user input and will suffice for most arcade games.
Caution: If you rely on polling, you might miss events, e.g. a fast paced key down/key up. If you need to make sure a specific sequence of input action was completed, use event handling instead.
Polling for input from a Keyboard is done with just one simple line of code, like below.
boolean isAPressed = Gdx.input.isKeyPressed(Keys.A);
The parameter passed to that method is a Key Code. Rather than having to memorize these codes there is a static class within the Input
interface that contains the codes which you can use. They can be seen here.
There are a number of methods concerning polling the touch screen/mouse. To check whether one or more fingers are currently on the screen (which is equivalent to a mouse button being pressed) you can do the following:
boolean isTouched = Gdx.input.isTouched();
For multi-touch input you might be interested whether a specific finger (pointer) is currently on the screen:
// Will Return whether the screen is currently touched
boolean firstFingerTouching = Gdx.input.isTouched(0);
boolean secondFingerTouching = Gdx.input.isTouched(1);
boolean thirdFingerTouching = Gdx.input.isTouched(2);
Each finger that goes down on the screen gets a so called pointer index. The first finger to go down gets the index 0, the next one gets the index 1 and so on. If a finger is lifted off the screen and touched down again, while other fingers are still on the screen, the finger will get the first free index. An example:
- first finger goes down -> 0
- second finger goes down -> 1
- third finger goes down -> 2
- second finger goes up -> 1 becomes free
- first finger goes up -> 0 becomes free, at this point only 2 is used
- another finger goes down -> 0, as it is the first free index
On the desktop or the browser you will only ever have a single "finger" so to speak.
If you want to check if the user touched down and released any finger again you can use the following method:
// Will return whether the screen has just been touched
boolean justTouched = Gdx.input.justTouched();
This can be used in situations where you want to check a touch down/up sequence really quickly, e.g. on a screen that says "touch screen to continue". Note that it is not a reliable method as it is based on polling.
To get the coordinates of a specific finger you can use the following methods:
int firstX = Gdx.input.getX();
int firstY = Gdx.input.getY();
int secondX = Gdx.input.getX(1);
int secondY = Gdx.input.getY(1);
Here we get the touch coordinates at pointer index 0 (0 is default) and pointer index 1. Coordinates are reported in a coordinate system relative to the screen. The origin (0, 0) is in the upper left corner of the screen, the x-axis points to the right, the y-axis points downwards.
On the desktop you can also check which mouse buttons are currently pressed:
boolean leftPressed = Gdx.input.isButtonPressed(Input.Buttons.LEFT);
boolean rightPressed = Gdx.input.isButtonPressed(Input.Buttons.RIGHT);
See the Buttons class for more constants.
Note that on Android we only emulate the left mouse button. Any touch event will be interpreted as if it was issued with a left mouse button press. Touch screens obviously don't have a notion of left, right and middle button.
-
Developer's Guide
- Introduction
- Goals & Features
- Community & Support
- Contributing
- Games Built with Libgdx
- Prerequisites
- Project Setup, Running & Debugging
- Third Party Services
- Working from Source
- Using libgdx with other JVM languages
- The Application Framework
- A Simple Game
- File Handling
- Preferences
- Input Handling
- Memory Management
- Audio
-
Graphics
- Configuration & Querying Graphics ??
- Fullscreen & VSync
- Continuous & Non-Continuous Rendering
- Clearing the Screen
- OpenGL ES Support * Configuration & Querying OpenGL ?? * Direct Access ?? * Utility Classes * Rendering Shapes * Textures & TextureRegions * Meshes * Shaders * Frame Buffer Objects
- 2D Graphics * SpriteBatch, TextureRegions, and Sprite * Clipping, with the use of ScissorStack * Orthographic camera * Mapping Touch Coordinates ?? * NinePatches * Bitmap Fonts * Distance field fonts * Using TextureAtlases * Pixmaps * Packing Atlases Offline * Packing Atlases at Runtime * 2D Particle Effects * Tile Maps * scene2d * scene2d.ui * Skin
- 3D Graphics ?? * Quick Start * 3D animations and skinning * Importing Blender models in LibGDX * Perspective Camera ?? * Picking ??
- Managing Your Assets
- Utilities
-
Math Utilities
- Interpolation
- Vectors, Matrices, Quaternions
- Circles, Planes, Rays, etc.
- Bounding Volumes ??
- Intersection & Overlap Testing ??
- Physics
- Tools
- Extensions
- gdx-audio ??
- gdx-freetype
- Deploying your Application
- Building Libgdx ??
- Articles
- Deprecated (May be outdated)
- The Application Framework
- The Application Life-cycle ??
- Modules Overview
- File Module
- Graphics Module
- Asset Manager
- Tutorials
- Video Tutorials
- Beginner * Hello World * My First Triangle * Mesh, Color & Texture * Projection, Viewport, & Camera * Creating and texturing 3d models in Blender for Libgdx
- Intermediate * Actions
- Misc * AdMob in Libgdx * Integrating Libgdx and the Device camera * String Builder ??
- The Application Framework