keyPressed事件在第一次重复时变慢 [英] keyPressed event slow on first repeat

查看:108
本文介绍了keyPressed事件在第一次重复时变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,对于这个非常奇怪的问题,我感到抱歉,但这让我发疯.

Okay so I am sorry for this being a really weird question but it is driving me insane.

我通过以下方式处理WASD运动:

I handle my WASD movement for my game via:

Action ClassWASDKeyPressed = new ClassWASDKeyPressed();
Action ClassWASDKeyReleased = new ClassWASDKeyReleased();
BitKeys movementBitKeys = new BitKeys(); //for WASD movement key pressed/releases

//pressed
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "wButtonPress");
theDesktop.getActionMap().put("wButtonPress", ClassWASDKeyPressed);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "aButtonPress");
theDesktop.getActionMap().put("aButtonPress", ClassWASDKeyPressed);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "sButtonPress");
theDesktop.getActionMap().put("sButtonPress", ClassWASDKeyPressed);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "dButtonPress");
theDesktop.getActionMap().put("dButtonPress", ClassWASDKeyPressed);
//released
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released W"), "wButtonRelease");
theDesktop.getActionMap().put("wButtonRelease", ClassWASDKeyReleased);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released A"), "aButtonRelease");
theDesktop.getActionMap().put("aButtonRelease", ClassWASDKeyReleased);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released S"), "sButtonRelease");
theDesktop.getActionMap().put("sButtonRelease", ClassWASDKeyReleased);
theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released D"), "dButtonRelease");
theDesktop.getActionMap().put("dButtonRelease", ClassWASDKeyReleased);

这是两个Action类:

Here are the two Action classes:

class ClassWASDKeyPressed extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public void actionPerformed(ActionEvent e) {
        if (chatTextField.isFocusOwner() == false){
            if (e.getActionCommand().equals("w")){
                keyPressed(87);
            } else if(e.getActionCommand().equals("a")){
                keyPressed(65);
            } else if(e.getActionCommand().equals("s")){
                keyPressed(83);
            } else if(e.getActionCommand().equals("d")){
                keyPressed(68);
            }
        }
    }
}

class ClassWASDKeyReleased extends AbstractAction {
    private static final long serialVersionUID = 1L;

    public void actionPerformed(ActionEvent e) {
        if (chatTextField.isFocusOwner() == false){
            if (e.getActionCommand().equals("w")){
                keyReleased(87);
            } else if(e.getActionCommand().equals("a")){
                keyReleased(65);
            } else if(e.getActionCommand().equals("s")){
                keyReleased(83);
            } else if(e.getActionCommand().equals("d")){
                keyReleased(68);
            }
        }
    }
}

这是逻辑:

public void keyPressed(int e) {
    long endTime = System.nanoTime();
    long elapsedTime = endTime - startTime;
    double elapsedTimeSeconds = (double)elapsedTime / 1000000000.0;

    if (elapsedTimeSeconds < .125){ //let them move 8 times a second
        logger.info("KeyPressed (QUICK): " + elapsedTimeSeconds);
    } else {
        logger.info("KeyPressed (VALID): " + elapsedTimeSeconds);
        //logger.debug("Key Pressed: " + e.getKeyChar());  //FOR TROUBLESHOOTING
        movementBitKeys.keyPressed(e);
        //movementBitKeys.showKeyList();  //FOR TROUBLESHOOTING

        if (movementBitKeys.isKeyPressed(87) && !movementBitKeys.isKeyPressed(68) && !movementBitKeys.isKeyPressed(83) && !movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("North");
        }
        if (movementBitKeys.isKeyPressed(87) && movementBitKeys.isKeyPressed(68) && !movementBitKeys.isKeyPressed(83) && !movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("NorthEast");
        }
        if (movementBitKeys.isKeyPressed(87) && !movementBitKeys.isKeyPressed(68) && !movementBitKeys.isKeyPressed(83) && movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("NorthWest");
        }
        if (!movementBitKeys.isKeyPressed(87) && movementBitKeys.isKeyPressed(68) && !movementBitKeys.isKeyPressed(83) && !movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("East");
        }
        if (!movementBitKeys.isKeyPressed(87) && !movementBitKeys.isKeyPressed(68) && movementBitKeys.isKeyPressed(83) && !movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("South");
        }
        if (!movementBitKeys.isKeyPressed(87) && movementBitKeys.isKeyPressed(68) && movementBitKeys.isKeyPressed(83) && !movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("SouthEast");
        }
        if (!movementBitKeys.isKeyPressed(87) && !movementBitKeys.isKeyPressed(68) && movementBitKeys.isKeyPressed(83) && movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("SouthWest");
        }
        if (!movementBitKeys.isKeyPressed(87) && !movementBitKeys.isKeyPressed(68) && !movementBitKeys.isKeyPressed(83) && movementBitKeys.isKeyPressed(65)){
            requestCharacterMove("West");
        }
        startTime = endTime;
    }
}

public void keyReleased(int e) {
    //logger.debug("Key Released: " + e.getKeyChar());  //FOR TROUBLESHOOTING
    movementBitKeys.keyReleased(e);
    //movementBitKeys.showKeyList();  //FOR TROUBLESHOOTING
}

public void keyTyped(int e) {
    // not used - but in case I ever want it
}

还有BitSet类:

package com.jayavon.game.helper;

import java.util.BitSet;

public class BitKeys{

    private BitSet keyBits = new BitSet(256);

    public void keyPressed(final int keyCode) {
        keyBits.set(keyCode);
    }

    public void keyReleased(final int keyCode) {
        keyBits.clear(keyCode);
    }

    public void keyTyped(final int keyCode) {
        // don't care
    }

    public boolean isKeyPressed(final int keyCode) {
        return keyBits.get(keyCode);
    }

    public void showKeyList(){
        System.out.println(keyBits.toString());
    }

}

我的问题是,当您按住第一个和第二个运动"之间的一个运动键比所需的.125毫秒等待时间长得多时.这是一些示例数据:

My problem is when you hold down one of the movement keys between the first and second 'movement' is much longer than the required .125 millisecond wait. Here is some sample data:

设置1:

6059 [AWT-EventQueue-0]信息com.jayavon.game.client.MyClient-KeyPressed(有效):2.567790275

6059 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 2.567790275

6620 [AWT-EventQueue-0]信息com.jayavon.game.client.MyClient-KeyPressed(有效):0.560479937

6620 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.560479937

6670 [AWT-EventQueue-0]信息com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.0504469 6710 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.09360516 6750 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(VALID):0.129943376

6670 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.0504469 6710 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.09360516 6750 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.129943376

6791 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.04009505 6821 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.07098997 6851 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.102378686 6902 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(VALID):0.152006677

6791 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.04009505 6821 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.07098997 6851 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.102378686 6902 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.152006677

设置2:

9690 [AWT-EventQueue-0]信息com.jayavon.game.client.MyClient-KeyPressed(VALID):2.03802468

9690 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 2.03802468

10272 [AWT-EventQueue-0]信息com.jayavon.game.client.MyClient-KeyPressed(有效):0.582025645

10272 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.582025645

10322 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.054749323 10342 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.069890042 10372 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.100790212 10412 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(VALID):0.141337411

10322 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.054749323 10342 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.069890042 10372 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.100790212 10412 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.141337411

10462 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.049483458 10462 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.049720381 10512 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(QUICK):0.098888524 10542 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient-KeyPressed(VALID):0.128729361

10462 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.049483458 10462 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.049720381 10512 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (QUICK): 0.098888524 10542 [AWT-EventQueue-0] INFO com.jayavon.game.client.MyClient - KeyPressed (VALID): 0.128729361

现在是时候提问了.显然,第一次移动经过的时间取决于多长时间,所以这很有意义.我不明白的是为什么第二个运动"在0.5附近而不是在.125附近.从数据中可以看到,在按住键的同时,第三步和第四步的触发速度如此之快,以至于它们小于0.125.

And now time for the question. Obviously the first time you move the elapsed time is huge depending on how long, so that makes sense. What I dont get is why the second 'movement' is around .5 instead of closer to .125. As you can see from the data the third and fourth steps are firing so fast while holding down the key that they are less than .125.

有人能以任何方式帮助我吗?任何以任何方式改进我的代码的建议的确会提供巨大帮助.毫秒计数.

Can anyone help me out with this in any way? Any suggestions to improve my code in any way would be tremendous help indeed. Milliseconds count.

推荐答案

由于@aioobe的巨大帮助,这就是我解决此问题的方法.

Here is how I solved this due to the tremendous help of @aioobe.

Timer timer = new Timer();
timer.schedule(new TimerTask() {
    public void run() {
        checkKeyStrokes();
    }
}, 0, 0135);

public void checkKeyStrokes(){
    if (movementBitKeys.isKeyPressed(87)){ //w
        keyPressed(87);
    }
    if (movementBitKeys.isKeyPressed(65)){ //a
        keyPressed(65);
    }
    if (movementBitKeys.isKeyPressed(83)){ //s
        keyPressed(83);
    }
    if (movementBitKeys.isKeyPressed(68)){ //d
        keyPressed(68);
    }
}

希望这对其他人有帮助!

Hope this can be of help to others!!!

这篇关于keyPressed事件在第一次重复时变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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