[LWJGL] isKeyReleased() method?

First off, sorry if this question has already been asked. I’ve searched the docs and this site and can’t find what I’m looking for!

Right now I have a variable that I would like to increase, or decrease, every time a specific key is released I know how to use

Keyboard.isKeyDown()

and all that, but that doesn’t work for what I’m looking for. I need a function that will only increment the variable once every time the key is pressed. Right now, the variable increases constantly while the key is pressed. I need it to only increase by one every time the key is pressed. Is there any function out there that can do this? Or do I just have to make my own(which I can do).
Thanks!

Have a read of this tutorial.

Much appreciated!

I seem to be having a little trouble… I tried following what the tutorial said, but it doesn’t work! Here’s the code currently:

while (Keyboard.next()) {
			if (Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
				if (Keyboard.getEventKeyState()) {
					if (brushSize <= 4) {
						System.out.println(brushSize);
						brushSize += 1;
					} else {
						brushSize = 4;
					}
				}
			}
		}

Now, the brushSize is increased, but when I try to access it through another class, the variable says it never was! To explain better, I make a new instance of my input class and then access the brushSize variable through that instance by doing this:

		brushSize = input.brushSize;

Which obviously should work. Now, I print out the value of brushSize in the input class and it increases. However, when I print it out in the other class, it stays at 0. Basically, the code works in the input class, but does not work when accessed outside of the class. Any help?

Questions:

  • When and how is your input object obtaining user input? To my knowledge (and this is how it is done in my projects) it should be called once in each iteration of the game loop.
  • Do you only have one input object or multiple? To my understanding it would be best if you only have one input object (perhaps making it static, though take my advice lightly as I am not that experienced).
  • Could you show us more source code relevant to your problem?

This isn’t a solution to your problem but I would just like to point out that instead of doing

brushSize = input.brushSize;

you should do

brushSize = input.getBrushSize();

Some reasons from http://stackoverflow.com/questions/1568091/why-use-getters-and-setters are

  • “Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.”
  • “Hiding the internal representation of the property while exposing a property using an alternative representation.”
  • “Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without effecting existing consumers.”
  • “Controlling the lifetime and memory management (disposal) semantics of the property - particularly important in non-managed memory environments (like C++ or Objective-C).”
  • “Providing a debugging interception point for when a property changes at runtime - debugging when and where a property changed to a particular value can be quite difficult without this in some languages.”
  • “Improved interoperability with libraries that are designed to operate against property getter/setters - Mocking, Serialization, and WPF come to mind.”
  • “Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods.”
  • “Allowing the getter/setter to be passed around as lambda expressions rather than values.
    Getters and setters can allow different access levels - for example the get may be public, but the set could be protected.”

EDIT: I plugged your code into one of my projects and I believe you are only tracking pressed input, not released. It my not be the best design but here is how I obtain input, hopefully it is useful to you.

	protected boolean[] keysDown = new boolean[256];
	protected boolean[] keysPressed = new boolean[256];
	protected boolean[] keysReleased = new boolean[256];

	protected void pollInput() {
		for (int i = 0; i < this.keysDown.length; i++) {
			this.keysDown[i] = Keyboard.isKeyDown(i);
		}
		while (Keyboard.next()) {
			if (Keyboard.getEventKeyState()) {
				for (int i = 0; i < this.keysPressed.length; i++) {
					if (Keyboard.getEventKey() == i) {
						this.keysPressed[i] = true;
					}
				}
			} else {
				for (int i = 0; i < this.keysReleased.length; i++) {
					if (Keyboard.getEventKey() == i) {
						this.keysPressed[i] = false;
						this.keysReleased[i] = true;
					}
				}
			}
		}
	}

without more code it’s hard to say, but i guess what you want is:


if (brushSize < 4) { // < 4 (not <= 4)                          
    brushSize += 1;
    System.out.println(brushSize); // print after the change
} // no else

either you are accessing the wrong instance or you do not set the new instance’s brushSize member coz of something like


brushSize = brushSize;
// instead of
this.brushSize = brushSize;
// or
brushSize = other.brushSize;

in a copy constructor or clone method.

I’m sorry but none of your suggestions are even remotely helpful… First, I do need less than or equal to four, I have my reasons. Second, there can be an else. What makes you think there can’t be one? I do not need to print after the change as honestly, it was for debugging purposes and it will only print out the incorrect thing once.

As to your other suggestion, really? I think I know how to use classes and objects by now. The code used to work, but when I changed it over to the new kind of input, it stopped working.

Please, don’t just post without thinking…

@Vladiedoo, thank you! I’ll see if that helps.

thats why you have to change the ‘<=’ to ‘<’ otherwise you increment brushSize to 5!

sure, there can be an else but it makes no sense here.

well, i just thought that if you print before the change, you could -and obviously you did- not see that brushSize is 5.

the code you posted does not look as if you know, sorry.

Please, don’t just post without thinking… :wink:

Really, are you really going to do this? The brushSize variable does not increase to 5, I have tested it. Seeing as LESS THAN OR EQUAL TO means that you can only increase the number up to 4. Second, you don’t know what the rest of my code looks like, don’t say I don’t know how to code. Third, my else statement does work. I use it for other things to besides the brushSize.

Somewhere, you are changing that variable. Do you mind giving a better code example showing all the instances where you are using brushSize? (Paste-bin?) Variables usually stay put on a number unless another part of the code changes them, which might be the case here.

ok, relax, i try again…

your code says: if brushSize is less than or equal to 4 then add 1.
so what happens if it is equal to 4? it will be increased to 5!

and you did not see it because you print it before it is increased to 5.