public class GameInput

  1. Object
  2. GameInput

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.

Methods

public boolean isKeyDown(int keyCode)Returns true while the given raw key code is held down.
public boolean wasKeyPressed(int keyCode)Returns true during the single frame in which the given key went down.
public boolean wasKeyReleased(int keyCode)Returns true during the single frame in which the given key was released.
public boolean isGameKeyDown(int gameAction)Returns true while any currently held key maps to the given game action.
public int getPointerX()The last known pointer x position, relative to the GameView’s top left.
public int getPointerY()The last known pointer y position, relative to the GameView’s top left.
public boolean isPointerDown()Returns true while the pointer (finger / mouse button) is held down.
public boolean wasPointerPressed()Returns true during the single frame in which the pointer went down.
public boolean wasPointerReleased()Returns true during the single frame in which the pointer was released.
public float getAxisX()The horizontal analog value, -1 (left) to 1 (right), from an on-screen VirtualJoystick.
public float getAxisY()The vertical analog value, -1 (up) to 1 (down the screen), from an on-screen VirtualJoystick.

Inherited methods

Method details

isKeyDown

public boolean isKeyDown(int keyCode)
Returns true while the given raw key code is held down.

Parameters

keyCode int
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 int
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. 0 when there is no joystick or it is centered.

getAxisY

public float getAxisY()
The vertical analog value, -1 (up) to 1 (down the screen), from an on-screen VirtualJoystick. 0 when there is no joystick or it is centered.