Understanding key events or not

ok, I cant seem to find this or I don’t understand how it works. I need to capture the key press(up and down or click) to turn things off and on.

example: key type O to turn on shields, key type O again to turn them off.

I tried

if isKeyDown { shielsUp = true; }
else { shieldsUp = false; }

but this sets/calls shieldsUp = false every frame tick…or is this the way to do it?

Thanks

M

Use buffered input. Hopefully it is explained in the javadoc. If not ther are some tips here: http://www.puppygames.net/forums/viewtopic.php?t=466

Are you trying to set up the key to toggle or hold for effect and release to stop effect?

If you want a toggle it would be something like this:


if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) {
    f1 = true;
    switchMode();
}
if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {
    f1 = false;
}

This will ensure that holding down the key will produce only one event. You will need to release and then press the key again to switch again.

With buffered input you will get events when the keys are pressed or released. No need to litter the code with state variables.

one press and release turns on and another press and release turns off.

and thanks tom, Ill take a look. :slight_smile:

Its funny, i saw that and read that a while backa nd even posted this question at the end of it. After rereading it just now, I understand, thanks!!