(There might be a better way, but this is what i've come up with; This approach is inspired by Unity)
Ref link: https://github.com/gabrielevierti/Advent3D/blob/master/Core/Core/src/Logic/input.h
As you can see, i made an enum, called keycode, which lets me choose which key on the keyboard is pressed by the user. There is also a boolean array, which contains the state of each key. By creating a 'getKey(KeyCode)' type of function, we can get the keycode from the 'KeyCode' enum, and the corresponding boolean in the 'keys' boolean array will be set to it's state(either true -> pressed; false-> released). The same approach can be applied to Buttons, like mouse buttons or joystick buttons, joystick axis, and basically any type of input, VR too for example.
If we want to check if the X key was pressed, all we do is:
//the X key was pressed
I'm using GLFW as a base, and I'm deriving from it. This is not the smartest way to do it, since we are just "renaiming" GLFW values to ours, but hey it's still a valid option.
Map Based approach
As suggested to me in the past (from Tyyppi77), you could use a map based approach: you basically store a map of function callbacks, that get invoked when a specific key is pressed.
using Callback = std::function<void()>;
void addBinding(int key, const Callback& callback)
void onKeyPress(int key)
for (Callback& callback : m_Callbacks[key])
std::map<int, std::vector<Callback>> m_Callbacks;
This way you can add callbacks based on your needs, just by creating
std::functions. You could call
onkeypress(), specify the correct key, and the corresponding callback will get invoked.