Delay between pressing a key and the key being rea

2019-07-26 15:25发布

问题:

I have noticed that while holding a key down, there is a short delay between the key being read as being held down. What I mean is this; hold down any key and observe closely. The pattern goes like this: h.....hhhhhhhhhhhhhhhhhhhhhh where . symbolizes a pause. This is fine, because when we type, it would be harder trying to write a single letter e.g. 'i'.

But what if we want to make a game? I mean, we wouldn't like the delay. This is exacly my problem. So now, let me show you my code:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tutorial3{
    static MyPanel panel;
    public static void main(String[] agrs){
        createAndStartGui();
        new gameplay();
    }
    static void createAndStartGui(){
        JFrame f = new JFrame("tutorial 3");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setPreferredSize(new Dimension(500, 300));
        f.addKeyListener(new MyKeyListener());

        panel = new MyPanel();
        f.add(panel);

        f.pack();
        f.setVisible(true);
    }
}

class gameplay {
    static String current_key = "stop";

    public gameplay(){
        begin();
    }

    void begin(){
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Tutorial3.class.getName()).log(Level.SEVERE, null, ex);
            }
            Tutorial3.panel.user.move(current_key);
            Tutorial3.panel.repaint();
            System.out.println(current_key);
            current_key = "stop";
        }
    }

    static void key_event(){

    } 


}

class MyKeyListener extends KeyAdapter{
        public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode();
        switch(keyCode){
            case KeyEvent.VK_UP:
                gameplay.current_key = "up";
                break;
            case KeyEvent.VK_DOWN:
                gameplay.current_key = "down";
                break;
            case KeyEvent.VK_LEFT:
                gameplay.current_key = "left";
                break;
            case KeyEvent.VK_RIGHT:
                gameplay.current_key = "right";
                break;
            default:
                gameplay.current_key = "stop";
        }
    }

}

class MyRectangle{
    int x;
    int y;
    public MyRectangle(int x, int y){
        this.x = x;
        this.y = y;
    }
    void move(String direction){
        switch (direction){
            case "up":
                this.y -= 10;
                break;
            case "down":
                this.y += 10;
                break;
            case "left":
                this.x -= 10;
                break;
            case "right":
                this.x += 10;
                break;
            default:
                break;
        }
    }
}

class MyPanel extends JPanel{
    MyRectangle user = new MyRectangle(10, 10);
    public MyPanel(){

    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g.drawRect(user.x, user.y, 10, 10);
    }

}

The code basically creates a square on a JPanel and moves it around according to the user's inputs (right arrow would move the square 10 pixels to the right). Everything works fine, except that there is a delay which ruins the experience.

I had a read over this question: Ignoring delay when a key is held down? but sadly enough, it appears to only apply to JavaScript. Any help?

I would also appreciate links to existing questions of this type (which I might have missed).

回答1:

What you are doing right now is setting current_key to "stop" after each 100 ms, and listening for keyPressed to assign a direction again. The problem with this is you are limited by how often keyPressed is being fired.

What you really want to do is start moving when a direction is pressed, and stop moving when the key is released.

An easy way to do this is to remove the stop line from your main loop, and instead make a keyReleased method in your MyKeyListener which sets the current_key to "stop".

This way whenever a key is pressed, we keep moving in that direction until it is released.

e.g.

class gameplay {
    static String current_key = "stop";

    public gameplay() {
        begin();
    }

    void begin() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Tutorial3.class.getName()).log(Level.SEVERE, null, ex);
            }
            Tutorial3.panel.user.move(current_key);
            Tutorial3.panel.repaint();
            System.out.println(current_key);
//          current_key = "stop"; // only "stop" when the key is released
        }
    }

    static void key_event() {}
}

class MyKeyListener extends KeyAdapter {
    // set direction to "stop" when a key is released
    @Override
    public void keyReleased(KeyEvent e) {
        gameplay.current_key = "stop";
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
                gameplay.current_key = "up";
                break;
            case KeyEvent.VK_DOWN:
                gameplay.current_key = "down";
                break;
            case KeyEvent.VK_LEFT:
                gameplay.current_key = "left";
                break;
            case KeyEvent.VK_RIGHT:
                gameplay.current_key = "right";
                break;
            default:
                gameplay.current_key = "stop";
        }
    }
}