question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input.isKeyJustPressed() method?

See original GitHub issue

I would find a isKeyJustPressed(int) method similar to the justTouched() method very helpful.

I know this kind of event can be captured using InputProcessor, but this method would come in handy if

  • you can’t easily use InputProcessor, for example if you have several game objects that want to listen for input. In that case you need to write another class which manages an InputMultiplexer and make it available to all objects that want to listen to input.
  • you just want to check one button. Implementing InputProcessor or creating an annonymous subclass of InputAdapter seems like a lot of boiler plate code for one button.
  • you need things to happen at a specific point / in a specific order in your render() method. This requires you to create flags for all your buttons and then check them in the desired order in the render method.

I can make a PR if you want…

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
GreenLightningcommented, Aug 4, 2014

In my code I currently use this pattern in several places because I encountered one or more of the problems listed above.

private boolean keyPressed = false;

public void render() {
...
    if (Gdx.input.isKeyPressed(...)) {
        if (!keyPressed) {
            keyPressed = true;
            doStuff();
        }
    } else {
        keyPressed = false;
    }
}

I thought maybe more people have these problems and this method would simplify their game code.

1reaction
jrennercommented, Aug 4, 2014

I don’t think this requires a new method. Just create a boolean[] of length 256. At the begin of every frame reset all its value to false. Then on each keydown you set the index that equals the keycode to true. Through the rest of your loop you can check this value to see if the key was pressed this frame.

Read more comments on GitHub >

github_iconTop Results From Across the Web

com.badlogic.gdx.Input.isKeyJustPressed java code examples
Returns whether the key has just been pressed. Popular methods of Input. setInputProcessor. Sets the InputProcessor that will receive all touch and key...
Read more >
libGDX: Why doesn't Gdx.input.isKeyJustPressed() work?
I'm going to use the isKeyPressed() method and prevent multiple calls by saving the last key state and make only a call when...
Read more >
Libgdx isKeyJustPressed(Input.Keys.UP) does not work and ...
From using a similar function isKeyPressed() , when I type in (for example) UP I have to press the right arrow key to...
Read more >
Search - appsloveworld
isKeyPressed/Gdx.input.isKeyJustPressed-android-Java. ... If I might suggest an alternative approach, it sounds like what you really want is to apply a ...
Read more >
Input - TUKE
... done by calling static methods isKeyPressed(Key) or isKeyJustPressed(Key) . Keyboard keys are translated to the constants in Input. ... public Input() ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found