-1

I am trying to make a graphical text box for my game. I need to trigger a function called addChar(char c) with c being the key pressed in character form.

1 Answers1

0

In your update method, one of the parameters that slick passes in when calling it is the GameContainer. Suppose the update function looks something like this:

public void update(GameContainer gc, int delta)  throws SlickException     
{
...
}

If you get the input from the gc, you can then ask the input if a specific key was pressed.

After that, you could get the actual representation of which key was pressed by using the getKeyName(int code) method of the input, on the int you just got. Finally, call the method you wanted with the c you got.

public void update(GameContainer gc, int delta)  throws SlickException     
{
Input input = gc.getInput();
//here you should create a collection of all the keys you're interested to check
Integer[] collection = {Input.KEY_C, Input.KEY_B, Input.KEY_A};
for (Integer key: collection)
{
  if (input.isKeyPressed(key))
  {
  char c = input.getKeyName(int code);
  addChar(char c);
  }
}

Notice that the getKeyName doesn't always return a char though - sometimes it's a string. (Enter for example will return "enter".)

You might want to add a break; in the loop, if you're only interested in one key. Also, a useful reference for all the possible keys and some useful methods is found here: http://slick.ninjacave.com/javadoc/

Eric
  • 522
  • 5
  • 9