LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: ryiden on March 19, 2007, 21:35:24

Title: Problems with the Keyboard class
Post by: ryiden on March 19, 2007, 21:35:24
I want to rotate an object from two different classes when a keyboard event is fired. When the key Keyboard.KEY_R is down, the object should rotate to the left. And when the keys Keyboard.KEY_R and Keyboard.KEY_LCONTROL are down, the object should rotate to the right:

if (Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateLeft();
}



if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateRight();
}


Now my problem is, both events are fired on Keyboard.KEY_LCONTROL and Keyboard.KEY_R.
I tried the following without any success, because the function getNumKeyboardEvents() is incrementing until infinity:

if (getNumKeyboardEvents() == 1 && Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateLeft();
}

Title: Re: Problems with the Keyboard class
Post by: bobjob on March 20, 2007, 01:08:01
in the class containing the rotate function without the left control button add


if (!Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_R))


add a "!" in front of  left Control there for leftcontrol cant b down while r is down.

is this wot u ment
Title: Re: Problems with the Keyboard class
Post by: Fool Running on March 20, 2007, 13:21:28
Try:

if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateRight();
}
else if (Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateLeft();
}


Hope that helps ;D
Title: Re: Problems with the Keyboard class
Post by: ryiden on March 20, 2007, 17:51:44
It doesnt help, because I access the Keyboard from different objects/classes, for example multiple Rotation objects:

int angle;
int[] keys;

angle = 2
keys = new int[]{Keyboard.KEY_R};
Rotation rotateLeft = new Rotation(angle, keys);

angle  = -2;
keys = new int[]{Keyboard.KEY_LCONTROL, Keyboard.KEY_R};
Rotation rotateRight = new Rotation(angle, keys);


This is my current algorithm inside the class Rotation:
boolean hit = true;
for (int index = 0; index < keys.length; index++)
{
  if (!Keyboard.isKeyDown(keys[index]))
  {
    hit = false;
  }
}
if (hit)
{
  System.out.println("do something");
}

With this algorithm both rotations would be fired when Keyboard.KEY_LCONTROL and Keyboard.KEY_R has been hit.
I need a member function of the Keyboard class which gives me the exact amount of keys which are currently down, so I can do something like this inside the class:
if (Keyboard.currentNumberOfKeysDown() == keys.length))
{
  boolean hit = true;
  for (int index = 0; index < keys.length; index++)
  {
    if (!Keyboard.isKeyDown(keys[index]))
    {
      hit = false;
    }
  }
  if (hit)
  {
    System.out.println("do something");
  }
}

Title: Re: Problems with the Keyboard class
Post by: bobjob on March 20, 2007, 19:26:53
so wot i said didnt work??
First class keycheck

if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateRight();
}


Second class keycheck

if (!Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_R))
{
  rotateLeft();
}


only dif is the "!" not symble in the secound class with the control button
Title: Re: Problems with the Keyboard class
Post by: ryiden on March 21, 2007, 00:37:07
It didn't work, because the key assignment is not static. The keys will be assigned in the constructor of the class. I can not do !Keyboard.isKeyDown(Keyboard.KEY_...) for all the rest of the keys on my keyboard. Look at the code snippets above.
I can check for the given keys which are assigned in the constructor, but I have to be sure that no other key of the keyboard has been pressed.
Title: Re: Problems with the Keyboard class
Post by: bobjob on March 21, 2007, 01:44:10
ok ok ok, interesting, dont get y or wot ur doing but 1 way is.. btw is this betalord? cuz he was askn the same question on irc

i dont understand at which key u dont want 2 allow any other key 2 b pressed

but as soon as the key or combo of key is pressed, make an  int = Keyboard.getNumKeyboardEvents();
so that...
button triggered:
int lastKeyState = Keyboard.getNumKeyBoardEvents();
if at any point (Keyboard.getNumKeyBoardEvents() > lastKeyState) then another key has been fired.
dont allow the action from the button trigger 2 take place.
then loop
int lastKeyState = Keyboard.getNumKeyBoardEvents();
if at any point (Keyboard.getNumKeyBoardEvents() > lastKeyState) ....

if u want 2 know specifically which key im not sure how 2 help, if no key is allowed then try that
Title: Re: Problems with the Keyboard class
Post by: ryiden on March 22, 2007, 18:07:50
In this case I need an extra class with a static key counter, that could work.