JTextField和F10键 - 如何删除键绑定?

时间:2013-09-05 22:23:50

标签: java swing keystroke

我正在尝试从JTextField删除 F10 按钮键绑定,但下面没有任何工作:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_TYPED), "none");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_RELEASED), "none");

实际上,我想控制弹出菜单 - 显示/隐藏,但 F10 无法正常工作 - 它正在执行其他一些操作。如果我切换到 F11 ,一切正常。

据我所知 - Shift + F10 会在各种平台上显示弹出窗口。

2 个答案:

答案 0 :(得分:5)

KeyStroke.getKeyStroke(int, int)不用于获取新闻或发布活动的KeyStroke,而是用于以KeyStroke的形式将修饰符应用于KeyEvent.SHIFT_DOWN_MASK和/或KeyEvent.CTRL_DOWN_MASK和/或KeyEvent.ALT_DOWN_MASK和/或KeyEvent.ALT_GRAPH_DOWN_MASK和/或KeyEvent.META_DOWN_MASK ......

而不是getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none");您应该使用getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

当我使用以下内容时,我可以在关注字段时触发关键操作...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextFieldKeyStroke {

    public static void main(String[] args) {
        new TestTextFieldKeyStroke();
    }

    public TestTextFieldKeyStroke() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JTextField field = new JTextField(20);
            add(field);
            JPopupMenu pop = new JPopupMenu();

            field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "happy");
            field.getActionMap().put("happy", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Happy");
                }
            });

        }
    }
}

更新了弹出窗口

基于前面的例子,我仍然遗漏了一些东西,如果我做的话......

final JTextField field = new JTextField(20);
add(field);
JPopupMenu pop = new JPopupMenu();
pop.add(new JLabel("Hello"));
field.setComponentPopupMenu(pop);

field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "happy");
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.CTRL_DOWN_MASK), "happy");
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.ALT_DOWN_MASK), "happy");
field.getActionMap().put("happy", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JPopupMenu popup = field.getComponentPopupMenu();
        popup.show(field, 0, field.getHeight());
    }
});

我可以让它显示JPopupMenu

enter image description here

答案 1 :(得分:-1)

我试过这个并且它正在运作:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "doNothing");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.SHIFT_DOWN_MASK), "doNothing");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.CTRL_DOWN_MASK), "doNothing");

        getActionMap().put("doNothing", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Do nothing
            }
        });

现在 F10 Shift + F10 Ctrl + F10 事件是除去。但是,如果我跳过getActionMap().put("doNothing", new AbstractAction() {}),那么上面的键绑定不起作用..但似乎仍然附加某些操作 - 光标正在改变(从Cursor.TEXT_CURSORCursor.MOVE_CURSOR并再次返回Cursor.TEXT_CURSOR)当我按下那些按钮时(应该什么都不做)。我已经尝试了textField.getActionMap().size()但它返回0.所以我认为动作附加到其他一些组件上。是否有可能找到它?

编辑:

这里http://docs.oracle.com/javase/...我找到了所有答案...... :)