Mouse event with double click in java - java

By default MouseClicked event starts with one click. I have one in a JTextPane but I want to start with double click. Is it possible?

I believe you can extract the click count from the MouseEvent (assuming its called e)
Try this
if (e.getClickCount() == 2 && !e.isConsumed()) {
e.consume();
//handle double click event.
}

I don't think there will be a solution to this, since Java can run on non-pc devices.
Most portable devices don't support double-click.
You may keep track of the moment of each mouse click and fire your own "double-click" event. But I don't think this is a good idea.

private void jEditorPane3MouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 2 && !evt.isConsumed()) {
evt.consume();
System.out.println("Double Click");
}
}

You can override the mousePressed() or mouseReleased() methods and asking if e.getClickCount() == 2 , I recommend using the mousePressed() or mouseReleased() instead of mouseClicked() method since using those will give the user more time to perform the clicks.

You can compute the time lapsed between consecutive clicks. Compare it with a threshold value and decide yourself whether it is a double click or not.

Related

Can I get data from a Swing Mouse Adapter/Listener method?

I'm coding a game and I'm trying to get the value of a dice roll from a button click in my UI.
I'm not too familiar with the MVC principles but I heard it's the way to go. Any tips on how to get there from this code ?
int move = -1;
while (move == -1) {
this.btnLancerLeD.addMouseListener(new MouseAdapter() {
public int diceRoll;
#Override
public void mouseClicked(MouseEvent arg0) {
diceRoll = jeu.getDe().lancer();
move = diceRoll;
}
});
}
I'm trying to get diceRoll value to move my players on the board.
First, you should only call addMouseListener() once rather than doing it repeatedly in a while loop. Second, you can do the logic of the "move" all inside mouseClicked(). You probably need an if statement.
Note that Swing already contains the logic to wait for the user to do something. This is the whole point of event listeners. They are only fired when that event occurs. In the case of a button, you should use an ActionListener instead of a MouseListener. For more details, I suggest that you check out the official Oracle tutorials for Swing. You can find these with a quick Google search.

React on User Keyevents whitout laggs JavaFx

First thing i want to do is to apologize for my english, i will try to write as understanable as possible.
Also, i have already tried to search the solution for this problem, but i didnt find one until now..
The problematic part of the code is the following:
//eventmanagement for the bat
scene.setOnKeyPressed (new EventHandler <KeyEvent> () {
public void moveBat(double speed) {
if ((speed > 0) && ((bat.getLayoutX() + bat.getWidth())<scene.getWidth())){
bat.setLayoutX(bat.getLayoutX() + speed);
}
if ((speed < 0) && ((bat.getLayoutX() > 0))){
bat.setLayoutX(bat.getLayoutX() + speed);
}
};
#Override
public void handle(KeyEvent event){
if (event.getCode().toString() == "RIGHT"){
this.moveBat(batVelocity);
}
if (event.getCode().toString() == "LEFT"){
this.moveBat(-batVelocity);
}
}
});
This thing works, but if i press the LEFT key for example, and i dont unpress it, so just let it remain pressed, then the "bat" will move left once, then delay for about 1 second, and then continue moving in the left direction.
I want to have a continuos movement in the left direction for the time the LEFT button remains pressed. Anyone has an idea how to fix this??
Thank you very much for your time and answers!!
Crutz
So, OnKeyPressed fires when a key is first pressed and then every second or so after. If you want something to happen continuously, instead of having it happen when the OnKeyPressed event fires, consider having OnKeyPressed and OnKeyReleased control some sort of boolean keyIsPressed, and using that in a while loop of some sort. So OnKeyPressed would set keyIsPressed=true and OnKeyReleased would do the opposite
So what you wanna do is save your KeyCode to a List and check it in your AnimationTimer:
Fields:
private List<KeyCode> input = new ArrayList<>();
and your listeners:
scene.setOnKeyPressed((KeyEvent event) -> {
input.add(event.getCode());
});
scene.setOnKeyReleased((KeyEvent event) -> {
input.remove(event.getCode());
});
And in your AnimationTimer:
if (input.contains(KeyCode.RIGHT)) {
//do something
}
if (input.contains(KeyCode.LEFT)) {
//do something else
}

LWJGL: detecting left/right clicks when getButtonCount returns 0

On the Dell Inspiron 15 3000, the touchpad doesn't have any physical left/right buttons. Instead, it is one giant touchpad that is pressure sensitive. I'm assuming it detects right/left clicks based off of hand position on the trackpad.
In my LWJGL application, I detect mouse button clicks with Mouse.isButtonDown(0). This works fine on computers with a mouse with physical buttons, but doesn't work on touchpads that lack physical buttons. Mouse.getButtonCount() returns 0.
Has anybody had any success in detecting if a mouse button is pressed, should the user be using a trackpad that doesn't have physical buttons?
I think, instead of using
org.lwjgl.input.Mouse
This class could be what you are searching for:
org.lwjgl.input.Controllers
http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Controllers.html
Im not entirely sure though since I only have a mouse and no way to test this with a touchpad.
For those who find this in the future, I did find a fix:
You cannot, should there be no physical buttons, use the Mouse.isButtonDown() method. Instead, you are going to have to read the event buffer. To do so, I wrote my own helper class:
public class Mouse{
private static boolean button_left = false, button_right = false;
public static boolean isButtonDown(int button){
if(button == 0) return button_left;
if(button == 1) return button_right;
return false;
}
public static void update(){
while(org.lwjgl.input.Mouse.next()){
if(org.lwjgl.input.Mouse.getEventButton() == 0) button_left = org.lwjgl.input.Mouse.getEventButtonState();
if(org.lwjgl.input.Mouse.getEventButton() == 1) button_right = org.lwjgl.input.Mouse.getEventButtonState();
}
}
}
The update() method is called every tick, and as such I can get button states using Mouse.isButtonDown(button).

How to detect if two mouse buttons were released at the same time?

I am implementing minesweeper and I have a little problem with handling mouse events. I want actions to take place on mouseRelease (not mousePressed), so the user can change his mind after pressing a mouse button. There are three possible actions:
LMB released - reveal current field
RMB released - put a flag in current field
both LMB and RMB released - highlight or reveal adjacent fields
Actions are not really important, what I have problem with is how to trigger them.
In cases 1 and 2 no others buttons are down - this is easy to check.
The problem is that releasing both buttons results in two calls of mouseRelease method. If there is too large delay between both buttons releases I would prefer not to trigger an action. In other words, it should trigger only if both buttons were released at about the same time.
I can easily check that e.g. button 3 is down when button 1 is released, however it is not really helpful.
At the moment I have no other idea then storing a time when first button was released and then comparing it against the time when another button is released - and if the difference is small enough, trigger an action.
I do not really like an idea of playing with System.currentTimeMillis() or System.nanoTime() and I wonder whether there is a better way of doing it?
SOLUTION:
What has extremely simplified the problem was not allowing the user to drag a mouse pointer to another field (and hence changing the "selected" field). Delay between releasing both buttons does not matter. If the mouse pointer goes to another field, everything is cleared. Here is the code snippet:
private static final int B1DM = MouseEvent.BUTTON1_DOWN_MASK;
private static final int B3DM = MouseEvent.BUTTON3_DOWN_MASK;
private boolean bothWereDown = false;
#Override
public void mousePressed(MouseEvent e) {
// action if single button pressed
// in my case this is a sub-action of pressing both buttons
int both = B1DM | B3DM;
bothWereDown = (e.getModifiersEx() & both) == both;
if (bothWereDown) {
// action if both buttons pressed
}
}
#Override
public void mouseDragged(MouseEvent e) {
// some other code
if ( /* if pointer leaves field where mouse was pressed */) {
// some other code
bothWereDown = false;
// some other code
}
}
#Override
public void mouseReleased(MouseEvent e) {
// some other code
if (e.getButton() == MouseEvent.BUTTON1) {
if (bothWereDown) {
if ((e.getModifiersEx() & B3DM) != B3DM) {
// action if both released
}
} else {
// action if left button released
}
} else if (e.getButton() == MouseEvent.BUTTON3) {
if (bothWereDown) {
if ((e.getModifiersEx() & B1DM) != B1DM) {
// action if both released
}
} else {
// action if right button released
}
}
}
Obvious solution is to also track mouse presses. No need to play around with timeouts in this case, I think.
So pressing both buttons, then releasing one, will not count as release of single button. In fact, in your case, it would probably do nothing. Only when also other mouse button gets released, that will then count as releasing both buttons.
Edit: Actually, you do not even need to track mouse presses, though you probably want to give visual indication that button is pressed, and releasing it will have an effect. But when button is released, and you detect other is still pressed, just set a flag that half of two button release has happened, and single release of one button will then complete it. If flag is not set on single button release, then it's not part of two button action, and you do the single button release action instead.

Determine mouse button release in LWJGL

I have a program in LWJGL where I have a series of buttons. I'm trying to use Mouse.getEventButton() and Mouse.getEventButtonState() in order to figure out when the mouse is released, but neither seem to be working. After adding a few print statements for debugging, it seems that getEventButton() always returns 0 regardless of what the mouse is doing, and getEventButtonState() always returns false. All other mouse methods I've used so far have behaved normally. Any idea what might be going on?
It can be done by analogy with keyboard as described in this tutorial.
while (Mouse.next()){
if (Mouse.getEventButtonState()) {
if (Mouse.getEventButton() == 0) {
System.out.println("Left button pressed");
}
}else {
if (Mouse.getEventButton() == 0) {
System.out.println("Left button released");
}
}
}

Categories