Java 使用 inputmap 跟踪击键

发布于 2024-09-24 08:42:16 字数 1245 浏览 4 评论 0原文

我的代码遇到这个问题,我正在尝试学习如何在 Java 中使用击键,并且我希望能够跟踪我按下的击键。我正在尝试使用 KeyEvent.VK_UP 来跟踪我所按的内容。

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

public class TrackArrows
{
    protected static InputMap inputMap;

    public static void main(String[] arg){
        JPanel panel = new JPanel();

        inputMap = panel.getInputMap();

        panel.getActionMap().put("keys", new AbstractAction() {
            public void actionPerformed(ActionEvent e){
                if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//My problem lies here
                    System.out.println("Key pressed up");
                }
                if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//And here
                    System.out.println("Key pressed down");
                }
            }
        });

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "keys");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "keys");

        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(20,20);
        frame.setVisible(true);
    }
}

我这样做是错误的,还是有其他方法可以做到这一点?

I have this problem with my code, I'm trying to learn how to use keystrokes in Java, and i want to be able to track which keystrokes I'm pressing. I'm trying to use KeyEvent.VK_UP to track what I'm pressing.

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

public class TrackArrows
{
    protected static InputMap inputMap;

    public static void main(String[] arg){
        JPanel panel = new JPanel();

        inputMap = panel.getInputMap();

        panel.getActionMap().put("keys", new AbstractAction() {
            public void actionPerformed(ActionEvent e){
                if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//My problem lies here
                    System.out.println("Key pressed up");
                }
                if(inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), true)){//And here
                    System.out.println("Key pressed down");
                }
            }
        });

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "keys");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "keys");

        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(20,20);
        frame.setVisible(true);
    }
}

Am i wrong in doing this, or is there another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2024-10-01 08:42:16

Action 无法访问 KeyStroke。您需要为每个键绑定创建一个单独的操作。像这样的事情:

class SimpleAction extends AbstractAction
{
    public SimpleAction(String name)
    {
            putValue( Action.NAME, "Action " + name );
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println( getValue( Action.NAME ) );
    }
}

然后您创建如下操作:

Action up = new SimpleAction("Up");

但是您仍然会遇到问题,因为默认的 InputMap 仅在具有焦点时接收按键事件,并且默认情况下 JPanel 不可聚焦。因此,您有两个选择:

a)使面板可聚焦:

panel.setFocusable( true );

b)使用不同的InputMap:

inputMap = panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

按键绑定文章尝试简化 Swing 教程中的一些按键绑定概念。

The Action does not have access to the KeyStroke. You need to create a separate Action for each key binding. Something like this:

class SimpleAction extends AbstractAction
{
    public SimpleAction(String name)
    {
            putValue( Action.NAME, "Action " + name );
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println( getValue( Action.NAME ) );
    }
}

Then you create the Actions like:

Action up = new SimpleAction("Up");

However you will still have problems because the default InputMap only receives the key events when it has focus and by default a JPanel is not focusable. So you have two options:

a) make the panel focusable:

panel.setFocusable( true );

b) use a different InputMap:

inputMap = panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

The Key Bindings article attempts to simplify some of the key bindings concepts from the Swing tutortial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文