Class ScriptInput
public final class ScriptInput
Convenient, managed input helper for scripts.
All listeners registered through this class are automatically cleaned up when the owning
ScriptContext is detached or reloaded.
-
Method Summary
Modifier and TypeMethodDescriptionBinds an action to be executed when the specified key is pressed.Binds actions to be executed when the specified key is pressed and released.Binds a consumer to receive key press events for the specified key code.bindKeyTyped(int keyCode, Consumer<KeyEvent> onTyped) Binds a consumer to receive key typed events for the specified key code.Binds an action to be executed when the specified mouse button is pressed (e.g.Binds actions for when the specified mouse button is pressed and released.booleanChecks whether the left mouse button is currently pressed.booleanChecks whether any mouse button is currently pressed.booleanisPressed(int keyCode) Checks whether the key with the specifiedkeyCodeis currently pressed.booleanChecks whether the key with the specified name (e.g., "SPACE", "W", "ENTER", "ESCAPE") is currently pressed.booleanChecks whether the right mouse button is currently pressed.Gets the current mouse screen location relative to the game window.Gets the map tile coordinate currently under the mouse.Gets the current mouse world/map location translated via camera.static intresolveKeyCode(String name) Resolves a key name like "SPACE", "ESCAPE", "W" to aKeyEventVK_ constant code.booleanwasReleased(int keyCode) Checks whether the key with the specifiedkeyCodewas recently released.
-
Method Details
-
isPressed
public boolean isPressed(int keyCode) Checks whether the key with the specifiedkeyCodeis currently pressed.- Parameters:
keyCode- An AWT virtual key code.- Returns:
truewhile the key is pressed.
-
isPressed
-
wasReleased
public boolean wasReleased(int keyCode) Checks whether the key with the specifiedkeyCodewas recently released.- Parameters:
keyCode- An AWT virtual key code.- Returns:
trueif the key was recently released.
-
bindKey
Binds an action to be executed when the specified key is pressed.- Parameters:
keyCode- An AWT virtual key code.onPress- The action to invoke.- Returns:
- A subscription that removes the listener.
-
bindKey
Binds actions to be executed when the specified key is pressed and released.- Parameters:
keyCode- An AWT virtual key code.onPress- The press action, ornullfor none.onRelease- The release action, ornullfor none.- Returns:
- A subscription that removes both listeners.
-
bindKey
Binds a consumer to receive key press events for the specified key code.- Parameters:
keyCode- An AWT virtual key code.onPress- The event consumer.- Returns:
- A subscription that removes the listener.
-
bindKeyTyped
Binds a consumer to receive key typed events for the specified key code.- Parameters:
keyCode- An AWT virtual key code.onTyped- The event consumer.- Returns:
- A subscription that removes the listener.
-
isMouseButtonPressed
public boolean isMouseButtonPressed()Checks whether any mouse button is currently pressed.- Returns:
truewhile a mouse button is pressed.
-
isLeftMouseButtonPressed
public boolean isLeftMouseButtonPressed()Checks whether the left mouse button is currently pressed.- Returns:
truewhile the left button is pressed.
-
isRightMouseButtonPressed
public boolean isRightMouseButtonPressed()Checks whether the right mouse button is currently pressed.- Returns:
truewhile the right button is pressed.
-
mouseLocation
Gets the current mouse screen location relative to the game window.- Returns:
- The screen location, or
(0,0)when no mouse is available.
-
mouseWorldLocation
Gets the current mouse world/map location translated via camera.- Returns:
- The map location, or
(0,0)when no mouse is available.
-
mouseTile
Gets the map tile coordinate currently under the mouse.- Returns:
- The tile coordinate, or
(0,0)when no mouse is available.
-
bindMouse
Binds an action to be executed when the specified mouse button is pressed (e.g.MouseEvent.BUTTON1).- Parameters:
button- An AWT mouse button identifier.onPress- The action to invoke.- Returns:
- A subscription that removes the listener.
-
bindMouse
Binds actions for when the specified mouse button is pressed and released.- Parameters:
button- An AWT mouse button identifier.onPress- The press action, ornullfor none.onRelease- The release action, ornullfor none.- Returns:
- A subscription that removes both listeners.
-
resolveKeyCode
Resolves a key name like "SPACE", "ESCAPE", "W" to aKeyEventVK_ constant code.- Parameters:
name- The case-insensitive key name.- Returns:
- The virtual key code, or
KeyEvent.VK_UNDEFINEDif it cannot be resolved.
-