Typed Input

I am using the slick2d API and I wanted to make a input system that its typed. basically if I wanted to name a new game save or character or type in a IP the same way minecraft does, how would I go about doing this with slicks Input system.

A short thread on JGO that has a code example.

what are you having trouble with, specifically? Is that you don’t know how to get keyboard input at all, or is it that you’re not sure how to “hold on to” the text that is being typed in as the user types it?

I know how to get keyboard input, what I want it to do is record it to a string that can be referenced at a later time.

Edit: I looked at the link you sent and I have tried using that method and it basically spams any key I press, and the method is called key pressed, so i am not sure whats going on.

Based on actual’s post on a post I made this code, this may not be the best solution to your problem but it works.

private boolean isMyInputListenerCreated = false;
private StringBuffer userInputForIP = new StringBuffer();

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
	Input i = gc.getInput();

   // Only create MyInputListener once.
	if (!isMyInputListenerCreated) {
		createMyInputListener(i);
		isMyInputListenerCreated = true;
	}
}
private void createMyInputListener(Input i) {
	i.addKeyListener(new MyInputListener());
}
class MyInputListener implements KeyListener {
	public void keyPressed(int key, char c) {
		if (key == 14) {
         // If the key is backspace and the length of the String Buffer isn't zero, delete the last character.
			if (userInputForIP.length() != 0) {
				userInputForIP.deleteCharAt(userInputForIP.length() - 1);
			}
		} else if (key == 2 || key == 3 || key == 4 || key == 5 || key == 6 || key == 7 || key == 8 || key == 9 || key == 10 || key == 11 || key == 52) {
			// If key is a number or '.' then include it.
			userInputForIP.append(c);
		}
		System.out.println("Key: " + key + " Char: " + c + " StringBuffer: " + userInputForIP);
	}

	public void keyReleased(int key, char c) {
	}

	public void inputEnded() {
	}

	public void inputStarted() {
	}

	public boolean isAcceptingInput() {
		return true;
	}

	public void setInput(Input arg0) {
	}
}

Slick has a basic TextField UI. See the tests package for examples on usage.

For something more powerful/complex, use TWL.