Creating a basic key buffer

Hi all, I’ve made a really basic keyboard class that holds the states of each key, which are for me (pressed and not pressed). I’ve made the half of the class logs key presses and allows you to check whether a specific key is currently pressed. I’m just wondering how I would go about implementing a key buffer, so that I can allow the user to, for example, type characters and have the text be inserted somewhere within the program. Could anyone explain to me how this would typically be done? Any tutorials anyone has would be great too.

Thanks in advance,

Paul :slight_smile:

Porlus,
I’m not fully understanding what you are trying to do. Are you want to set up a system that stores which keys are pressed and which ones are not, then the remaining program will access that system to determine if a particular key is pressed or not?
If so, you will want to have your program “hook into” the key system, so that it can poll a key’s state. For example, a sprite class would have a reference to the key system (or just the keys it’s interested in), then it will query the reference for the key’s current state. Similar to the ActionListeners in Swing.
Let me know if that’s what you’re looking for.
Aazimon :slight_smile:

As always, the answer is “It depends”.

The two critical questions would be 1) where do you want to trade off between OO and to-the-metal. (java.util.List or a circular buffer?); 2) what do you want to do about key repeats?

Where would the text be written? Either use a JOptionPane for input or create your own TextField GUI widget.

Ah, I’ve worked out a way to do it. For anyone who’s interested I just made an ArrayList of KeyEvents and added keys to it as they’re pressed. Then I loop through this ArrayList and add them to whatever other part of the application, then at the end of that tick in the game loop I clear the ArrayList. :slight_smile: