Ускоритель JavaFX с использованием цифровых клавиш (Ctrl+1), а не кроссплатформенной формы?

У меня есть проект JavaFX, в котором есть строка меню и сочетания клавиш, назначенные кнопкам меню. Эти сочетания клавиш работают нормально, пока я не попытаюсь использовать цифровые клавиши (1234567890). Я пытаюсь назначить ярлыки для вкладок, чтобы Ctrl+1 выбирал первую вкладку, Ctrl+2 вторую и так далее. Тезисы отлично работают в Windows, но сочетания клавиш Ctrl+1 и Ctrl+0 не работают в macOS, и ни одно из них не работает в Linux (Mint 21.1).

Все клавиатуры французские (азерты).

Я попытался переопределить ускоритель fxml в инициализаторе оконного контроллера, используя KeyCode.SOFTKEY_1, DIGIT1 и AMPERSAND, но ни один из них не работает (см. HelloControler.java)

В новом проекте JavaFX:

привет-view.fxml

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

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.minrepex.HelloController">
    <MenuBar fx:id="mainMenuBar">
        <Menu text="Vue">
            <MenuItem text="Manatee" accelerator="Ctrl+1" fx:id="button1"/>
            <MenuItem text="Manatee" accelerator="Ctrl+2" fx:id="button2"/>
            <MenuItem text="Manatee" accelerator="Ctrl+3" fx:id="button3"/>
            <MenuItem text="Manatee" accelerator="Ctrl+4" fx:id="button4"/>
            <MenuItem text="Manatee" accelerator="Ctrl+5" fx:id="button5"/>
            <MenuItem text="Manatee" accelerator="Ctrl+6" fx:id="button6"/>
            <MenuItem text="Manatee" accelerator="Ctrl+7" fx:id="button7"/>
            <MenuItem text="Manatee" accelerator="Ctrl+8" fx:id="button8"/>
            <MenuItem text="Manatee" accelerator="Ctrl+9" fx:id="button9"/>
            <MenuItem text="Manatee" accelerator="Ctrl+0" fx:id="button0"/>
        </Menu>
    </MenuBar>
</VBox>

HelloController.java:

      package com.example.minrepex;

import javafx.fxml.FXML;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;

public class HelloController {
    private final boolean isMacos;

    @FXML private MenuBar mainMenuBar;
    @FXML private MenuItem button1;
    @FXML private MenuItem button2;
    @FXML private MenuItem button3;
    @FXML private MenuItem button4;
    @FXML private MenuItem button5;
    @FXML private MenuItem button6;
    @FXML private MenuItem button7;
    @FXML private MenuItem button8;
    @FXML private MenuItem button9;
    @FXML private MenuItem button0;

    public HelloController() {
        final String os = System.getProperty("os.name");
        this.isMacos = (os != null && os.startsWith("Mac"));
    }

    @FXML
    public void initialize() {

        if (isMacos){
            // Use macOS menu bar
            mainMenuBar.setUseSystemMenuBar(true);
        }

        button1.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button1.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button2.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button3.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button4.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button5.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button6.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button7.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button8.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button9.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
//      button0.setAccelerator(new KeyCodeCombination(KeyCode.AMPERSAND, KeyCombination.CONTROL_DOWN));
    }
}

HelloApplication.java(приложение)

      package com.example.minrepex;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Main.java

      package com.example.minrepex;

public class Main {

    public static void main(String[] args) {
        HelloApplication.main(args);
    }

}

Что происходит ?

0 ответов

Другие вопросы по тегам