Java (and not only) keyboard input delay

Started by
4 comments, last by Glass_Knife 8 years ago

Hi everybody! New on the forum! :lol:

Recently I've found this code to scan the keys pressed:


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class KeyListenerDemo extends JPanel implements KeyListener
{

    private JTextArea textArea;

    public KeyListenerDemo()
    {
        super(new BorderLayout());
        textArea = new JTextArea(20, 40);
        JTextField textField = new JTextField();
        JButton cancella = new JButton("Cancella");
        super.add(textField, BorderLayout.NORTH);
        super.add(new JScrollPane(textArea), BorderLayout.CENTER);
        super.add(cancella, BorderLayout.SOUTH);
        textField.addKeyListener(this);


        cancella.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });
    }
	
    public void keyTyped(KeyEvent e)
    {
        textArea.append("KEYTYPED -> KeyChar : " + e.getKeyChar() +
                        "  KeyCode : " + e.getKeyCode() + "\n");
    }

    public void keyPressed(KeyEvent e)
    {
        textArea.append("KEYPRESSED -> KeyChar : " + e.getKeyChar() +
                        "  KeyCode : " + e.getKeyCode() + "\n");
    }

    public void keyReleased(KeyEvent e)
    {
        textArea.append("KEYRELEASED -> KeyChar : " + e.getKeyChar() +
                        "  KeyCode : " + e.getKeyCode() + "\n");
    }
	
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("KeyListener Demo");
        frame.getContentPane().add(new KeyListenerDemo());
        frame.pack();
        frame.setVisible(true);
    }
    
}

It works, but there is a bit of delay after the first report of an input come out, just like when you press a key and you hold it.

I also remember that I did something similar in Pascal which had the same issue.

It's a problem in gaming.

How do I solve this?

Thx!

Advertisement

Works fine on my end. Probably its just that Swing takes a bit of time to redraw the JTextArea, thus why it isn't immediate.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Do you mean like when you are typing? This is a nuicance that comes with working with swing. It is meant for text, not gaming. What you can do is set a boolean on the first keyPressed event, and reset it on keyReleased.
Programmer of HolyPoly games.

Do you mean like when you are typing? This is a nuicance that comes with working with swing. It is meant for text, not gaming. What you can do is set a boolean on the first keyPressed event, and reset it on keyReleased.

Got it. Are there other libs which don't have this problem?

As Istarnion said, have the listener set a boolean flag, and then have a thread use that boolean to update the textArea. Just be sure to synchronise the method that updates the textArea...

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

https://github.com/TimothyWrightSoftware/Fundamental-2D-Game-Programming-With-Java/blob/master/CH02/src/javagames/util/KeyboardInput.java

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement