Chat in Slick

I’m going to add chat functionality to a game i’m making, and i would probably be able to do it in a not-so-great way.

How would one register each keypress and write it to the chat, without having to check each letter?
i.e:


        if (input.isKeyPressed(input.KEY_A)) {
            this.text += "a";
        }

For my first one attempt at a chat box, I created a ChatBox class, when enter is pressed, the input is disabled in the game, and only will only work in the ChatBox class.

using org.lwjgl.input.Keyboard class I have a method


	public char getChatKey(){
		return Keyboard.getEventCharacter();
	}

which is inside my InputHandler class.

in my ChatBox class, I have an array of char’s (because i get random chars in the way if I don’t), I loop through the allowed keys if the key matches one of the chars in the array, it is then applied to the message string.


char key = input.getChatKey();

			for (int i = 0; i < allowedKeys.length; i++) {
				if (key == allowedKeys[i]) {
					message += key;
				}
			}

You may also want to put a delay in their so 5000 of the same keys don’t instantly get put in. or some other way of checking to make sure unwanted amounts of the same key is entered.

EDIT: added more

Thank you for the answer! :slight_smile: The chat function is however a core functionality of my game, and should be available at all times without deactivating other movement (only moves by mouse).

Where did you put that method?

Well, It is inside my InputHandler Class, I’m not to sure about slicks use of input, but the getChatKey() method is inside my input handler class.

If you want to be still be able to talk, just ignore disabling the input.

basically you just went to get the char value of what key is pressed. this will save you having to do checks for 26 letters, 26 capital letters, 10 numbers and what 20 symbols?

I can’t figure how to make use of this… :frowning:

Perhaps you’d be better off using a GUI library of some sort like TWL or LibGDX+Stage2D. Both of them have steep learning curves, but it beats writing a GUI from scratch.

slick has actually a button and a textfield by itself - nothing else, but they do work

so if you only need those 2, and you want quick results, check em out

TWL and stage2d were mentioned, although the latter is for libgdx
there is also NiftyGui

I just wrote my own… but it really depends on what you need