1

Here is my code that set action for ctrl+c combination in JavaFX. It doesn't work when there is a focus on TextField command_line. Why?

public void setCtrlC() {
    command_line.getScene().getAccelerators().put(new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_ANY),
            new Runnable() {
                @Override
                public void run() {
                    LOGGER.debug("CTRL+C pressed");
                    try {
                        if (tab_toradex.isSelected()) {
                            bw.write(3);
                            bw.flush();
                        }
                        if(tab_novatel.isSelected()){
                            bw2.write(3);
                            bw2.flush();
                        }
                    } catch (IOException e) {
                        LOGGER.debug("CTRL+C command failed");
                    }
                }
            });
}

Thanks!

alk
  • 69,737
  • 10
  • 105
  • 255
Zuzana
  • 67
  • 3
  • a possible solution is described in the answer to [this question](http://stackoverflow.com/questions/31732583/pass-context-menu-shortcuts-up-from-editing-control). I'm not sure there is a simpler solution than adding an event filter to the scene/root. – Itai Feb 07 '17 at 09:40
  • Control + C (At least on Windows) is supported by default . You want to Log when the user tries to copy some text? Actually here you want to start a new Thread? Runnable is never started... – GOXR3PLUS Feb 07 '17 at 09:40
  • I'm really not sure of this, but I think the textfield control has its own accelarators, and it's intercepting yours... – Jai Feb 07 '17 at 10:02
  • Ok, now I probably know, where the problem is. The TextField is primarily set for listening ctrl+c for "copy" But I need to override this to listener for ctrl+c as "kill process". – Zuzana Feb 07 '17 at 11:09
  • Update: There is a possibility to remove focus from TextField every time I press SEND button and then it will work (but still only if there won't be focus on this TextField). The problem then will be, that I will have to click on the TexField every time I need to write something in it... – Zuzana Feb 07 '17 at 11:32

1 Answers1

0

Ok, solved with this:

final KeyCombination keyCombinationShiftC = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);

public void setCtrlC() {

    command_line.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            if (keyCombinationShiftC.match(event)) {
                LOGGER.debug("CTRL+C pressed");

                try {
                    if (tab_toradex.isSelected()) {
                        bw.write(3);
                        bw.flush();
                    }
                    if(tab_novatel.isSelected()){
                        bw2.write(3);
                        bw2.flush();
                    }
                } catch (IOException e) {
                    LOGGER.debug("CTRL+C command failed");
                }
            }
        }
    });
}

On the other hand, now it works only while TextField is focused...

Zuzana
  • 67
  • 3