Need help dealing with keyboard listener in java

In summary, the conversation discusses a program that uses a keyboard listener to move a character in the program. However, when the user lets go of the key, the character continues to move for a few more steps. The speaker believes this is due to the use of a while loop that waits a few seconds to draw an animation for the character, while the keyboard continues to queue up data. They have tried using a key released method, but it does not solve the issue. After receiving help and looking at the API, the speaker was able to solve the issue by putting an if loop around the entire method, which checks the time elapsed since the key was pressed.
  • #1
Yoyo_Guru
33
0
I have a program which uses a keyboard listener and moves a character in the program when the user places a key. However when the user let's go of the key the character keeps going a few more steps. I think what is happening is that I use a while loop and make it wait a few seconds in order to draw an animation for the character and while this is happening the keyboard is still taking in data and queuing it up then finishing everything in the queue after the player let's go of the button, I'm not completely sure if that's the problem, just what i think.

Here is the code I am using:
Code:
 public void movePlayer(int deltaX, int deltaY) throws IOException
  {
    inMovement=true;
    location[0]+=deltaX;
    location[1]+=deltaY;
    if(phaseNum==0)
    {
      phaseNum=1;
    }
    else if(phaseNum==1)
    {
      phaseNum=0;
    }
      playerPhase="player"+phaseNum+".gif";
      File input = new File(playerPhase);
      secondImage = ImageIO.read(input);
      this.paintImmediately(0, 0, 500, 500);
    long start=System.nanoTime();
    //sets the variable start to the current time then waits 200000000 nanoseconds or 0.2 seconds
    while(System.nanoTime()-start<105000000)
    {
      
    }
     if(phaseNum==0)
    {
      phaseNum=1;
    }
    else if(phaseNum==1)
    {
      phaseNum=0;
    }
      playerPhase="player"+phaseNum+".gif";
      input = new File(playerPhase);
      secondImage = ImageIO.read(input);
      location[0]+=deltaX;
      location[1]+=deltaY;
      this.paintImmediately(0, 0, 500, 500);
}

the method gets called in the method keyPressed(KeyEvent e)
So I'm just wondering how to stop the character from moving right when the player let's go of the key instead of having the character continue to move for a few more steps. and the longer the player holds down a key the more steps the character takes after the player let's go.
 
Technology news on Phys.org
  • #2
You might need another handler, one that is called when a key is released, or else modify your keyPressed method. This link might be helpful -http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html.
 
Last edited by a moderator:
  • #3
I've tried a few things, and no matter what i do in a key released method it still has the same problem cause the key presses are still queued up. I'm fairly certain that it is queuing up the key presses while the program is waiting for the while method and then executes the method using those keys afterward and if i have anything in the keyReleased it doesn't matter because it doesn't go through that until after it's gone through all the actions queued up
 
  • #4
Ok, so i finally got it working by putting an if loop around the entire method which was just:

if(System.currentTimeMillis()-e.getWhen()<2)

thanks a lot for the help, looked at the API on the link you posted.
 
  • #5
Yoyo_Guru said:
Ok, so i finally got it working by putting an if loop around the entire method which was just:

if(System.currentTimeMillis()-e.getWhen()<2)

Don't call it an if loop or no one will understand what you're talking about. An if structure is a decision control structure, that decides whether one path or another will execute, based on the value of an expression.

A loop executes a certain group of statements repeatedly.
 

FAQ: Need help dealing with keyboard listener in java

How do I implement a keyboard listener in Java?

To implement a keyboard listener in Java, you can use the KeyListener interface and its methods - keyPressed(), keyReleased(), and keyTyped(). You will need to add the KeyListener to the component that you want to listen for keyboard events on.

How do I handle different types of keyboard events in Java?

You can handle different types of keyboard events in Java by using the KeyListener methods - keyPressed(), keyReleased(), and keyTyped(). These methods will provide you with information about the key that was pressed, released or typed. You can then use this information to perform different actions based on the type of event.

How do I register a keyboard listener to a specific component in Java?

To register a keyboard listener to a specific component in Java, you can use the addKeyListener() method on the component. This method takes in a KeyListener object as a parameter, which will then listen for keyboard events on that specific component.

How can I make my keyboard listener work in a specific JFrame in Java?

To make your keyboard listener work in a specific JFrame in Java, you will need to add the KeyListener to the JFrame object, just like you would for any other component. You can also use the KeyListener methods to perform actions based on specific key events, such as updating the JFrame's components or changing the JFrame's size.

How do I stop my keyboard listener from receiving events in Java?

To stop your keyboard listener from receiving events in Java, you can use the removeKeyListener() method on the component that you no longer want to listen for events on. This will remove the KeyListener object from the component and it will no longer receive keyboard events.

Similar threads

Replies
9
Views
3K
Replies
6
Views
2K
Replies
1
Views
4K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
4
Views
12K
Back
Top