Class 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.
All state is written and read on the Codename One EDT, so no synchronization is required.
-
Method Summary
Modifier and TypeMethodDescriptionintThe last known pointer x position, relative to theGameView's top left.intThe last known pointer y position, relative to theGameView's top left.booleanisGameKeyDown(int gameAction) Returns true while any currently held key maps to the given game action.booleanisKeyDown(int keyCode) Returns true while the given raw key code is held down.booleanReturns true while the pointer (finger / mouse button) is held down.booleanwasKeyPressed(int keyCode) Returns true during the single frame in which the given key went down.booleanwasKeyReleased(int keyCode) Returns true during the single frame in which the given key was released.booleanReturns true during the single frame in which the pointer went down.booleanReturns true during the single frame in which the pointer was released.
-
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 tocom.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_RIGHTandcom.codename1.ui.Display#GAME_FIRE.Parameters
gameAction: one of theGAME_*constants oncom.codename1.ui.Display
-
getPointerX
public int getPointerX()The last known pointer x position, relative to theGameView's top left. -
getPointerY
public int getPointerY()The last known pointer y position, relative to theGameView'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.
-