This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Input
Vrekt edited this page Jul 19, 2018
·
1 revision
The InputAdapter
handles mouse and keyboard listeners. Let's start with getting keyboard input.
DefaultKeyListener listener = myGameManagerInstance.getInputAdapter().getKeyboard();
Now that you have the keyboard listener instance you can check which keys are down and for how long.
boolean keyPressedW = listener.isKeyDown(KeyEvent.VK_W);
To check how long a key has been down: PS. This value is in milliseconds.
long timeW = listener.keyDownDuration(KeyEvent.VK_W);
To get an instance of the mouse listener:
DefaultMouseListener listener = myGameManagerInstance.getInputAdapter().getMouse();
The mouse listener contains these methods.
Point getLastClick()
, boolean isMouseDown()
, Component getEnteredComponent()
.
getEnteredComponent()
WILL return null if no component was entered.
Want to register a custom listener or adapter? The InputAdapter contains methods for just this.
To register a custom keyboard listener:
inputAdapter.addKeyListener(new MyKeyListener());
Removing adapters or listeners:
inputAdapter.removeKeyListener(keyListenerInstance);
inputAdapter.removeMouseListener(mouseListenerInstance);
inputAdapter.removeMouseAdapter(mouseAdapterInstance);
To register a custom mouse listener:
inputAdapter.addMouseListener(new MyMouseListener());
You can also register custom adapters.
inputAdapter.addMouseAdapter(new MyMouseAdapter());