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.

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

  • Method Summary

    Modifier and Type
    Method
    Description
    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
    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
    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, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • 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.