public class Input implements KeyListener, MouseMotionListener, MouseListener {
private HashMap<Integer, Inputs> inputs = new HashMap<Integer, Inputs>();
public int mouseX, mouseY;
public void update() {
for (Map.Entry<Integer, Inputs> entry : inputs.entrySet()){
entry.getValue().update();
}
}
public boolean isDown(int keyCode) {
Inputs input = inputs.get(keyCode);
if(input != null){
return input.isDown;
}
else return false;
}
public boolean isClicked(int keyCode) {
Inputs input = inputs.get(keyCode);
if(input != null){
return input.isClicked;
}
else return false;
}
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(!inputs.containsKey(keyCode)){
Inputs input = new Inputs();
input.toggle(true);
inputs.put(keyCode, input);
}
else inputs.get(keyCode).toggle(true);
}
public void keyReleased(KeyEvent ke) {
inputs.get(ke.getKeyCode()).toggle(false);
}
public void mousePressed(MouseEvent mo) {
int mouseCode = mo.getButton();
if(!inputs.containsKey(mouseCode)){
Inputs input = new Inputs();
input.toggle(true);
inputs.put(mouseCode, input);
}
else inputs.get(mouseCode).toggle(true);
}
public void mouseReleased(MouseEvent mo) {
inputs.get(mo.getButton()).toggle(false);
}
public void mouseMoved(MouseEvent arg0) {
mouseX = arg0.getX() / Engine.SCALE;
mouseY = arg0.getY() / Engine.SCALE;
}
public void keyTyped(KeyEvent ke) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseDragged(MouseEvent arg0) {
}
public class Inputs {
private int presses, absorbs;
public boolean isDown, isClicked;
public void toggle(boolean pressed) {
if(pressed != isDown) isDown = pressed;
if(pressed) presses++;
}
public void update() {
if(absorbs < presses){
absorbs++;
isClicked = true;
}
else isClicked = false;
}
}
}
So I’ve experimented in different implementations of getting input from the user. This current implementation allows me to poll for constant input and also achieve a psuedo event system with the isClicked method. Also, with the addition of a hash map, I can check for any java KeyEvent or MouseEvent very quickly. Is there anything else that can be improved? I’ve been through many implementations, adding on what I learn over time, and I’ll be glad to take any advice.