JavaFX Accelerator using Numeric keys (Ctrl+1) not cross plateform?

148 Views Asked by At

I have a JavaFX project that has a menu bar, and keyboard shortcuts assigned to the menu buttons. These shortcuts works fine, until I try to use the numeric keys (1234567890). I'm trying to assign shortcuts to tabs, so that Ctrl+1 selects the first tab, Ctrl+2 the second, and so on. Theses work fine on Windows, but the Ctrl+1 and Ctrl+0 shorcuts don't work on macOS, and not a single one works on Linux (Mint 21.1).

All the keyboards are french (Azerty).

I tried to override the fxml accelerator in the window controller initalizer, using KeyCode.SOFTKEY_1, DIGIT1 and AMPERSAND, but none of them work (see HelloControler.java)

In a new JavaFX project:

hello-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 (app)

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

}

What is going on ?

0

There are 0 best solutions below