聆听组件层次结构的关键事件 [英] Listening to key events for a component hierarchy

查看:197
本文介绍了聆听组件层次结构的关键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swing应用程序需要根据是否按下控制键或alt键显示不同的控件集。我向主组件添加了一个KeyListener,但是只有选择了该组件,才会通知KeyListener,而不是选择一个子组件。有没有办法听组件和所有后代的事件?

I have a Swing app that needs to display different sets of controls based on whether the control or alt keys are pressed. I added a KeyListener to the main component, but it is notified only if that component is selected, not if a subcomponent is selected. Is there a way to listen to events for a component and all descendents?

编辑:

我尝试使用主要组件的InputMap,但按下修饰键时没有触发任何事件。具体来说,我有以下代码:

I tried using the main component's InputMap, but no event is fired when pressing a modifier key. Specifically, I have the following code:

InputMap inputMap = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke("pressed CONTROL"), "test1");
inputMap.put(KeyStroke.getKeyStroke("released CONTROL"), "test2");
ActionMap actionMap = panel.getActionMap();
actionMap.put("test1", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("pressed");
    }
});
actionMap.put("test2", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("released");
    }
});

当按下并释放控制键时,这将打印释放但不打印。没有任何其他的东西在任何InputMap中注册,所以它不像其他东西注册同一个关键笔划。

When pressing and releasing the control key, this will print "released" but not "pressed". Nothing else is registering anything in any InputMap, so it's not as though something else is registered for the same key stroke.

推荐答案

更改您的 getKeyStroke(...)调用如下:

InputMap inputMap = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, KeyEvent.CTRL_DOWN_MASK, false), "test1");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0, true), "test2");
ActionMap actionMap = panel.getActionMap();
actionMap.put("test1", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("pressed");
    }
});
actionMap.put("test2", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("released");
    }
});

我花了很多尝试和错误来提出正确的咒语。

I took me a lot of trial and error to come up with the correct incantation.

使用getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)来重要的是能够收听孩子和更深入的小部件。

It is important to use getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT), to be able to listen to child and deeper widgets.

它特别重要的是在查找控制键PRESS事件时指定 KeyEvent.CTRL_DOWN_MASK ,除了 KeyEvent.VK_CONTROL 之外。您修改后的代码示例中缺少此详细信息。

It is especially important to specify KeyEvent.CTRL_DOWN_MASK, in addition to KeyEvent.VK_CONTROL when looking for control key PRESS events. This particular detail was missing from your modified code example.

这篇关于聆听组件层次结构的关键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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