Hotkeys - GameDevWeek/CodeBase GitHub Wiki

There will be situations, where you want to change or call something in runtime. Hotkeys (Shortcuts) are a quick way of doing this.

Creating a Hotkey

import de.hochschuletrier.gdw.commons.gdx.input.hotkey.Hotkey;
import de.hochschuletrier.gdw.commons.gdx.input.hotkey.HotkeyModifier;
//...
boolean doRender = false;
private final Hotkey renderExtra = new Hotkey(() -> doRender = !doRender, Input.Keys.F1, HotkeyModifier.CTRL);

This creates the Hotkey CTRL + F1 to toggle the boolean variable doRender.

Please make sure your teammates are not using the hotkey already for something else!

If you wonder what the () -> is, then please look up Java Lambda Expressions

Registering the Hotkey

But before you can use the Hotkey, you need to register it. Don't forget to unregister it when you're done with it.

// On init:
togglePhysixDebug.register();
//...
// On shutdown
togglePhysixDebug.unregister();

Further examples

It's just as easy to just call a member method:

private final Hotkey increaseSpeed = new Hotkey(this::doIncreaseSpeed, Input.Keys.PAGE_UP, HotkeyModifier.CTRL);
private final Hotkey decreaseSpeed = new Hotkey(this::doDecreaseSpeed, Input.Keys.PAGE_DOWN, HotkeyModifier.CTRL);

private void doIncreaseSpeed() {/*...*/}
private void doDecreaseSpeed() {/*...*/}

Or to toggle a console variable:

private final CVarBool physixDebug = new CVarBool("physix_debug", true, 0, "Draw physix debug");
private final Hotkey togglePhysixDebug = new Hotkey(() -> physixDebug.toggle(false), Input.Keys.F1, HotkeyModifier.CTRL);

Make sure the Hotkey Manager is called!

The HotkeyManager must be somewhere in the input-processing-call-hierarchy. Usually this is already done in the Main project:

inputMultiplexer.addProcessor(HotkeyManager.getInputProcessor());