【问题标题】:JavaFX (Scene builder) arrow keys OnKeyPressedJavaFX(场景生成器)箭头键 OnKeyPressed
【发布时间】:2021-11-06 04:15:56
【问题描述】:

我正在做一个学校项目,但我被困住了...... 在 Scene Builder 中有一个 OnKeyPressed 和一个 OnKeyReleased '方法',当我分配我自己的方法时(参见下面的代码),我想检查我是按左箭头键还是右箭头键。

问题是,OnKeyReleased 工作正常,但 OnKeyPressed 仅在我按住 shift 时才有效! 有谁知道这个问题的原因?如果是这样,如何解决/解决它? (我在 MacBook 上,这与它有什么关系吗?)

谢谢:)

OnKeyPressed 的方法:

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

完整代码:

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 代码:

<?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>

其他场景控制器:

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");
    }
}

其他 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>

【问题讨论】:

  • 听上去是个愚蠢的问题,但你用不同的键盘试过了吗?
  • 我们尝试了两台不同的 MacBook... 不幸的是,结果相同..
  • 您可以添加其余代码吗?也许你的代码中某处有类似isShiftDown() 的东西阻止你得到预期的结果。
  • 这是我的最佳猜测,那就是出了点问题。当然,请也发布另一个场景
  • 您可以尝试移动光标,例如TextField (只是在代码中没有添加任何其他事件处理程序的情况下)。如果这不起作用,那么 javafx 或您的键盘布局或类似的东西有问题。如果它确实有效,则您尚未发布包含该问题的代码。顺便说一句:KeyCode 是一个枚举,因此您可以使用== 而不是equals。另一个对检查多个代码有用的选项是switch 语句。

标签: javafx scenebuilder


【解决方案1】:

在同一个问题上,我将 OnKeyPressed 从 AnchorPane 更改为该 AnchorPane 上的某个 Button,并且 OnKeyPressed 与箭头配合良好 :)

@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();
                    }
                });
    }

【讨论】:

    【解决方案2】:

    这个有点棘手,但还是比较简单的:

    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
                }
            }
        });
    

    记得用您的对象更改“节点”(它可以是场景、AnchorPane - 或任何类型的窗格、按钮等)。

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 2016-03-26
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多