Skip to content

Add a key callback

Romain Milbert edited this page Mar 19, 2022 · 7 revisions

You can easily add a key callback to your window to execute an action on a key input.

The method Window::addKeyCallback() requires to be given a key (available in Raz::Keyboard) and a function; said function must take a float as parameter, indicating the elapsed time from the last frame (commonly called "delta time"). The easiest solution is to pass a lambda, capturing a reference on whatever object you want to apply an action to.

Note that RaZ doesn't handle scan codes yet; the keys are based on the QWERTY layout. As such, with an AZERTY keyboard for example, you must indicate Raz::Keyboard::W if you want to use your Z key.

Raz::Transform& transform = ...;

// Pressing S will apply a translation along +Z
// Multiplicating by deltaTime allows the movement to appear the same,
//  whatever the refresh rate your application executes at
window.addKeyCallback(Raz::Keyboard::S, [&transform] (float deltaTime) {
    transform.translate(0.f, 0.f, 0.5f * deltaTime);
});

// Pressing S will apply a translation along +X
window.addKeyCallback(Raz::Keyboard::D, [&transform] (float deltaTime) {
    transform.translate(0.5f * deltaTime, 0.f, 0.f);
});

A frequency can also be specified when adding a key callback (ALWAYS by default):

  • Raz::Input::ONCE if the action should be executed a single time
  • Raz::Input::ALWAYS if the action should be executed as long as the key is pressed
// When X is pressed, the model's size will double
// This action should probably be executed only once
// The deltaTime isn't used here, since we want to scale a single time
window.addKeyCallback(Raz::Keyboard::X, [&transform] (float /* deltaTime */) {
    transform.scale(2.f);
}, Raz::Input::ONCE);

Raz::Entity& light = world.addEntityWithComponent<Raz::Light>(/* ... */);

// Pressing L will disable the light, making it ineffective
// Letting the default Input::ALWAYS here wouldn't be so bad, but is unnecessary
window.addKeyCallback(Raz::Keyboard::L, [&light] (float /* deltaTime */) {
    light.disable();
}, Raz::Input::ONCE);

Finally, another function can be passed, which will be executed when the key is released:

// In the previous example, our light could only be disabled, but not reenabled back
// Giving another function can fix this!
window.addKeyCallback(Raz::Keyboard::L,
                      [&light] (float /* deltaTime */) { light.disable(); },
                      Raz::Input::ONCE,
                      [&light] () { light.enable(); }); // Reenabled when L is released

// Let's say the window's cursor is disabled by default...
window.disableCursor();

// ... it can be reenabled by pressing the alt key, as long as it remains pressed
window.addKeyCallback(Raz::Keyboard::LEFT_ALT,
                      [&window] (float /* deltaTime */) { window.showCursor(); },
                      Raz::Input::ONCE,
                      [&window] () { window.disableCursor(); });
Clone this wiki locally