简体   繁体   English

按键绑定分开按键检测

[英]Keybindings separate key detection

I have a problem with keybindings: I wrote this program, but it only detects the down arrow. 我的键绑定有问题:我编写了此程序,但是它只能检测到向下箭头。 how do I get it to read the keys separately? 如何获得分别读取按键的信息? What did I do wrong? 我做错了什么?

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class KeysExample extends JFrame{

    public static void main(final String args[]){
        final JFrame frame = new JFrame("Frame Key");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Action actionSpace = new AbstractAction(){
            public void actionPerformed(ActionEvent actionSpaceEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Spacebar");
            }
        };

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Right Arrow");
            }
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Left Arrow");
            }
        };

        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Up Arrow");
            }
        };

        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Down Arrow");
            }
        };

        JPanel content = (JPanel) frame.getContentPane();
        KeyStroke space = KeyStroke.getKeyStroke("SPACE");
        KeyStroke right = KeyStroke.getKeyStroke("RightArrow");
        KeyStroke left = KeyStroke.getKeyStroke("LeftArrow");
        KeyStroke up = KeyStroke.getKeyStroke("UpArrow");
        KeyStroke down = KeyStroke.getKeyStroke("DownArrow");

        InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(space, "OPEN");
        inputMap.put(right, "OPEN");
        inputMap.put(left, "OPEN");
        inputMap.put(up, "OPEN");
        inputMap.put(down, "OPEN");
        content.getActionMap().put("OPEN", actionSpace);
        content.getActionMap().put("OPEN", actionRight);
        content.getActionMap().put("OPEN", actionLeft);
        content.getActionMap().put("OPEN", actionUp);
        content.getActionMap().put("OPEN", actionDown);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

Also, I know the code is somewhat extensive and some of the lines are probably redundant. 另外,我知道代码有些扩展,有些行可能是多余的。 I'm a beginner, so I still have a lot to learn about this stuff. 我是一个初学者,所以我仍然有很多要学习的知识。 I also realize that nothing displays in the window I created with JFrame, that is going to come later. 我还意识到,在用JFrame创建的窗口中什么也没显示,以后还会出现。 For know I need to fix this problem. 知道我需要解决这个问题。

but it only detects the down arrow. 但它只会检测到向下箭头。

This is because you assign all the Strokes to the "OPEN" keyword and the last Action you assign to the "OPEN" keyword is the downAction. 这是因为您将所有笔画都分配给了“ OPEN”关键字,而最后分配给“ OPEN”关键字的动作是downAction。

Each InputMap/ActionMap pairing must have a unique keyword name (like "UP", "DOWN", "RIGHT"...). 每个InputMap / ActionMap配对必须具有唯一的关键字名称(例如“ UP”,“ DOWN”,“ RIGHT” ...)。

KeyStroke.getKeyStroke("");

Also, you can't just make up Strings for the KeyStroke. 另外,您不能只为KeyStroke组成字符串。 You must use the names define in the KeyEvent class: 您必须使用在KeyEvent类中定义的名称:

KeyEvent.VK_UP becomes "UP"
KeyEvent.VK_DOWN becomes "DOWN"

etc.. 等等..

Read the API for the exact format of the String. 阅读API以获取字符串的确切格式。

You may want to check out Motion Using the Keyboard for more information and examples. 您可能想查看使用键盘运动的更多信息和示例。

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

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