Javafx Control EventHandler MouseClicked All isxDown() methods false - java

I can't just know which button is clicked because all below methods are returning false. I am trying to grab the click type on a HBox control and using the code below. How to differentiate right and left click?
hb.addEventHandler(MouseEvent.MOUSE_CLICKED, event ->
{
System.out.println("Meta Down?" + event.isMetaDown());
System.out.println("Middle Down?" + event.isMiddleButtonDown());
System.out.println("Primary Down?" + event.isPrimaryButtonDown());
System.out.println("Secondary Down?" + event.isSecondaryButtonDown());
System.out.println("Synthesized?" + event.isSynthesized());
}
Output;
Meta Down?false
Middle Down?false
Primary Down?false
Secondary Down?false
Synthesized?false

The reason none of them are down is because MouseClicked gets called when a mouse button is pressed and then released, in other words, it's a Mouse Pressed event followed by a Mouse Released event.
If you need to know what triggered it, look at the event.getButton() and see if it is any of the MouseButtons. Try changing your code to something like this:
hb.addEventHandler(MouseEvent.MOUSE_CLICKED, event ->
{
System.out.println("Middle Clicked?" + event.getButton()== MouseButton.MIDDLE);
System.out.println("Primary Clicked?" + event.getButton()== MouseButton.PRIMARY);
System.out.println("Secondary Clicked?" + event.getButton()== MouseButton.SECONDARY);
System.out.println("None Clicked?" + event.getButton()== MouseButton.NONE);
}

Related

How can I prevent the CTRL KeyEvent from being dispatched when pressing the right ALT key (ALT Gr)?

I am writing a key binding system for my application using key events from Swing. I am detecting the key presses using a KeyListener which I added to my Canvas.
Canvas canvas = new Canvas()
frame.add(canvas);
canvas.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println("KEY CODE: " + e.getKeyCode() + " KEY LOCATION: " + e.getKeyLocation());
}
#Override
public void keyReleased(KeyEvent e) {
}
});
Everything works as indented, the keyPressed(KeyEvent e) method of the KeyListener gets called for every key pressed on the keyboard.
The Problem arises when I press the aforementioned right ALT key. It results in two consecutive KeyEvents being dispatched. One for the left CTRL key, and one for the right ALT key. As far as I know the so dispatched event for the CTRL key is indistinguishable from the KeyEvent which is dispatched when actually pressing the CTRL key.
Is there a way to prevent the CTRL event from being dispatched when pressing the right ALT key, or do I have to make the right ALT key unbindable in order to avoid confusing behaviour?
Based on an answer in:
When i press the right alt key on my keyboard the left control gets registered pressed?
Some keyboards have al ALT key on the left, and an ALT GR key on the
right, if you have an ALT GR key, then it is correct that pressing
that key will trigger an even that tells the operating system that Alt
+Ctrl was pressed, this is normal.
It should mentioned that in some Windows keyboard layouts you have not this problem and right ALT key acts as ALT only.

JavaFx KeyEvent on Rectangle doesn't work

I have a Rectangle on a GridPane and after adding KeyEvent, it doesn't fire.
this is my code:
Rectangle rectangle = new Rectangle();
rectangle.setX(500.0f);
rectangle.setY(200.0f);
rectangle.setWidth(400.0f);
rectangle.setHeight(300.0f);
//add key event
rectangle.setOnKeyTyped(event->{
System.out.println("TYPED - Character: "+event.getCharacter()+
", Code: " + event.getCode() +
", Text: " +event.getText());
});
GridPane.setRowIndex(rectangle, 0);
GridPane.setColumnIndex(rectangle, 0);
gridPane.getChildren().add(rectangle);
Pressing any key does not print anything. What could be wrong with my code?
The problem is that the Rectangle doesn't have focus and KeyEvents doesn't trigger on Nodes which doesn't have focus. You can ask for focus by invoking rectangle.requestFocus().
It seems by simply clicking on it wont get focus so you have to give it "manually".
If you don't want to get on click you can decide when do you want to get the Rectangle the focus and after it gets the focus, it would trigger the keyEvents too.
The onClick I mentioned would look like this:
rectangle.setOnMouseClicked(event -> rectangle.requestFocus());

Libgdx actor position resets after adding another action

I'm making some simple animation using Actors (Labels, to be precize) and Actions.
My initial plan was to move one of them far to the left instantly, then make it smoothly appear through the edge of screen, replacing another. But every time a new Action is executed, Label's position resets, so I can't get the effect I planned. How do I avoid it?
Here's code, if you need it:
newAttack.addAction(Actions.moveBy(-1 * game.W, 0));
newAttack.setText(game.local.get(_attack));
newAttackType.background = game.skin.getDrawable("attack_" + getAttackType(_attack));
lastAttack.addAction(Actions.sequence(Actions.moveBy(game.W, 0, 1.5f),
Actions.run(new Runnable() {
public void run() {
System.out.println(lastAttack.getX() + " " + lastAttack.getY());
newAttack.addAction(Actions.sequence(Actions.moveBy(game.W, 0, 1.5f), Actions.run(new Runnable() {
public void run() {
lastAttack.addAction(Actions.moveBy(-1 * game.W, 0));
newAttack.addAction(Actions.moveBy(-1 * game.W, 0));
System.out.println(lastAttack.getX() + " " + lastAttack.getY() + "\n" + "--");
lastAttack.setText(game.local.get(_attack));
lastAttackType.background = game.skin.getDrawable("attack_" + getAttackType(_attack));
}
})));
}
})));
This may not work, but looking into the source code of Label.setText() reveals that the method calls Actor.invalidateHierarchy. This means that whatever is containing the lastAttack actor will reset the lastAttack's position.
So since you are adding the actions before you are calling Label.setText(), the position will be reset and then the actions won't work properly.
You should try setting the text, then setting the position of the actor to off screen, and then adding the slide in Action.

Click event on Donut Highchart with GWT

I'm using the Highchart librairy with GWT to display multiseries Donut charts.
I would like to be able to click on one point (one series' subpart) and retrieve within the event handler the name of this point.
I tried "setPointLegendItemClickEventHandler" in the "PiePlotOption" that I set to my series but that's not working.
I can't find anything in the documentation that helps me.
Any idea ?
You just need to add click event listeners in your highcharts. For capturing the click event on the inner level series, add the following code in the Series (browsers) tag inside highcharts:
point: {
events: {
click: function(event) {
alert("Name: "+this.name + " X: " + this.y + " Y: " + this.x);
}
}
}
and for capturing the click on on the outer level 2 series, add the following inside plotOptions>Pie tag:
point: {
events: {
click: function(event) {
alert(this.name);
}
}
}
You will get the name as well as X, Y values of each point clicked.
See the DEMO here.
So I found the solution. See below :
myDonutChart.setSeriesPlotOptions(new SeriesPlotOptions().setPointClickEventHandler(new PointClickEventHandler() {
#Override
public boolean onClick(PointClickEvent pointClickEvent) {
// log pointClickEvent.getPointName();
return false;
}
}));
There you go.

How can I read coordinates from a graph in Java?

My aim is to read coordinate point values from a graph in a Java program. I don't know if it even needs to be a graph. The user clicks on various parts of a plane, and I need to know which places were clicked pixel-wise so that I can find the distances between those points. Are there any simple Java libraries for accomplishing this task?
Just add a mouse listener to a JPanel.
In the mouse listener, you can do something like the following:
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at (" + e.getX() +"," +
+ e.getY + ")");
}

Categories