10

I want to ask you can I get key code combination of multiple keys. For example I can get the key code from this example:

public void handle(KeyEvent event) {
    if (event.getCode() == KeyCode.TAB) {
    }
}

But how I can get the key code of this example:

textField.setText("");
// Process only desired key types
if (event.getCode().isLetterKey()
        || event.getCode().isDigitKey()
        || event.getCode().isFunctionKey()) {
    String shortcut = event.getCode().getName();
    if (event.isAltDown()) {
        shortcut = "Alt + " + shortcut;
    }
    if (event.isControlDown()) {
        shortcut = "Ctrl + " + shortcut;
    }
    if (event.isShiftDown()) {
        shortcut = "Shift + " + shortcut;
    }
    textField.setText(shortcut);
    shortcutKeyEvent = event;
} else {
    shortcutKeyEvent = null;
}

Is it possible to get the key code combination of these keys Ctrl + Tab or Ctrl + A?

Jack T
  • 315
  • 1
  • 6
  • 18
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

21

No, the handled keyEvent has only one main KeyCode, for example this code

public void handle(KeyEvent event) {
    if (event.getCode() == KeyCode.TAB) { 
    }
}

will handle TAB, ALT + TAB, or CTRL + TAB etc. If you only interested in CTRL + TAB, you have 2 choices:
1) using isControlDown()

public void handle(KeyEvent event) {
    if (event.getCode() == KeyCode.TAB && event.isControlDown()) { 
    }
}

2) using KeyCodeCombination

final KeyCombination kb = new KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN);
...
...
public void handle(KeyEvent event) {
    if (kb.match(event)) { 
    }
}
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • 2
    Also consider `KeyCombination.SHORTCUT_DOWN`. "By using shortcut key modifier, developers can create platform independent shortcuts." – trashgod Aug 03 '15 at 17:36
  • 1
    Problem is because it works also when you only press Tab without Control_Down. How to make Handler which will work only if both keys are pressed ? – Miljan Rakita Jul 04 '16 at 09:09
-1

I Don't see directly there is any way except Menus but still We can handle multi key event e.g. Ctrl + S by below work around.

at controller class level keep

public static boolean halfCtrlSPressed=false;

and in Event filter add logic as

if(ke.getCode().getName() == "Ctrl") {
            halfCtrlSPressed=true;
        }else if(ke.getCode().getName() == "S"  && halfCtrlSPressed) {
            halfCtrlSPressed=false;
            //doDomething
}