Sign in to follow this  
Ciozun

Java (and not only) keyboard input delay

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this