-2
\$\begingroup\$

I have my following (static) KeyListener http://pastebin.com/gR1i3Xzb

and this is my update Method:

@Override
  public void update()
  {
    input();
    /*...*/
  }

  private void input()
  {
    if (KeyBinding.downDown)
    {
      nextEntry();
    }
    else if (KeyBinding.upDown)
    {
      previousEntry();
    }
  }

someone has an idea to make single presses?

Let's say I have a menu. If I press the Down key for a short amount of time, many menu entries are skipped. But I want to take the next menu entry only once.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You could check to see if the key was pressed in the previous frame. Consider doing something like this:

bool DownPressed;
bool UpPressed;

....

if (KeyBinding.downDown)
{    
   if (!DownPressed)
   {
       DownPressed = true;
       nextEntry();
   }
}
else
{
    DownPressed = false;
}

if (KeyBinding.upDown)
{    
   if (!UpPressed)
   {
       UpPressed= true;
       previousEntry();
   }
}
else
{
    UpPressed= false;
}

I would suggest that you encapsulate the click logic in a Button class or something, as you can see this style of coding could get messy pretty quickly if you had a lot of different key bindings.

\$\endgroup\$
0

Not the answer you're looking for? Browse other questions tagged .