0

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 ?

Magyar_57
  • 25
  • 7
  • 1
    [mcve] please .. and unrelated: stick to java naming conventions (no underscores) – kleopatra Jan 08 '23 at 23:30
  • 1
    See [`KeyCombination`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/input/KeyCombination.html): "By using `shortcut` key modifier developers can create platform independent shortcuts." – trashgod Jan 09 '23 at 00:30
  • @kleopatra I updated my code, should be good now – Magyar_57 Jan 09 '23 at 08:45
  • hmm .. are you sure that this example is runnable without errors? there are several issues: importing awt classes, incorrect controller class name in the fxml, different names for the tab menuItem .. all of which would throw exceptions – kleopatra Jan 09 '23 at 08:45
  • @trashgod I actually want to use the control key, and not command on macOS – Magyar_57 Jan 09 '23 at 08:45
  • well I just ran this code yes, it works and the problem is present (I'm using java 19.0.1) – Magyar_57 Jan 09 '23 at 08:48
  • ahh .. you already edited the code, thanks :) my comment was for the first version.. – kleopatra Jan 09 '23 at 08:48
  • can't really help you if it's mac/linux only (I'm on Win) - just curious: a) why do you replace the accelerators (unconditionally) in initialize if you already defined them in fxml? b) why do you think that AMPERSAND might work? – kleopatra Jan 09 '23 at 11:12
  • a) because I'm trying to debug by setting different keys, and using the fxml often leads to crashes (for example '&' don't work, it has to be "&" or something) so it's simpler b) because on azerty keyboards it's the default char printed by the "1" key (1 is obtained by pressing shift), and in some games when I assign this key to an action the key printed is ampersand and not 1 – Magyar_57 Jan 09 '23 at 12:41
  • well, I would suggest to read the api doc instead of playing wild experimenting games which are not overly useful to future readers .. good luck anyway ;) – kleopatra Jan 09 '23 at 13:02
  • 1
    @Magyar_57: I can see using the control key on macOS as a personal choice, but macOS users generally expect the command key. – trashgod Jan 09 '23 at 13:34
  • 2
    @trashgod I agree, and I did use control commands for other menus (like Save), but i'd like to use control for the tabs. And this is not really related to my issue – Magyar_57 Jan 09 '23 at 16:03

0 Answers0