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 Details

    • isPressed

      public boolean isPressed(int keyCode)
      Checks whether the key with the specified keyCode is currently pressed.
      Parameters:
      keyCode - An AWT virtual key code.
      Returns:
      true while the key is pressed.
    • isPressed

      public boolean isPressed(String keyName)
      Checks whether the key with the specified name (e.g., "SPACE", "W", "ENTER", "ESCAPE") is currently pressed.
      Parameters:
      keyName - A case-insensitive KeyEvent VK_ field name, with or without the prefix.
      Returns:
      true while the resolved key is pressed.
    • wasReleased

      public boolean wasReleased(int keyCode)
      Checks whether the key with the specified keyCode was recently released.
      Parameters:
      keyCode - An AWT virtual key code.
      Returns:
      true if the key was recently released.
    • bindKey

      public Subscription bindKey(int keyCode, Runnable onPress)
      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

      public Subscription bindKey(int keyCode, Runnable onPress, Runnable onRelease)
      Binds actions to be executed when the specified key is pressed and released.
      Parameters:
      keyCode - An AWT virtual key code.
      onPress - The press action, or null for none.
      onRelease - The release action, or null for none.
      Returns:
      A subscription that removes both listeners.
    • bindKey

      public Subscription bindKey(int keyCode, Consumer<KeyEvent> onPress)
      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

      public Subscription bindKeyTyped(int keyCode, Consumer<KeyEvent> onTyped)
      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:
      true while a mouse button is pressed.
    • isLeftMouseButtonPressed

      public boolean isLeftMouseButtonPressed()
      Checks whether the left mouse button is currently pressed.
      Returns:
      true while the left button is pressed.
    • isRightMouseButtonPressed

      public boolean isRightMouseButtonPressed()
      Checks whether the right mouse button is currently pressed.
      Returns:
      true while the right button is pressed.
    • mouseLocation

      public Point2D 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

      public Point2D mouseWorldLocation()
      Gets the current mouse world/map location translated via camera.
      Returns:
      The map location, or (0,0) when no mouse is available.
    • mouseTile

      public Point mouseTile()
      Gets the map tile coordinate currently under the mouse.
      Returns:
      The tile coordinate, or (0,0) when no mouse is available.
    • bindMouse

      public Subscription bindMouse(int button, Runnable onPress)
      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

      public Subscription bindMouse(int button, Runnable onPress, Runnable onRelease)
      Binds actions for when the specified mouse button is pressed and released.
      Parameters:
      button - An AWT mouse button identifier.
      onPress - The press action, or null for none.
      onRelease - The release action, or null for none.
      Returns:
      A subscription that removes both listeners.
    • resolveKeyCode

      public static int resolveKeyCode(String name)
      Resolves a key name like "SPACE", "ESCAPE", "W" to a KeyEvent VK_ constant code.
      Parameters:
      name - The case-insensitive key name.
      Returns:
      The virtual key code, or KeyEvent.VK_UNDEFINED if it cannot be resolved.