So I am making a game in java, and I am currently working with keyboard input. I have a keylistener class setup called KeyboardManager
and in it a static function that detects if a key is being held down. here is the code for that class:
public class KeyboardManager implements KeyListener {
public static Map<Integer, Boolean> keys;
public KeyboardManager () {
keys = new HashMap<>();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
keys.put(e.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent e) {
keys.put(e.getKeyCode(), false);
}
public static boolean isKeyDown(int key) {
if (!keys.containsKey(key)) return false;
return keys.get(key);
}
}
The isKeyDown
function works perfectly fine, and I use it for player movement, but if I use it for opening a gui, like a player inventory, as you would imagine, it opens and closes every tick. I am looking for a way to do what I have now, but with keyTyped
instead of keyPressed
and keyReleased
. The challenge here, obviously is that there is no way to tell when the keytype ends. For example, in Unity, when you are coding in c# you can do something like Input.GetKeyDown("E")
and it would test for a keytype, whereas Input.GetKey("E")
would detect if it is being held down. Any help would be appreciated.