1
\$\begingroup\$

I am new to game development and am working on my first game using OpenGL and C++. I have a game class which handles everything necessary in an update() function. Now i want to handle keyboard inputs. I use GLFW which supports key callbacks. However i wonder how to deal with inputs. Should i record the keys pressed and poll on it the next time my game updates or should the callback immediately perform the necessary actions? And why?

Thanks :)

\$\endgroup\$
2
\$\begingroup\$

It's far easier to control how your game controller acts if you take any asynchronous key inputs and store them away until the update loop can get to them.

First reason is that the update function may change conditions before the key is handled. For example: I press fire. If handled immediately you would create a fire action, but in update handling it could be that you're declared dead and unable to fire.

Second reason is that let's say you quickly press two keys, go left and go right. If handled immediately that could fire off all sorts of secondary actions like animation changes, but if handled at update you can parse all the inputs and intelligently decide what the final outcome is and avoid lots of extra work.

Corollary to second reason is that combination keys are now easily handled, say Left + Up which would normally be handled by two separate callbacks for left and then up are now parsed by update as an entire action that means go diagonal.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

It depends. Personally I would look at key presses in a constant part of the loop instead of performing the action as soon as the key is pressed. But it may be beneficial to you to perform the actions as soon as the key press comes in. Is there anything that need immediate processing in your application?

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

As pointed out in the comments, I have discovered that this version only works if using LWJGL, the java port of OpenGL.

Use OpenGL's Keyboard class.

Please note that I use LWJGL so the syntax may be different from yours.

while (Keyboard.next()){
            if (Keyboard.isKeyDown(Keyboard.getEventKey())){
            String app = Keyboard.getKeyName(Keyboard.getEventKey());
            if (!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
                app = app.toLowerCase();
            }
            if (app.length() != 1){

            }else{
            //if (app != null && app != "" && app != " "){
                TextDemo.user.append(app);
                System.out.println(app);
            }
            }
            //}
        }

Keyboard.next() checks if a key has been pressed down or released. I check if the event key is down. The event key is the key that is involved in the press or release, and I want to see that it has been pressed, not released. The output is capitals, so if shift is down I keep it capitals, if it is not then I make it lowercase. I have yet to implement caps lock.

And the rest of the code is checking that it is not something like the spacebar or shift (More than 1 character), and if it is not, then append it to a string which I draw onto the screen in another piece of code that I have not shown above.

You can also use Keyboard.getEventCharacter(), however in my experience this returns a weird box character which can be annoying. Experiment with it :).

Hope this helped!

Edit: The above was mainly for typing strings. You can use the following code as well:

if (Keyboard.isKeyDown(Keyboard.KEY_YOURKEYHERE));
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE));
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Opengl does not have a keyboard class. This seems to be something that lwjgl implemented. \$\endgroup\$ – Eejin Jun 8 '14 at 18:43
  • \$\begingroup\$ @Eejin Dammit!! >:( \$\endgroup\$ – joehot200 Jun 8 '14 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy