For some reason unknown to me, when I use Buttons.LEFT with an event handler, nothing happens. Here is my code:
if (Gdx.input.isKeyPressed(Buttons.LEFT)) {
System.out.println("Mouse clicked!");
}
If I do, say, Keys.LEFT, or justTouched(), the message prints, but not when I do Buttons.LEFT. And yes, I'm 100% I'm pressing my mouse button, and that it works correctly. ;)
Thank you!
There are three main ways (that I know of) to obtaining input in LibGDX. The first is as you said, changing the ClickListener, the second will be setting the setting the current screen as an implementation of InputProcessor , and the third will be obtaining the mouse click through a new class, or a sub-class to get the input.
The next complication comes from the fact that LibGDX sets the origin at the bottom left corner, but mouse positions are relative to the top left corner. Simply subtracting the position from the screen height gives you the location of the mouse in screen coordinates.
If the mouse’s left-click button doesn’t work, only sometimes responds, accidentally “unclicks” as you drag, misclicks, or double-clicks when you click once, that’s a pretty good sign there’s something wrong with the hardware in the left-click button itself.
If your mouse’s left-click button is sticking, isn’t consistently responding, or is accidentally double-clicking, this often indicates a hardware problem with the mouse. It could be a software issue, however.
This is because Gdx.input.isKeyPressed() is for Keyboard input. If you want mouse button input you should be doing
if (Gdx.input.isButtonPressed(Buttons.LEFT)){
System.out.println("Mouse clicked!");
}
Buttons and Keys are different classes, and as such have matching methods in input. Explore the Input API Javadocs of theirs a bit more, it's helped me quite a lot.
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Input.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With