Too many ways to receive input

First, I can use the key callback method, which is very complicated and messy.
Then, there is using glfwGetKeyScancode,which easier.
Lastly,theres glfwGetKey,which for me is the easiest to use.
But since all method works,why is there so many??
Which one of them should i use???

There aren’t too many ways to receive input, as each method has it’s uses. The best way to understand this is to read the keyboard input documentation and start using whichever method seems the right choice for your needs.

With callbacks you are guaranteed to receive all keypresses. For example, during one event processing iteration (call to glfwPollEvents) user can press ‘G’ key, release it, press it again and release it again. Callback will be invoked four times with Press, Release, Press and Release arguments. In case you use glfwGetKey, you can only retrieve current state of key. So if you do it after glfwPollEvents you will see only that key is up, but you will not know it was pressed. Depending on functionality you want this is something you might want to detect or not. For example, if you want to throw granade on G key press then you want to know whether key was pressed or not. User can press key and release it so fast that current status will simply return that key is released - and you will miss keypress and user will be annoyed that game did not detect keypress.

But if you only want to know if key is down or up (like for arrow key up for moving forwards) - then you don’t care how many times key was pressed, so you can just ask for current status to know whether to move fowards or not and not use callback at all.

In general - callback is much safer way how to get information about keypresses (same with mouse button presses). Alternative is to enable GLFW_STICKY_KEYS which will keep information whether key was pressed at least once since last update. Only disadvantage is that you will not know how many times it is pressed during update. Depending on game style/functionality this is OK or not. For example, if you are writing Mortal Kombat style game where each keypress is important to execute some kind of action, you really want to detect each key down/up event instead of only is key down or up status.

As for difference between glfwGetKeyScancode vs glfwGetKey - glfwGetKey is for getting status of key - is it up or down. glfwGetKeyScancode is for getting scancode for specific key (NOT the status of key). You use scancode in key callback to compare which layout-specific key is pressed - for example, on german keyboard Z and Y keys are swapped compared to US layout.

2 Likes

Ok,thanks…
So basically the first and second are almost the same,but the first is more accurate and more broader,right.??