Ahmadali
Ahmadali

Reputation: 25

Java FXML key combination example wanted

I am building a calculator application in Java FXML. For more functionality aside from mouse clicking, I want to add keyboard key function so that user can type a number or a character on the keyboard to make them appear on calculator screen. I am using MacBookPro, in order to get + symbol I need to press key combination CMD and + keys. I tried the following way below, but it isn't working. Only escape and backspace keys are working very well. Even I just tried to check back_slash and minus keys, still they aren't working.

Can anybody help me how to create working key combinations only in Java FXML (which includes Main.java, Controller.java and FXMLDocument.fxml)

public class FXML_MainController implements Initializable {

KeyCombination plus = new KeyCodeCombination(KeyCode.PLUS, KeyCombination.SHORTCUT_DOWN);

@FXML
private Label displayTextBottom;

@FXML
void btnOnKeyPressed(KeyEvent event) {
    if (plus.match(event)) {
        displayTextBottom.setText(event.getText());
        System.out.println("+ pressed");
        event.consume();
    }

    switch (event.getCode()) {
        case SLASH:
            displayTextBottom.setText(event.getText());
            break;
        case MINUS:
            displayTextBottom.setText(event.getText());
            break;
        case ESCAPE:
            displayTextBottom.setText("");
            break;
        case BACK_SPACE:
            if (displayTextBottom.getText().length() > 0) {
                StringBuilder removing = new StringBuilder(displayTextBottom.getText());
                removing.deleteCharAt(displayTextBottom.getText().length() - 1);
                String removed = removing.toString();
                displayTextBottom.setText(removed);
                setDisplayFontSizeBottom();
            }
            break;

        default:
            System.out.println("error");
    }
}

Upvotes: 0

Views: 637

Answers (1)

Keyur Bhanderi
Keyur Bhanderi

Reputation: 1544

Replace SLASH to DIVIDE, MINUS to SUBTRACT and Change code in this way and try now.

public class FXML_MainController implements Initializable {

        @FXML
        private Label displayTextBottom;

        final KeyCombination keyShiftPlus = new KeyCodeCombination(KeyCode.EQUALS, KeyCombination.SHIFT_ANY);
        final KeyCombination keyShiftMul = new KeyCodeCombination(KeyCode.DIGIT8, KeyCombination.SHIFT_ANY);
        final KeyCombination keyShiftDiv = new KeyCodeCombination(KeyCode.SLASH, KeyCombination.SHIFT_ANY);

    @FXML
    void btnOnKeyPressed(KeyEvent event) {

           if (keyShiftPlus.match(event) || event.getCode() == KeyCode.ADD) {
                field.setText("+");
                System.out.println("+ ADD");
                event.consume();
            }
            if (keyShiftMul.match(event) || event.getCode() == KeyCode.MULTIPLY) {
                field.setText("*");
                System.out.println("* Mul");
                event.consume();
            }
            if (keyShiftDiv.match(event) || event.getCode() == KeyCode.DIVIDE) {
                field.setText("/");
                System.out.println("/ Div");
                event.consume();
            }
            if (event.getCode() == KeyCode.MINUS || event.getCode() == KeyCode.SUBTRACT) {
                field.setText("-");
                System.out.println("- Minus");
            }

         switch (event.getCode()) {
            case DIVIDE:
                displayTextBottom.setText(event.getText());
                break;
            case SUBTRACT:
                displayTextBottom.setText(event.getText());
                break;
            case ESCAPE:
                displayTextBottom.setText("");
                break;
            case BACK_SPACE:
                if (displayTextBottom.getText().length() > 0) {
                    StringBuilder removing = new StringBuilder(displayTextBottom.getText());
                    removing.deleteCharAt(displayTextBottom.getText().length() - 1);
                    String removed = removing.toString();
                    displayTextBottom.setText(removed);
                    setDisplayFontSizeBottom();
                }
                break;

            default:
                System.out.println("error");
        }
    }

Upvotes: 1

Related Questions