【问题标题】:How to set on key press key binding?如何设置按键按键绑定?
【发布时间】:2012-03-19 15:19:45
【问题描述】:

此应用程序的箭头键需要引起独立于焦点的按下和释放事件。 设置 onKeyRelease true 会按预期导致释放事件,但设置 onKeyRelease false (下面的代码)似乎不会停止自动重复。 有没有办法实现按键绑定在按住方向键时触发一次?

Action right = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        ...
    }
};
mainPanel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");
mainPanel.getActionMap().put("right", right);

【问题讨论】:

  • (1):您是否试图阻止长按某个键时的自动重复? - (2):您在寻找跨平台分辨率吗?
  • @ring bearer:我不想阻止自动重复,我只想在按下或释放箭头键时触发一次事件。是的,这需要在各种 Windows 和 Linux 平台上运行。

标签: java swing key-bindings keyevent keystroke


【解决方案1】:

...可能是个好问题,但请不要把所有内容都弄清楚,请您详细说明...

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MoveIcon extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    private static final String IMAGE_PATH_PLAYER = "http://duke.kenai.com/iconSized/duke4.gif";
    public static final int STEP = 3;
    private static final int TIMER_DELAY = STEP * 8;
    private BufferedImage bkgrndImage = null;
    private BufferedImage playerImage = null;
    private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
    private int playerX = 0;
    private int playerY = 0;

    enum Direction {

        UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1),
        LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0);
        private int keyCode;
        private int xDirection;
        private int yDirection;

        private Direction(int keyCode, int xDirection, int yDirection) {
            this.keyCode = keyCode;
            this.xDirection = xDirection;
            this.yDirection = yDirection;
        }

        public int getKeyCode() {
            return keyCode;
        }

        public int getXDirection() {
            return xDirection;
        }

        public int getYDirection() {
            return yDirection;
        }
    }

    public MoveIcon() {
        try {
            URL bkgrdImageURL = new URL(IMAGE_PATH);
            URL playerImageURL = new URL(IMAGE_PATH_PLAYER);
            bkgrndImage = ImageIO.read(bkgrdImageURL);
            playerImage = ImageIO.read(playerImageURL);
            setPreferredSize(new Dimension(bkgrndImage.getWidth(), bkgrndImage.getHeight()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (Direction direction : Direction.values()) {
            directionMap.put(direction, false);
        }
        setKeyBindings();
        Timer timer = new Timer(TIMER_DELAY, new TimerListener());
        timer.start();
    }

    private void setKeyBindings() {
        InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actMap = getActionMap();
        for (final Direction direction : Direction.values()) {
            KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
            KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
            inMap.put(pressed, direction.toString() + "pressed");
            inMap.put(released, direction.toString() + "released");
            actMap.put(direction.toString() + "pressed", new AbstractAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    directionMap.put(direction, true);
                }
            });
            actMap.put(direction.toString() + "released", new AbstractAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    directionMap.put(direction, false);
                }
            });
        }

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bkgrndImage != null) {
            g.drawImage(bkgrndImage, 0, 0, null);
        }
        if (playerImage != null) {
            g.drawImage(playerImage, playerX, playerY, null);
        }
    }

    private class TimerListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean moved = false;
            for (Direction direction : Direction.values()) {
                if (directionMap.get(direction)) {
                    playerX += STEP * direction.getXDirection();
                    playerY += STEP * direction.getYDirection();
                    moved = true;
                }
            }
            if (moved) {
                int x = playerX - 2 * STEP;
                int y = playerY - 2 * STEP;
                int w = playerImage.getWidth() + 4 * STEP;
                int h = playerImage.getHeight() + 4 * STEP;
                MoveIcon.this.repaint(x, y, w, h); // !! repaint just the player
            }
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("MoveIcon");
        frame.getContentPane().add(new MoveIcon());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

【讨论】:

  • 你这里的和我做的基本一样。问题是如果按住箭头键,“KeyStroke press”会触发连续事件。在我的系统中,这会使作为事件最终目标的固件崩溃,并且是需要在固件中修复的问题,但是 GUI 应该只在按键被按下时发送一次事件,并且在按下按键时发送不同的事件密钥被释放(密钥释放按预期工作)。我想我可以添加一个设置新闻事件并重置发布事件的布尔值。但是应该有适当的方法来做到这一点。
  • @jacknad 好吧,对,那么假设您可以将 KeyBindings 的输出重新使用到 Swing Action 或 ActionLitener 有三种方法 1)从 ActionMap 中删除 Action(错误的方式) 2)禁用 Swing 的输出使用 Boolean 进行操作 3) 使用 Swing Timer 通过使用 Boolean 定义延迟来禁用 Swing Action
  • 使用布尔值禁用自动重复动作。
  • @mKorbel - 我不明白你是如何解决这个问题的? “使用布尔值禁用 Swing Action 的输出”——这到底是什么意思?谢谢
  • @0__ 非常有趣的问题,Swing Action 是可设置的、可扩展的、可共享的,我敢肯定,在官方 Oracle 教程和 API 中休息,太接近......,到......如何使用 (java.beans.)EventHandler
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多