keyboard - oxygine/oxygine-framework GitHub Wiki
Keyboard
Oxygine does not have its own API for working with keys. If you need access to the keyboard, then you must use functions from SDL2 or Marmalade.
There exist multiple ways of dealing with keystrokes in SDL:
- Event-based
- Loop-based
Event-based
When the user presses any key, then the event SDL_KEYDOWN and SDL_KEYUP is fired.
Example:
#include "oxygine-framework.h"
void example_init()
{
ox::core::getDispatcher()->addEventListener(ox::core::EVENT_SYSTEM, onEvent);
}
void onEvent(Event* ev)
{
SDL_Event *event = (SDL_Event*)ev->userData;
if (event->type != SDL_KEYDOWN)
return;
//all key codes could be found in "SDL_keyboard.h" from SDL
switch (event->key.keysym.sym)
{
case SDLK_RETURN:
//ENTER key was pressed, handle it
break;
case SDLK_SPACE:
//SPACE key was pressed, handle it
break;
}
}
with the help of core::EVENT_SYSTEM, you may catch all SDL events.
Loop Based (Key Scanning)
Scans the status of specific keys. This method is usually used in a cycle for smooth and accurate control of an object's movement.
In the following example, the overloaded method Actor::doUpdate is called every frame and requests the status of the keys A, S, D, W. Then, the sprite's position on the screen changes accordingly:
#include "oxygine-framework.h"
class MySprite: public Sprite
{
protected:
void doUpdate(const UpdateState &us);
};
void MySprite::doUpdate(const UpdateState &us)
{
//calculate speed using delta time
float speed = 1.0f * (us.dt / 1000.0f);
Vector2 pos = getPosition();
if (key::isPressed(SDL_SCANCODE_A)) pos.x -= speed;
if (key::isPressed(SDL_SCANCODE_D)) pos.x += speed;
if (key::isPressed(SDL_SCANCODE_W)) pos.y -= speed;
if (key::isPressed(SDL_SCANCODE_S)) pos.y += speed;
setPosition(pos);
}
Was Pressed/Released
These functions from ox::key namespace tell you whether a key was pressed or not (scans every frame):
bool wasPressed(keycode);
bool wasReleased(keycode);
before using them, you must initialize the ox::key subsystem
ox::key::init();
usage:
if (ox::key::wasPressed(SDL_SCANCODE_SPACE))
jump();