Need help dealing with keyboard listener in java

  • Context: Java 
  • Thread starter Thread starter Yoyo_Guru
  • Start date Start date
  • Tags Tags
    Java Keyboard
Click For Summary

Discussion Overview

The discussion revolves around handling keyboard input in a Java program, specifically addressing issues with a character continuing to move after a key is released. Participants explore the mechanics of key event handling, including the queuing of key presses and the timing of actions within the program.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the character continues to move because a while loop is used to create a delay, which may cause key presses to queue up and execute after the key is released.
  • Another participant proposes adding a key release handler or modifying the keyPressed method to address the issue.
  • A participant expresses frustration that attempts to implement a key released method do not resolve the problem, as queued key presses still execute after the while loop completes.
  • A later reply indicates success in resolving the issue by adding a conditional check based on the current time and the time of the key event.
  • Another participant corrects the terminology used in the solution, clarifying the difference between an "if structure" and a "loop."

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach initially, with differing opinions on how to handle the key events. However, one participant claims to have resolved the issue with a specific conditional check.

Contextual Notes

Some participants express uncertainty about the queuing behavior of key events and how it interacts with the timing of the while loop. The discussion does not fully resolve the implications of these interactions.

Who May Find This Useful

Java developers working with keyboard input handling, particularly in game development or interactive applications, may find this discussion relevant.

Yoyo_Guru
Messages
32
Reaction score
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
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:
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
 
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.
 
Yoyo_Guru said:
Ok, so i finally got it working by putting an if loop[/color] 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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
16K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K