PageUp in KeyCombination

Asked 2 years ago, Updated 2 years ago, 33 views

Setting keyboard shortcuts in javafx.

I checked the operation of this.

KeyCombination.valueOf("Shortcut+W")

This won't work, but how do I specify PageUp?

KeyCombination.valueOf("Shortcut + PageUp")

It won't work just by changing W and PageUp, so I think it's a problem with how to specify it.
I couldn't use KeyCode.PAGE_UP.

java

2022-09-30 14:59

1 Answers

We have verified that KeyCombination.valueOf("Shortcut+PageUp") does not work in MenuItem and that it works if you modify the code.
Use the start method directly below the sample code to test whether it works by rewriting it as follows:

Rewrite to one of the following

KeyCombination.valueOf("Shortcut + Page Up";
newKeyCodeCombination(KeyCode.PAGE_UP,KeyCombination.SHORTCUT_DOWN);

sample code

package javafxapplication1;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyComposition;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXApplication 1 extensions Application {

    @ Override
    public void start (Stage primaryStage) {
        // The two lines below are the same expression.
        KeyCombination copyKey=KeyCombination.valueOf("Shortcut + Page Up");
        // KeyCombination copyKey = new KeyCodeCombination (KeyCode.PAGE_UP, KeyCombination.SHORTCUT_DOWN);

        MenuItem = new MenuItem("Test";
        m.setAccelerator(copyKey);
        Button btn = new Button();
        m.setOnAction(ActionEvent)->{
            btn.fire();
        });
        ContextMenu cm = new ContextMenu(m);

        btn.setText("Say'Hello World");

        btn.setOnAction(ActionEvent)->{
            System.out.println("Hello World!");
        });

        btn.setContextMenu(cm);

        StackPane root=new StackPane();
        root.getChildren().add(btn);

        Scenescene= new Scene(root, 300, 250);
        scene.setOnKeyPressed((KeyEvent event)->{
            System.out.println(event.getCode().toString()); // CONTROL, PAGE_UP appears
        });

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

The API references KeyCombination and KeyCode also did not know the name of the special key to use in KeyCombination #valueOf.
Please let me know if anyone knows.


2022-09-30 14:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.