Is LWJGL capable of checking if a key was 'tapped?'

I want to check if a key was just pressed, and released quickly, in short.


    public boolean keyClicked(int key)
    {
        while (Keyboard.next())
            if (Keyboard.getEventKeyState())
                if (Keyboard.getEventKey() == key)
                    return true;
        return false;
    }

I have been using this code, but it does not seem to work twice in the same update frame. If it was checked once, it won’t check any others again for some reason…

Thanks,
-wes

That checks for events, so when you call the Keyboard.next it goes through each event that happened and takes it off the queue, which happens once a frame. So if you want to be able to do that, you’ll probably have to use key states or assign the function to a variable when used and use the variable instead of calling the function twice

Here is my keyboardHandler class.

public class KeyboardHandler {

	public List<Character> charsPressed = new ArrayList<Character>();

	public KeyboardHandler() {
	}

	public void processInput() {

		while (Keyboard.next()) {

			int key = Keyboard.getEventKey();
			boolean state = Keyboard.getEventKeyState();

			charsPressed.add(Keyboard.getEventCharacter());

			toggle(key, state);

		}

	}

	private void toggle(int keycode, boolean state) {
		for (Key key : keys) {
			if (key.keycode == keycode) key.toggle(state);
		}
	}

	public List<Key> keys = new ArrayList<Key>();

	public Key KEY_W = new Key(Keyboard.KEY_W);
	public Key KEY_A = new Key(Keyboard.KEY_A);
	public Key KEY_S = new Key(Keyboard.KEY_S);
	public Key KEY_D = new Key(Keyboard.KEY_D);

	public Key KEY_CTRL = new Key(Keyboard.KEY_LCONTROL);
	public Key KEY_R = new Key(Keyboard.KEY_R);
	public Key KEY_T = new Key(Keyboard.KEY_T);

	public Key KEY_K = new Key(Keyboard.KEY_K);

	public Key KEY_V = new Key(Keyboard.KEY_V);

	public void tick() {

		processInput();

		for (Key key : keys) {
			key.tick();
		}
	}
	
	public void releaseAll() {
		for(int i=0;i<keys.size();i++) {
			keys.get(i).pressed = false;
			keys.get(i).down = false;
		}
	}

	public class Key {

		public int keycode;

		public boolean down = false, pressed = false;
		private boolean wasDown = false;

		public Key(int keycode) {
			this.keycode = keycode;
			keys.add(this);
		}

		public void toggle(boolean bool) {
			down = bool;
		}

		public void tick() {

			if (!wasDown && down) {
				pressed = true;
			} else {
				pressed = false;
			}

			wasDown = down;
		}

	}

}

You also need to call tick() method in keyboardHandler each time game updates.
To create a key, specify a certain keycode from Keyboard class.
KEY_W.down is a boolean whether key is down.
KEY_W.pressed is a boolean whether key was pressed. You can swap it a little if you want it to occur when key is Pressed down or when key is Released up. Or you can create your own booleans and handle them in the tick method of Key class.
My mouseHandler class is like a replica of this.

charsPressed is a list that contains which characters were pressed this frame. You can use this list for textFields and such to take in “parameters” of which keys to assign. Try it. If you press A it will contain character ‘A’ for 1 tick. If you press a, it will contain character ‘a’ for 1 tick. If you press !@# it will contain ‘!’, ‘@’, ‘#’ for 1 tick.