Jump to content
  • Advertisement
Sign in to follow this  
Ciozun

Java (and not only) keyboard input delay

This topic is 895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!