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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| private boolean isMyInputListenerCreated = false; private StringBuffer userInputForIP = new StringBuffer();
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { Input i = gc.getInput();
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 (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) { 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) { } } |