按下Java多个键

时间:2014-06-03 23:27:11

标签: java animation user-interface set frame

我正试图将球移过一个框架。我的上,下,左,右动作都很顺利。但是当我试图对角移动时,它似乎不起作用。这是我的代码。

首先我宣布一些集合;按下键是按下的键,其他键是按键的组合,用于将球向上移动等等;

public Set<Integer> pressed = new HashSet<>();
public Set<Integer> upLeft, upRight, downLeft, downRight;

接下来,在我的init方法中,我开始将整数值添加到集合中,并将关键侦听器添加到我的框架中:

private void myInit(){
        ... // cut down code from above
        int[] a = new int[2];
        a[0] = 38; a[1] = 37;
            upLeft = new HashSet(Arrays.asList(a));
        a[1] = 39;
            upRight = new HashSet(Arrays.asList(a));
        a[0] = 40; a[1] = 37;
            downLeft = new HashSet(Arrays.asList(a));
        a[1] = 39;
            downRight = new HashSet(Arrays.asList(a));

        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });
    }

现在我的方法是处理按下和释放的键:

public synchronized void formKeyPressed(java.awt.event.KeyEvent evt) {                                
        pressed.add(evt.getKeyCode());
        System.out.println("Added:"+evt.getKeyCode());

        if(pressed.size() == 1) {
            System.out.println("Size is 1");
            if(evt.getKeyCode() == evt.VK_UP) {
                if(y <= 25) {
                    y = 25;
                }
                else  {
                    y = y - yUpSpeed;
                }
            }
            if(evt.getKeyCode() == evt.VK_DOWN) {
                if(y >= (500-25)) {
                    y = (500-25);
                }
                else {
                    y = y + yDownSpeed;
                }
            }
            if(evt.getKeyCode() == evt.VK_LEFT) {
                if(x <= 0) {
                    x = 0;
                }
                else {
                    x = x - xLeftSpeed;
                }
            }
            if(evt.getKeyCode() == evt.VK_RIGHT) {
                if(x >= (500-25)) {
                    x = (500-25);
                }
                else {
                    x = x + xRightSpeed;
                }
            }
        }

        else if(pressed.size() == 2) {
            System.out.println("Size is 2");
            if(pressed.containsAll(upLeft)) {
                if(y <= 25) {
                    y = 25;
                }
                else  {
                    y = y - yUpSpeed;
                }
                if(x <= 0) {
                    x = 0;
                }
                else {
                    x = x - xLeftSpeed;
                }
            }
            if(pressed.containsAll(upRight)) {
                if(y <= 25) {
                    y = 25;
                }
                else  {
                    y = y - yUpSpeed;
                }
                if(x >= (500-25)) {
                    x = (500-25);
                }
                else {
                    x = x + xRightSpeed;
                }
            }
            if(pressed.containsAll(downLeft)) {
                if(y >= (500-25)) {
                    y = (500-25);
                }
                else {
                    y = y + yDownSpeed;
                }
                if(x <= 0) {
                    x = 0;
                }
                else {
                    x = x - xLeftSpeed;
                }
            }
            if(pressed.containsAll(downRight)) {
                if(y >= (500-25)) {
                    y = (500-25);
                }
                else {
                    y = y + yDownSpeed;
                }
                if(x >= (500-25)) {
                    x = (500-25);
                }
                else {
                    x = x + xRightSpeed;
                }
            }
        }
        else {
            System.out.println("Size is more than 2");
        }

    public synchronized void formKeyReleased(java.awt.event.KeyEvent evt) { 
            System.out.println("Removed:"+evt.getKeyCode());
            pressed.remove(evt.getKeyCode());
        }

问题在于,从debug system.out消息中,按下的集合的大小永远不会大于1。我做错了什么?

1 个答案:

答案 0 :(得分:0)

查看Motion Using the Keyboard中的KeyBoardAnimation示例。它保留当前按下的Map个键,然后根据Map中的所有键移动对象。

此方法使用Key Bindings,由于博客中给出的原因,它优先于KeyListener。