简体   繁体   English

复杂gui的关键绑定

[英]Key Bindings for complex gui

I am trying to get my application to respond to keyboard input. 我试图让我的应用程序响应键盘输入。 Eventually, I would like it to register ctrl+f and initiate a search, but I started simple and tried for the space bar. 最终,我希望它注册ctrl + f并启动搜索,但我开始简单并尝试空格键。 The Java tutorials on using Key Bindings got me this far, but no matter what I apply the key binding to, nothing registers. 关于使用Key Bindings的Java教程让我走得很远,但无论我将键绑定应用于什么,都没有注册。 In the below code, panel is a JPanel and the others are assorted swing objects which have been added to panel. 在下面的代码中,面板是JPanel,其他是已添加到面板的各种摆动对象。

    Action ctrlF = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
           System.out.println("Action performed");
        }
    };
    panel.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    openStallsList.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    openStalls.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    stallScroller.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    assignLabel.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    tenantInfo.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    unitSpinner.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    buildingAddress.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");
    buildingLogo.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"crtlF");

What am I missing here? 我在这里错过了什么? Does it have something to do with focus? 它与焦点有关吗? There are a couple of assorted labels and buttons which are not included on that list. 这个列表中没有包含几个各种各样的标签和按钮。 Is there any way to get panel to register all of the input from all of it's children? 有没有办法让面板注册所有孩子的所有输入?

Thanks 谢谢

First, you need bind a KeyStroke to some kind of "key". 首先,您需要将KeyStroke绑定到某种“键”。 Now personally, it's eaiser to specifiy the virtual key then using a String , as the String value can be a little temperamental, but that's me 现在个人来说,然后使用String来指定虚拟键是eaiser,因为String值可能有点气质,但那就是我

panel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),"crtlF");

Next, you need to bind the "key" to an Action 接下来,您需要将“key”绑定到Action

panel.getActionMap().put("crtlF", ctrlF);

See How to use key bindings for more details. 有关更多详细信息,请参见如何使用键绑定

The next problem you will have is the component will need to be focused before the key binding can be triggered 您将遇到的下一个问题是在触发键绑定之前需要关注组件

You could try and get the InputMap with a different focus requirement using either WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or WHEN_IN_FOCUSED_WINDOW which will allow you to change the focus level required by the component in order for the key binding to be triggered. 您可以尝试使用WHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_IN_FOCUSED_WINDOW获取具有不同焦点要求的InputMap ,这将允许您更改组件所需的焦点级别,以便触发键绑定。

ie,

int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = panel.getInputMap(condition);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),"crtlF");
//... etc

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

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