Skip to content

Instantly share code, notes, and snippets.

@Elix-x
Created November 1, 2015 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elix-x/78c2f4ee9533eb07328e to your computer and use it in GitHub Desktop.
Save Elix-x/78c2f4ee9533eb07328e to your computer and use it in GitHub Desktop.
Solving strange null pointers.
public class InputHandler {
public final KBOApi api;
private Map<Integer, Boolean> keyStatesMap = new HashMap<Integer, Boolean>();
public InputHandler(KBOApi api) {
this.api = api;
}
public void tick(){
for(int i = 0; i < Mouse.getButtonCount(); i++){
setMouseKeyPressed(i, Mouse.isButtonDown(i));
}
for(int i = 0; i < Keyboard.getKeyCount(); i++){
setKeyboardKeyPressed(i, Keyboard.isKeyDown(i));
}
}
public boolean isPressed(int key){
return keyStatesMap.get(key);
}
public void setPressed(int key, boolean pressed){
keyStatesMap.put(key, pressed);
}
public boolean isKeyboardKeyPressed(int key){
return isPressed(key);
}
public void setKeyboardKeyPressed(int key, boolean pressed){
setPressed(key, pressed);
}
public boolean isKeyboardKeyPressed(String key){
return isKeyboardKeyPressed(Keyboard.getKeyIndex(key));
}
public void setKeyboardKeyPressed(String key, boolean pressed){
setKeyboardKeyPressed(Keyboard.getKeyIndex(key), pressed);
}
public boolean isMouseKeyPressed(int key){
return isPressed(-100 + key);
}
public void setMouseKeyPressed(int key, boolean pressed){
setPressed(-100 + key, pressed);
}
public boolean isMouseKeyPressed(String key){
return isMouseKeyPressed(Mouse.getButtonIndex(key));
}
public void setMouseKeyPressed(String key, boolean pressed){
setMouseKeyPressed(Mouse.getButtonIndex(key), pressed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment