Class GameInput

java.lang.Object
com.codename1.gaming.GameInput

public class GameInput extends Object

Pollable snapshot of keyboard and pointer state for a GameView.

Games generally prefer to poll the current input state from inside their update loop ("is the left key down right now?") rather than reacting to event callbacks. GameInput collects the raw Codename One key and pointer events delivered to its owning GameView and exposes them as simple queries.

Two kinds of query are available:

  • Level state -- #isKeyDown(int), #isPointerDown() -- true for as long as the key/pointer is held.

  • Edge state -- #wasKeyPressed(int), #wasKeyReleased(int), #wasPointerPressed(), #wasPointerReleased() -- true only during the single frame in which the transition happened. Edges are cleared by the GameView at the end of every frame, after GameView#update(double) has run.

Game controllers (see Gamepads) are folded in: #isGamepadButtonDown(int) and #wasGamepadButtonPressed(int) answer for any connected controller, and #getAxisX() / #getAxisY() fall back to the first controller's left stick when no on-screen joystick is held.

All state is written and read on the Codename One EDT, so no synchronization is required.

  • Method Summary

    Modifier and Type
    Method
    Description
    float
    The horizontal analog value, -1 (left) to 1 (right), from an on-screen VirtualJoystick, or else the first controller's left stick.
    float
    The vertical analog value, -1 (up) to 1 (down the screen), from an on-screen VirtualJoystick, or else the first controller's left stick.
    int
    The last known pointer x position, relative to the GameView's top left.
    int
    The last known pointer y position, relative to the GameView's top left.
    boolean
    isGameKeyDown(int gameAction)
    Returns true while any currently held key maps to the given game action.
    boolean
    isGamepadButtonDown(int button)
    Returns true while the button is held on any connected controller.
    boolean
    isKeyDown(int keyCode)
    Returns true while the given raw key code is held down.
    boolean
    Returns true while the pointer (finger / mouse button) is held down.
    boolean
    Returns true during the first frame in which the button is held on any connected controller.
    boolean
    Returns true during the first frame after the button was released on every connected controller.
    boolean
    wasKeyPressed(int keyCode)
    Returns true during the single frame in which the given key went down.
    boolean
    wasKeyReleased(int keyCode)
    Returns true during the single frame in which the given key was released.
    boolean
    Returns true during the single frame in which the pointer went down.
    boolean
    Returns true during the single frame in which the pointer was released.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isGamepadButtonDown

      public boolean isGamepadButtonDown(int button)

      Returns true while the button is held on any connected controller.

      Parameters
      • button: one of the Gamepad.BUTTON_* constants
    • wasGamepadButtonPressed

      public boolean wasGamepadButtonPressed(int button)
      Returns true during the first frame in which the button is held on any connected controller.
    • wasGamepadButtonReleased

      public boolean wasGamepadButtonReleased(int button)
      Returns true during the first frame after the button was released on every connected controller.
    • isKeyDown

      public boolean isKeyDown(int keyCode)

      Returns true while the given raw key code is held down.

      Parameters
      • keyCode: a Codename One key code as delivered to com.codename1.ui.Component#keyPressed(int)
    • wasKeyPressed

      public boolean wasKeyPressed(int keyCode)
      Returns true during the single frame in which the given key went down.
    • wasKeyReleased

      public boolean wasKeyReleased(int keyCode)
      Returns true during the single frame in which the given key was released.
    • isGameKeyDown

      public boolean isGameKeyDown(int gameAction)

      Returns true while any currently held key maps to the given game action.

      Game actions abstract over device specific key codes for the directional pad and fire button. See com.codename1.ui.Display#GAME_UP, com.codename1.ui.Display#GAME_DOWN, com.codename1.ui.Display#GAME_LEFT, com.codename1.ui.Display#GAME_RIGHT and com.codename1.ui.Display#GAME_FIRE.

      Parameters
      • gameAction: one of the GAME_* constants on com.codename1.ui.Display
    • getPointerX

      public int getPointerX()
      The last known pointer x position, relative to the GameView's top left.
    • getPointerY

      public int getPointerY()
      The last known pointer y position, relative to the GameView's top left.
    • isPointerDown

      public boolean isPointerDown()
      Returns true while the pointer (finger / mouse button) is held down.
    • wasPointerPressed

      public boolean wasPointerPressed()
      Returns true during the single frame in which the pointer went down.
    • wasPointerReleased

      public boolean wasPointerReleased()
      Returns true during the single frame in which the pointer was released.
    • getAxisX

      public float getAxisX()
      The horizontal analog value, -1 (left) to 1 (right), from an on-screen VirtualJoystick, or else the first controller's left stick. 0 when neither is deflected.
    • getAxisY

      public float getAxisY()
      The vertical analog value, -1 (up) to 1 (down the screen), from an on-screen VirtualJoystick, or else the first controller's left stick. 0 when neither is deflected.