Libgdx - Check if a key is being held down?

I'm using the java libgdx game library and im curious if I can tell if a key is being HELD, not pressed and let go.

I need to know this because I'm going to play a shorter mp3 file if it is just pressed and a longer one if it is held.

Answers


Yes, you can easily check they either via Gdx.input.isKeyPressed(Input.Keys.XXX) or by implementing an InputProcessor.

public class MyInputProcessor implements InputProcessor {

    public boolean keyPressed;

    @Override
    public boolean keyDown(int keycode) {
        if (keycode == Input.Keys.XXX) {
            keyPressed = true;
        }

        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        if (keycode == Input.Keys.XXX) {
            keyPressed = false;
        }

        return false;
    }
}

And using it like this:

MyInputProcessor processor = new MyInputProcessor();
Gdx.input.setInputProcessor(processor);

...

if (processor.keyPressed) {
    // do some stuff
}

You can read more about that here.


Need Your Help

VBA Code does not work when I try to submit multiple entries at once

vba excel-vba excel

I have this piece of VBA code that works whenever I paste one entry into column 16 (see code below).

firing itemtap event on Ext.List

list events sencha-touch

I have defined a sencha List as below