I would like to lock the mouse inside a JFrame. That is, the mouse can not leave the contents of the JFrame (unless the user hits escape, alt-tab, or the window otherwise looses focus). Ideas?
Thanks!
I'm not sure if there's a more automatic way of doing that, but you could use the Robot class to set the mouse position. So in the event handler for when the JFrame gains focus you can start watching the mouse move event, and when the mouse moves just make sure it stays within the JFrame. If it leaves the JFrame you can use the Robot class to set the mouse's position to go back.
Then when the window loses focus, you can unregister from the mouse move event.
The Robot class is ideal for this type of thing, but I would suggest another approach.
Perhaps making the game full screen (maximizing the window pane) would achieve what you want instead. The mouse would be unable to exit the window and no ugly Robot-esque hack needs to be used to force the user to stay within the borders.
Another workaround I just thought of - lock the cursor to the centre of the Frame, and make it invisible.Then render a software cursor where the real cursor should be.
You can then lock the cursor to whatever area you want.
Here's a sneaky one could work if you don't use mouse button 2 in your game.
Use a Robot to press down BUTTON2.
The idea is so the mouse gets dragged, not moved. Whenever you get a mouse moved event, it's because the user has released button2, so press it down again.
Whenever you get a mouse dragged event, if the mouse is outside the window, put it back in.
Related
I'm using justTouched for manipulate easly touches and released in android, cause isTouched method acumulates many touches; but it works as the same in windows? Meaning, one event even when keep pressed? Or do I need another method/invoker/listener?
On desktop builds, Libgdx treats mouse button presses as touches. justTouched acts exactly the same, except that it polls mouse buttons instead of screen taps. And just like how on mobile you can't tell which finger just touched the screen, you can't tell which mouse button was just pressed. If you need to know which mouse button or finger touched down, you need to use an InputProcessor, which gives you far more information than using the Gdx.input convenience methods.
If you don't care which mouse button was just pressed, all you need is:
if (Gdx.input.justTouched()){
//...
}
Based on your comments under your question, you seem to be trying to distinguish which button just touched with || Gdx.input.isButtonPressed(Input.Buttons.LEFT)) which will return true on every frame as long as the left button is held down. And if instead you did && Gdx.input.isButtonPressed(Input.Buttons.LEFT)), then you wouldn't be sure that it's the left button that was just pressed. (Maybe you're holding down the left button and just pressed the right button.) There is no easy way to distinguish which button was pressed unless you are using an InputProcessor.
I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
What you observe is the typical behavior of the ButtonModel used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI and a custom ButtonModel that uses isRollover() to add buttons to your program's notion of a selection model.
As an alternative, consider JList, which contains a ListSelectionModel that allows MULTIPLE_INTERVAL_SELECTION. A compete example is shown here.
In some situations I need to place a JFrame right where the mouse cursor is located. Do I really need a mouse listener to track mouse move events, or I can just read current mouse position somehow?
this could help:
MouseInfo.getPointerInfo().getLocation()
I would implement the view moving, I just need to know if the mouse is offscreen (offwindow) and adjust the offset variables accordingly.
Maybe you can use a MouseListener to listen for the mouseExited event. Then you can use the MouseInfo class to get the current location of the mouse and then reset the location of the window accordingly.
If the mouse is moving too fast, the mouse may still be outside the window after you reset the location so maybe you will need to start a Timer to continually check the MouseInfo to get the current mouse location and then continually adjust the window location. If at any time a mouseEntered event is generated then you can stop the Timer.
I have a JPanel, which I would like to detect the following events
(1) When the mouse move in
(2) When the mouse move out
The (1) is quick easy. (2) is a bit tricky. Currently, I have to register event at all the components around JPanel. If the neighbor around JPanel detected a mouse move in event, this also means that JPanel is having (2) situation. However, this is a rather dirty away, as I add in new components in the future, this dirty workaround will break.
Another method is to have a timer to monitor the JPanel. If the mouse position is not within JPanel within x seconds, I can consider JPanel is having mouse move out event.
However, this seem a dirty way to me too, as having a separate timer to perform such common task is overkill.
Is there any better way, which Java platform may provide?
Have your class implement MouseListener and add it as a mouse listener on the outermost panel. You should get a mouse-entered event when the mouse moves over the panel, and mouse-exited when it leaves; regardless of whatever components the panel contains.
From the JavaDoc:
void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.