Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Key Bindings work with delay.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Thanked 0 Times in 0 Posts

    Default Key Bindings work with delay.

    (This has been posted in another forum, but it often takes time to get help there and I really need to solve this. I hope you understand and I'll appreciate your help).

    Sorry for not finding the right sub-forum for this.

    I got Key Bindings for a game I'm working on. They're supposed to move objects when keys are pressed.

    (currently, each println(..) represents actual code to move things on the screen, will be added later).

    The printing in the console works, but with delays. The words "left", "right", etc. appear with a delay in the console, sometimes half a second delay, sometimes a few seconds delay.

    There is no delay at all sometimes, but when a lot of keys are pressed one after the other, there's a delay of several seconds, and only then the words suddenly appear in the console (like the computer had too much to handle at the same time).

    When I did the same Key Bindings in a different project without all the game logic, only pressing buttons that print words in the console - it worked perfectly without delay.

    So I suspect the problem is something with the game code, or the way I used Key Bindings inside the game code.

    How do I fix this?

    I tried putting the Key Bindings code in the thread that contains the game-loop (before the game-loop), and in the constructor. Same problem.

    Here is the relevant code of the Board class (extending JPanel), the main JPanel of the game which displays the graphics and manipulates objects.

        import java.util.*;
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        public class Board extends JPanel implements Runnable {
        	Tank tank1,tank2;
        	boolean[] keysPressed1,keysPressed2;
        	Action upAction,leftAction,rightAction,wAction,aAction,dAction;
        	InputMap inputMap;
        	ActionMap actionMap;
        	public Board(){
        		tank1 = new Tank("red");
        		tank2 = new Tank("blue");
        		inputMap = this.getInputMap();
        		actionMap = this.getActionMap();
        		Thread gameloop = new Thread(this);
        	public void paintComponent(Graphics g){
        		Graphics2D g2d = (Graphics2D) g;
        		// Drawing tanks on the screen.
        	public void run(){
        		// Key Bindings //////
        		upAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        		leftAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        		rightAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        		wAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        		aAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
        		dAction = new AbstractAction(){
        			public void actionPerformed(ActionEvent e){
                    // End Key Bindings //////
                    // Start of game loop. ////
        		int TICKS_PER_SECOND = 50;
        		int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
        		int MAX_FRAMESKIP = 10;
        		long next_game_tick = System.currentTimeMillis();
        		int loops;
        		boolean game_is_running = true;
        		while( game_is_running ) {
        		    loops = 0;
        		    while( System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) {
                            // Code to manipulate tank1.
                            // .......
                            // Code to manipulate tank2.
                            // .......
        		        next_game_tick += SKIP_TICKS;

    As I said, I suspect the program demands too much of the computer, so pressing a lot of keys one after the other, freezes the program for several seconds, and then suddenly prints the words on the console.

    Thank you for the help

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 697 Times in 679 Posts

    Default Re: Key Bindings work with delay.

    You have a delay built in to your "game loop" during which time the button presses will not be processed. There's too much going on the the run() method, but this game loop is not appropriate for a Swing application. A repetitive action in a Swing application should be driven by a javax.swing.Timer. Further, the Swing app should be running on the EDT, but it's impossible to tell if yours is.

    I can't tell you specifically how to fix what you have without seeing more code, but you should read up on using a Swing Timer and see if you can't figure out how to use that instead of what you have.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Thanked 122 Times in 114 Posts

    Default Re: Key Bindings work with delay.

    After GregBrannon's answer, I add two things:

    1) For a basic JPanel-based game it's not necessary to go deeper at the level of input/action maps (that is an advanced concept). It's sufficient to register a more common KeyListener. The real problem/bottleneck is related to hardware limitations in keyboards. You already have had an useful answer in your thread http://www.javaprogrammingforums.com...eystrokes.html

    2) If you use multi-threading you must be aware and very careful about all implications of multi-threading: mainly synchronization and "visibility" of data changes between threads.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Key bindings capture
    By Zyrion in forum Java Theory & Questions
    Replies: 7
    Last Post: October 27th, 2013, 10:46 PM
  2. Replies: 1
    Last Post: February 10th, 2011, 08:57 AM
  3. Key Bindings
    By nemo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 28th, 2010, 08:50 PM
  4. video game problem - delay in response to arrow key presses
    By gib65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2010, 07:39 PM
  5. Question...Key bindings
    By TheEnd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2010, 09:10 PM

Tags for this Thread