Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class GameInput
- Object
- 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 theGameViewat the end of every frame, afterGameView#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.
Methods
public boolean isGamepadButtonDown(int button) | Returns true while the button is held on any connected controller. |
public boolean wasGamepadButtonPressed(int button) | Returns true during the first frame in which the button is held on any connected controller. |
public boolean wasGamepadButtonReleased(int button) | Returns true during the first frame after the button was released on every connected controller. |
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, or else the first controller’s left stick. |
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. |
Inherited methods
Method details
isGamepadButtonDown
public boolean isGamepadButtonDown(int button)Parameters
buttonint- one of the
Gamepad.BUTTON_*constants
wasGamepadButtonPressed
public boolean wasGamepadButtonPressed(int button)wasGamepadButtonReleased
public boolean wasGamepadButtonReleased(int button)isKeyDown
public boolean isKeyDown(int keyCode)Parameters
keyCodeint- a Codename One key code as delivered to
com.codename1.ui.Component#keyPressed(int)
wasKeyPressed
public boolean wasKeyPressed(int keyCode)wasKeyReleased
public boolean wasKeyReleased(int keyCode)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
gameActionint- one of the
GAME_*constants oncom.codename1.ui.Display
getPointerX
public int getPointerX()GameView’s top left.getPointerY
public int getPointerY()GameView’s top left.isPointerDown
public boolean isPointerDown()wasPointerPressed
public boolean wasPointerPressed()wasPointerReleased
public boolean wasPointerReleased()getAxisX
public float getAxisX()VirtualJoystick, or else the first controller’s left stick. 0 when
neither is deflected.getAxisY
public float getAxisY()VirtualJoystick, or else the first controller’s left stick. 0 when
neither is deflected.