0

I'm in the middle of a school project, but I'm stuck... In Scene Builder there is an OnKeyPressed and an OnKeyReleased 'method', and when I assign my own method (See code further down) I want to check if I'm pressing the left or right arrow key.

The problem is, OnKeyReleased works fine, but the OnKeyPressed only works when I hold down shift! Does anyone know the cause of this issue? and if so, how it can be fixed/worked around? (I'm on a MacBook, could this have anything to do with it?)

Thanks :)

Method for OnKeyPressed:

@FXML
private void Input(KeyEvent e)
{
    if(e.getCode().equals(KeyCode.LEFT))
    {
        System.out.println("Pressed left arrow key");
    }
}

FullCode:

package seasweeper.seasweeper;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class SecondaryController implements Initializable {

    @FXML
    private Label Label_Character;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void switchToPrimary() throws IOException
    {
        App.setRoot("primary");
    }

    @FXML
    private void Input(KeyEvent e)
    {
        if(e.getCode().equals(KeyCode.LEFT))
        {
            System.out.println("Pressed left");
        }

        System.out.println("KeyCode: " + e.getCode());
    }
}

FXML code:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#moveLeft" onKeyReleased="#moveLeft" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seasweeper.seasweeper.PacificController">
   <children>
      <Button layoutX="422.0" layoutY="64.0" mnemonicParsing="false" onAction="#switchToPrimary" prefHeight="27.0" prefWidth="164.0" text="Switch to Primary" />
      <Label layoutX="489.0" layoutY="31.0" text="Pacific" />
      <Label fx:id="Label_Character" layoutX="273.0" layoutY="192.0" text="Character" />
   </children>
</AnchorPane>

Other Scene Controller:

package seasweeper.seasweeper;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class PrimaryController {

    @FXML
    private Button Button_Arctic;
    @FXML
    private Button Button_Southern;
    @FXML
    private Button Button_Pacific;
    @FXML
    private Button Button_Atlantic;
    @FXML
    private Button Button_Indian;
    @FXML
    private Button Button_Pacific1;

    @FXML
    private void switchToPacific() throws IOException {
        App.setRoot("pacific");
    }

    @FXML
    private void switchToSouthern() throws IOException {
        App.setRoot("southern");
    }

    @FXML
    private void switchToAtlantic() throws IOException {
        App.setRoot("atlantic");
    }
    @FXML
    private void switchToIndian() throws IOException {
        App.setRoot("indian");
    }
    @FXML
    private void switchToArctic() throws IOException {
        App.setRoot("arctic");
    }
}

Other FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seasweeper.seasweeper.PrimaryController">
   <children>
      <Button fx:id="Button_Arctic" layoutX="226.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchToArctic" prefHeight="77.0" prefWidth="148.0" text="Arctic" />
      <Button fx:id="Button_Southern" layoutX="226.0" layoutY="309.0" mnemonicParsing="false" onAction="#switchToSouthern" prefHeight="77.0" prefWidth="148.0" text="Southern" />
      <Button fx:id="Button_Pacific" layoutX="14.0" layoutY="162.0" mnemonicParsing="false" onAction="#switchToPacific" prefHeight="77.0" prefWidth="148.0" text="Pacific" />
      <Button fx:id="Button_Atlantic" layoutX="226.0" layoutY="162.0" mnemonicParsing="false" onAction="#switchToAtlantic" prefHeight="77.0" prefWidth="148.0" text="Atlantic" />
      <Button fx:id="Button_Indian" layoutX="438.0" layoutY="162.0" mnemonicParsing="false" onAction="#switchToIndian" prefHeight="77.0" prefWidth="148.0" text="Indian" />
      <Button fx:id="Button_Shop" layoutX="556.0" layoutY="14.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" text="\$" textFill="#009712" />
   </children>
</AnchorPane>
  • Sounds like a stupid question but have you tried it with a different keyboard? – THess Nov 19 '19 at 14:31
  • We tried with two different MacBooks... Unfortunately, the same result.. – JakobxRasmussen Nov 19 '19 at 14:34
  • I am on a MacBook and cannot reproduce Im on Java 8 – Matt Nov 19 '19 at 14:36
  • I'm on Java 13 I believe - The version we were told to download in order for Scene Builder to work. Could it have something to do us trying it on MacBooks? – JakobxRasmussen Nov 19 '19 at 14:39
  • 1
    Could you add the rest of your code? Maybe there's something like `isShiftDown()` somewhere in your code preventing you to get the expected result. – THess Nov 19 '19 at 14:41
  • I get nothing from the OnKeyPressed. I used the same method for OnKeyReleased (to test), and I get: "KeyCode: " LEFT. - Which is the right key – JakobxRasmussen Nov 19 '19 at 14:43
  • THess, I don't think so. I haven't written that anywhere, (and it's a brand new project)... and what I've written is literally all the code. – JakobxRasmussen Nov 19 '19 at 14:44
  • Is the code you added in the question really everything in the whole project? Even if it's just a bit it helps us a lot to add everything. – THess Nov 19 '19 at 14:47
  • Currently its on an AnchorPane. I tried a label to begin with, but it didn't work. (I didn't even get an input) – JakobxRasmussen Nov 19 '19 at 14:53
  • Can't reproduce it either but I'm on Windows. What did you run it on @Matt – THess Nov 19 '19 at 15:06
  • So yours works? - I get no input when I press only the arrow keys. If I press shift it works fine though.... – JakobxRasmussen Nov 19 '19 at 15:07
  • This is the only code for this Scene. I have another Scene with a button I can press to change the scene to the one I'm having problems with. But that shouldn't have anything to do with this scene should it? – JakobxRasmussen Nov 19 '19 at 15:11
  • 2
    It's my best guess that that's where something went wrong. So sure, post the other scene too please – THess Nov 19 '19 at 15:12
  • 4
    You could try moving the cursor in e.g. a `TextField` (and just that without any other event handlers added anywhere in in your code). If this doesn't work, then there's an issue with javafx or with your keyboard layout or something similar. If it does work, you haven't posted the code containing the issue. BTW: `KeyCode` is an enum so you can use `==` instead of `equals`. Another option that's useful for checking multiple codes is a `switch` statement. – fabian Nov 19 '19 at 18:29

2 Answers2

0

On the same issue I changed OnKeyPressed from AnchorPane to some Button on that AnchorPane and OnKeyPressed works fine with arrows :)

@Override
    public void initialize(URL location, ResourceBundle resources) {
        someButton.addEventHandler(KeyEvent.KEY_PRESSED,
                new EventHandler<KeyEvent>() {
                    @Override
                    public void handle(KeyEvent event) {
                        switch (event.getCode().toString()) {
                            // your logic here
                        }
                        event.consume();
                    }
                });
    }
wetal
  • 1
  • 3
0

This one is a little tricky, but still relatively simple:

Node node = null;
    node.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.KP_UP) { // KeyPressed_UP
                // Do something
            }
            if (event.getCode() == KeyCode.KP_DOWN) { // KeyPressed_DOWN
                // Do something
            }
            if (event.getCode() == KeyCode.KP_LEFT) { // KeyPressed_LEFT
                // Do something
            }
            if (event.getCode() == KeyCode.KP_RIGHT) { // KeyPressed_RIGHT
                // Do something
            }
        }
    });

Remember to change "node" with your object (it could be a Scene, AnchorPane -or any kind of pane-, buttons, etc).

FARS
  • 313
  • 6
  • 20