获取密钥组合代码 [英] Get key combination code

查看:157
本文介绍了获取密钥组合代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你能否获得多个密钥的密钥代码组合。例如,我可以从此示例中获取密钥代码:

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;
                }

是否可以获得这些密钥的密钥代码组合 Ctrl + Tab Ctrl + A

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

推荐答案

不,处理过的 keyEvent 只有一个主 KeyCode ,例如此代码

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

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

将处理 TAB ALT + TAB ,或 CTRL + TAB 等。如果您只对 CTRL感兴趣+ TAB ,你有2个选择:

1)使用isControlDown()

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)使用KeyCodeCombination

2) using KeyCodeCombination

final KeyCombination kb = new KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN);
...
...
public void handle(KeyEvent event) {
    if (kb.match(event)) { 
    }
}

这篇关于获取密钥组合代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆