简体   繁体   English

如何定义数字键盘的笔触-输入密钥

[英]How to define the keyStroke for the numpad - enter key

I'm about to use keybinding in a swing application for the num pad enter key, but the key is difficult to catch. 我将在Swing应用程序中为数字键盘Enter键使用键绑定,但是该键很难捕捉。 All examples I have seen rely on something like 我看到的所有示例都依赖于类似
key == KeyEvent.VK_KP_LEFT

where VK_KP_LEFT is some predefined value. 其中VK_KP_LEFT是一些预定义的值。 Other options are to define a keystoke like this: 其他选项是定义这样的按键:
KeyStroke.getKeyStroke("control A");
KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK);

but I have not found the "modifier" for numpad. 但我没有找到小键盘的“修饰符”。

What is easily to obtain is the difference between the general and the numpad-enter: All numpad-keys (indepently if switched in the numeric mode or not) get assigned the 容易获得的是常规键和numpad输入键之间的区别:所有numpad键(无论是否以数字方式切换,都可以独立分配)
getKeyLocation() == 4
(I spottetd this from key pressed / key released methods) (我从按键/释放键的方法中发现了这一点)

The question is: 问题是:
How to properly prepare the keyStroke for numpad enter key to use it in the 如何正确地为小键盘准备keyStroke输入密钥以在键盘中使用它
inputMap.put(KeyStroke keyStroke, Object actionMapKey)
key binding method? 密钥绑定方法?

Thanks, 谢谢,
Tarik 塔里克

If you're looking for binding Enter key you can use KeyEvent.VK_ENTER , ie: 如果要查找绑定Enter键,则可以使用KeyEvent.VK_ENTER ,即:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "someAction");
getActionMap().put("someAction", someAction);

Here is a short example: 这是一个简短的示例:

import java.awt.event.*;
import javax.swing.*;

public class Test {
    public static void main(String[] args) throws Exception {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.add(new JLabel("Hit Enter"));

        Action someAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Got it");
            }
        };

        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "someAction");
        panel.getActionMap().put("someAction", someAction);

        frame.add(panel);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

EDIT: VK_ENTER on numeric pad vs main keyboard 编辑:数字键盘上的VK_ENTER与主键盘

After some testing, it looks like it may not be possible to bind these keys separately. 经过一些测试,看起来可能无法单独绑定这些密钥。 The same KeyStroke is generated for both keys. 两个键都生成相同的KeyStroke The implementation of JComponent.processKeyBinding does not examine the KeyEvent , all it cares is KeyStroke in order to find the desired action. JComponent.processKeyBinding的实现不检查KeyEvent ,它关心的只是KeyStroke ,以查找所需的操作。

SwingUtilities.notifyAction that is responsible for dispatching the actual action does not delegate all the details of the KeyEvent (only key, modifiers and when). 负责调度实际操作的SwingUtilities.notifyAction不会委派KeyEvent所有详细信息(仅键,修饰符和时间)。 So inside action there is no way to distinguish either as there is no details in ActionEvent . 因此,在Action内部,由于ActionEvent没有详细信息,因此也无法区分。

If it worth the trouble, you can override processKeyBinding and add some logic if needed. 如果遇到麻烦,则可以覆盖processKeyBinding并在需要时添加一些逻辑。 You can also use KeyboardFocusManageraddKeyEventDispatcher() for blocking one of the keys. 您也可以使用KeyboardFocusManageraddKeyEventDispatcher()阻止键之一。

How about this? 这个怎么样?

if(keyEvent.getKeyLocation() == KeyEvent.KEY_LOCATION_NUMPAD 
&& keyEvent.getKeyCode() == KeyEvent.VK_ENTER)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM