1

I am working on a simple in-fix to post-fix converter. I want to get the user's input from the keyboard. To enter some symbols, e.g."+", the user must press shift. I am using a KeyCombination object to capture whether the user is using shift or not.

My code keeps give me this error: Key code must not match modifier key!

However, when I look at the keycode, it is not Shift, rather it is whatever number row key is pressed. E.g., if the user presses Shift + =, the keycode is EQUALS, not the Shift_DOWN modifier. The code works as expected, but I can't figure out how to get rid of this exception.

tfInput.setOnKeyPressed(e -> {

        if (e.isShiftDown()) {
            KeyCombination kc = new KeyCodeCombination(e.getCode(),
                    KeyCombination.SHIFT_DOWN);
            userInput = kc.toString();  
        }
so8857
  • 351
  • 2
  • 14
  • 1
    This just seems like the wrong approach. Why don't you use a text field and register a listener with the `textProperty()`? – James_D Mar 22 '18 at 17:26
  • @James_D It probably isn't the best approach. I'm not terribly familiar with JavaFX. I needed to know when shift was pressed along with another key, and keyCombo seemed to be what I needed – so8857 Mar 22 '18 at 17:54
  • So you plan to ignore, say, a `+` symbol entered from a number pad, where the Shift key is not used - is that the intention? – James_D Mar 22 '18 at 17:55
  • @James_D No. There is more code that deals with that. Basically anything typed into the text field tfInput is assigned to a string. But key combinations were not storing as I expected, e.g. + was being stored as =. So I had to account for that. – so8857 Mar 22 '18 at 18:00
  • @James_D No, only one listener. tfInput.setOnKeyPressed(e -> { if (e.isShiftDown()) { KeyCombination kc = new KeyCodeCombination(e.getCode(), KeyCombination.SHIFT_DOWN); userInput = kc.toString(); } else { userInput = e.getText(); } parseInput(userInput); }); – so8857 Mar 22 '18 at 18:05
  • I guess I still don't understand why you're not just using `tfInput.textProperty().addListener((obs, oldText, newText) -> {/* process newText */});` – James_D Mar 22 '18 at 18:05

1 Answers1

1

The reason you are getting the error is because the first parameter in key combination is keycode and shift is a key modifier you can stop getting this error by checking if the key is SHIFT before you continue

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;

public class Main extends Application {

    private String userInput;

    @Override
    public void start(Stage stage) {
        TextField textField = new TextField();

        textField.setOnKeyPressed(e -> {
            System.out.println(e.getCode());
            if (e.isShiftDown()&&!e.getCode().toString().equals("SHIFT")) {
                KeyCombination kc = new KeyCodeCombination(e.getCode(), KeyCombination.CONTROL_ANY);
                userInput = kc.toString();
                System.out.println(userInput);
            }
        });

        Scene scene = new Scene(textField);
        stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}
Matt
  • 3,052
  • 1
  • 17
  • 30