Making your own input box

So, I would really like to make console or something for my game, but I’m kinda lazy.

The problem is that I don’t know a good way to make a box where you can enter characters, for example like JTextField. The problem is taking input. What is the best way to take input? I could just hash all the characters on the keyboard and stuff, but that is one ugly way of doing it. Are there any other ways of taking input beside hashing all the keyboard?

Once you break down the concept of a text input box, you’ll find it’s not that much work to implement a basic one. Essentially you’ll need to create a class that implements KeyListener and contains a Rectangle, a Point, and a String. Your key listener should check to see what key is being pressed and act appropriately. In the case of a backspace, you’ll want to trim a character off of the end of your classes stored String value (assuming the String isn’t already empty), in the case of an actual character, you’ll want to append the value to the end of your String. If you need to limit which keys are processed by the event handler, use the events KeyCode value to see if the key is one you want to handle (here’s an ASCII code reference chart). When it comes time to render your text control, draw your rectangle at the location stored in your Point variable, then draw the string over top of it using whatever methods your graphics library provides. If you want to avoid the text appearing outside of the control, use the Rectangles data to set a clipping bounds.

Of course the above is just a rough outline. At some point you’ll need to figure out how you want to handle things like text alignment, line wrapping, caret implementation, and other items to make it polished. If you’re going to have more than one or two controls you’ll probably want to look into implementing a control stack/manager at some point as well, but you should be able to get away without one for the time being.

If I did not make myself clear:

what I asked was a nice way to “act accordingly to which key was pressed”
What I meant by that is there any other already MADE way to do that. I know how to make it, but it is really ugly and I don’t like it. I don’t like to leave ugly things in my code :stuck_out_tongue:

Sorry, I misunderstood. Try having a look at InputMaps and ActionMaps for some ideas. These mechanisms are what’s used for handling key inputs to text components in Swing. If I recall correctly, they get their default values from the currently active L&F. That should at least give you a place to start.

I’m going to ask the stupid question here, but are you using Java2D or OpenGL? I know a way to do it in OpenGL, but not Java2D. Although, you’ll probably need a StringBuilder for each as its not immutable.

I was thinking maybe there was already a library for lwjgl made for that purpose of something, but I will just make my own :stuck_out_tongue: I know how to do it, but was I’m lazy :smiley:

I think that TWL for LWJGL probably has something you could use, but its so much easier to just do it yourself. Check this out:
theCodingUniverse Text

This reply might seem strange, but it is made for people who might be looking for this. (Like tags or something)

How to make lwjgl text input box? To take input from the keyboard with LWJGL, you can try to do this:

while(Keyboard.next()) {
			
			if((int) Keyboard.getEventCharacter() != 0) {
									
				System.out.println(Keyboard.getEventCharacter());
			
			}
			
		}

This awesome method will return the char of the keys that were pressed! It is just superb. I’m glad I rewrote my keyboard handler and discovered this method.