简体   繁体   English

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

[英]JTextField and F10 key - how to remove key binding?

I'm trying to remove F10 button key binding from JTextField , but nothing is working below: 我正在尝试从JTextField删除F10按钮键绑定,但以下操作JTextField

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");

Actually, I want to control popup menu - show/hide, but F10 not working correctly - it is performing some other actions. 实际上,我想控制弹出菜单-显示/隐藏,但是F10无法正常工作-它正在执行其他一些操作。 If I switch for example to F11 , everything works fine. 例如,如果我切换到F11 ,则一切正常。

As I know - Shift + F10 shows popup on various platforms. 据我所知-Shift + F10显示各种平台上的弹出窗口。

KeyStroke.getKeyStroke(int, int) isn't used to get the KeyStroke for a press or release event, it is used to apply a modifier to the KeyStroke in the form of KeyEvent.SHIFT_DOWN_MASK and/or KeyEvent.CTRL_DOWN_MASK and/or KeyEvent.ALT_DOWN_MASK and/or KeyEvent.ALT_GRAPH_DOWN_MASK and/or KeyEvent.META_DOWN_MASK ... KeyStroke.getKeyStroke(int, int)不用于获取按下或释放事件的KeyStroke ,它用于以KeyEvent.SHIFT_DOWN_MASK和/或KeyEvent.CTRL_DOWN_MASK和/或KeyEvent.ALT_DOWN_MASK的形式向KeyStroke施加修饰符。 KeyEvent.ALT_DOWN_MASK和/或KeyEvent.ALT_GRAPH_DOWN_MASK和/或KeyEvent.META_DOWN_MASK ...

Instead of getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none"); 代替getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none"); you should be using getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none"); 您应该使用getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

When I use the following, I can get the key action to trigger when the field is focused... 当我使用以下命令时,我可以获取触发重点领域时触发的关键操作...

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

        }
    }
}

Updated with popup 更新了弹出窗口

I'm still missing something here, based on the previous example, if I do something like... 如果我做类似的事情,基于上一个示例,我在这里仍然缺少一些东西。

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

I can get it to show the JPopupMenu 我可以显示JPopupMenu

在此处输入图片说明

I have tried this and it's working: 我已经尝试过了,它正在工作:

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

Now F10 , Shift + F10 and Ctrl + F10 events are removed. 现在删除F10Shift + F10Ctrl + F10事件。 But if I skip getActionMap().put("doNothing", new AbstractAction() {}) , then key bindings above not working.., but it seems that somewhere some action is still attached - cursor is changing (from Cursor.TEXT_CURSOR to Cursor.MOVE_CURSOR and again backs to Cursor.TEXT_CURSOR ) when I press on those buttons (should do nothing). 但是,如果我跳过getActionMap().put("doNothing", new AbstractAction() {}) ,则上面的键绑定无法正常工作..,但是似乎仍在某些动作处附加-光标正在更改(来自Cursor.TEXT_CURSOR Cursor.MOVE_CURSOR并再次返回Cursor.TEXT_CURSOR )(当我按这些按钮时(应该什么也不做))。 I've tried textField.getActionMap().size() but it return 0. So I suppose action is attached to some other component. 我尝试了textField.getActionMap().size()但它返回0。所以我想将动作附加到其他组件上。 Is it possible to find out it? 有可能找出答案吗?

EDIT: 编辑:

Here http://docs.oracle.com/javase/... I found all the answers... :) 在这里http://docs.oracle.com/javase / ...我找到了所有答案... :)

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

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