@sproingie
Well the goal was to have a system where no matter where I am in the code I can poll the keyboard without passing objects around or having 8 keylisteners. The most obvious solution to me was an abstract class with static methods.
@Cruiser
I’m on my phone so I can’t copy paste my code but I’ll try to explain it.
I have an abstract class called Input. It doesn’t extend anything and it doesn’t use any interfaces. In it, I have an instance of a Keyboard class as well as an init and getKeyboard method both of which are public static methods.
Keyboard is a keylistener and has a ton of static int constants for key codes as well as an array of booleans that is the same size as the number of key code constants. Basically each array element represents an individual keys being pressed (true) or normal (false). There is a reset method which just sets all the key states to false, an isKeyDown method which returns true if the element of the provided key code is true, and an isKeyUp method which returns true if the element of the provided key code is false.
In the keyPressed method I have a switch statement for the key event’s key code and for each case I will set the array element to true with my key constant. As an example, I might have case KeyEvent.VK_ESCAPE: keyboard[KEY_ESC] = true: where KEY_ESC is my constant for the escape key. I do the same thing for keyReleased but I’ll set it to false instead because that means they let go of the key.
So say they press Q. KeyPressed will be called and it will prse the switch statement to find case KeyEvent.VK_Q. In that case, I will use my int constant for Q which might have set to 15 or something. I’ll use that as my index for my boolean array and set element 15 (in this case KEY_Q I would have set to 15) to true because VK_Q was pressed. That all happens automatically in the EventDispatchThread. Now in my update method, after adding Input.getKeyboard() as a KeyListener, I can say if (Input.getKeyboard().isKeyDown(Keyboard.KEY_Q)) { … } to detect if Key Q was pressed.
I hope I explained that well enough. Basically the goal was to be able to poll the keyboard from anywhere without needing object passing or registering more than one key listener.