Skip to content

Instantly share code, notes, and snippets.

@mgsx-dev
Created April 30, 2020 03:47
Show Gist options
  • Save mgsx-dev/059ecae615c14f7095588e6b632486bc to your computer and use it in GitHub Desktop.
Save mgsx-dev/059ecae615c14f7095588e6b632486bc to your computer and use it in GitHub Desktop.
Libgdx keyboard abstract controls for ARROWS, WSAD, ZSQD layouts
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
public class UniControl {
public static final int DOWN = 1, RIGHT = 3, UP = 0, LEFT = 2;
// Layouts ARROWS, WSAD, ZSQD
private static final int [][] keyLayouts = {
{Input.Keys.UP, Input.Keys.DOWN, Input.Keys.LEFT, Input.Keys.RIGHT},
{Input.Keys.W, Input.Keys.S, Input.Keys.A, Input.Keys.D},
{Input.Keys.Z, Input.Keys.S, Input.Keys.Q, Input.Keys.D}, // this one is not necessary with LWJGL3 backend.
};
public static boolean isPressed(int direction){
for(int [] keyLayout : keyLayouts){
if(Gdx.input.isKeyPressed(keyLayout[direction])) return true;
}
return false;
}
public static boolean isJustPressed(int direction){
for(int [] keyLayout : keyLayouts){
if(Gdx.input.isKeyJustPressed(keyLayout[direction])) return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment