Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx: Why doesn't the mouse click event Buttons.LEFT register?

Tags:

java

libgdx

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!

like image 489
Sean Heiss Avatar asked Jan 11 '13 02:01

Sean Heiss


People also ask

How to get input from a click in libGDX?

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.

How to find the position of the mouse in libGDX?

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.

Why is my left click not working on my Mouse?

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.

Why is my left-click button sticking?

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.


1 Answers

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

like image 112
nhydock Avatar answered Nov 14 '22 22:11

nhydock