School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,481 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




OpenGL Key Handling

 

OpenGL Key Handling

Kanvus

21 Jun, 2009 - 06:00 PM
Post #1

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
Basic movement is choppy and slow with switch and stuff like that. Anyone know how to get the smooth movement like walking around in fps games? Searched DIC and google but running out of ideas for good keywords.

User is offlineProfile CardPM
+Quote Post


Tom9729

RE: OpenGL Key Handling

21 Jun, 2009 - 06:22 PM
Post #2

Debian ninja
Group Icon

Joined: 30 Dec, 2007
Posts: 2,129



Thanked: 53 times
Dream Kudos: 425
My Contributions
Make a table of key states (ie. which keys are up and which keys are down). Have your keyboard listener update the table when keys are pressed. You should check the table periodically to perform game logic (ex. "move left if A is pressed").

Edit: Here is my implementation of it (GLUT).

This post has been edited by Tom9729: 21 Jun, 2009 - 06:24 PM
User is offlineProfile CardPM
+Quote Post

bobjob

RE: OpenGL Key Handling

21 Jun, 2009 - 07:11 PM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2008
Posts: 115



Thanked: 8 times
My Contributions
what language are you programming in?
User is offlineProfile CardPM
+Quote Post

Kanvus

RE: OpenGL Key Handling

21 Jun, 2009 - 11:27 PM
Post #4

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
C++, bj. nice name

The idea is to create a table in memory that handles key presses rather than sending asynchronous presses one at a time to the api, right? Does the program scan the whole function the table is in every frame to see which key isn't in state of 1 or something similar? What is this concept called?
User is offlineProfile CardPM
+Quote Post

Tom9729

RE: OpenGL Key Handling

22 Jun, 2009 - 02:57 AM
Post #5

Debian ninja
Group Icon

Joined: 30 Dec, 2007
Posts: 2,129



Thanked: 53 times
Dream Kudos: 425
My Contributions
You should be able to make some constants or an enumeration from the key codes.

ex.
cpp

const int KEY_UPPER_A = 21;
const int KEY_UPPER_B = 22;
....


Then you could have a function like this that is called every time you render/draw.
cpp

void keyboardHandler(int *keyState)
{
// If upper case 'a' is down...
if (keyState[KEY_UPPER_A])
{
// Do stuff.
}

// If upper case 'b' is down...
if (keyState[KEY_UPPER_B])
{
// Do other stuff.
}
}


Edit: It would be helpful to know whether or not you are using GLUT. I'm not sure how other APIs (SDL, GLX, WGL, etc) handle keyboard input.

This post has been edited by Tom9729: 22 Jun, 2009 - 05:49 AM
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: OpenGL Key Handling

22 Jun, 2009 - 08:08 AM
Post #6

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

Yes can you give us more info on how you are implementing OpenGL?
User is online!Profile CardPM
+Quote Post

RudiVisser

RE: OpenGL Key Handling

22 Jun, 2009 - 09:15 AM
Post #7

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Take a look at the Q3 Source and see how they're handling it, they do it pretty quick indeed wink2.gif
User is offlineProfile CardPM
+Quote Post

Kanvus

RE: OpenGL Key Handling

22 Jun, 2009 - 01:12 PM
Post #8

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
Yeah it's just plain glut.h and nothing else. No winAPI or anything. Runs from console and portable.

If you create an enumeration or constants like above, and make A = 21, then how does the keyboard listener know that A is being pressed? Because A is 65 in ascii.

For that function,
CODE

void keyboardHandler(int *keyState)
{
    // If upper case 'a' is down...
    if (keyState[KEY_UPPER_A])
    {
        // Do stuff.
    }

    // If upper case 'b' is down...
    if (keyState[KEY_UPPER_B])
    {
        // Do other stuff.
    }
}

Would the keyboard listener in main look like
CODE
glutKeyboardFunc(keyboardHandler);


I think I'm starting to get it.

This post has been edited by Kanvus: 22 Jun, 2009 - 01:13 PM
User is offlineProfile CardPM
+Quote Post

Tom9729

RE: OpenGL Key Handling

22 Jun, 2009 - 01:34 PM
Post #9

Debian ninja
Group Icon

Joined: 30 Dec, 2007
Posts: 2,129



Thanked: 53 times
Dream Kudos: 425
My Contributions
Sort of, you wouldn't want to call your keyboard handler with glutKeyboardFunc(..) though.

Here is a more complete example.

cpp

int keyState[119]; // 119 keys, 0 = up, 1 = down
...
const int KEY_UPPER_A = 21;
const int KEY_UPPER_B = 22;
...
void keyDown(unsigned char key, int x, int y)
{
switch (key)
{
case 65:
keyState[KEY_UPPER_A] = 1;
break;
case 66:
keyState[KEY_UPPER_B] = 1;
break;
...
}

void keyUp(unsigned char key, int x, int y)
{
switch (key)
{
case 65:
keyState[KEY_UPPER_A] = 0;
break;
case 66:
keyState[KEY_UPPER_B] = 0;
break;
...
}

void keyInit()
{
glutKeyboardFunc(keyDown);
glutKeyboardUpFunc(keyUp);
}

// Call me from within your game/render/logic loop.
void keyHandler()
{
if (keyState[KEY_UPPER_A])
{
// Do something.
}

if (keyState(KEY_UPPER_B)
{
// Do something else.
}
}


They keyUp/keyDown functions are callbacks, you register them with GLUT (ie. the code in keyInit(..)) and it calls them for your whenever a key is pressed or released.

You want to keep the code that updates the table (ie. which keys are currently pressed down) separate from the code that performs actions based upon which keys are pressed down.

On a side note, GLUT considers keys like the arrow keys to be "special" and you need to use the glutSpecialFunc(..)/glutSpecialUpFunc(..) functions to listen for them.
User is offlineProfile CardPM
+Quote Post

Kanvus

RE: OpenGL Key Handling

22 Jun, 2009 - 08:13 PM
Post #10

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 451



Thanked: 39 times
Dream Kudos: 50
My Contributions
OOOOH

wow cool. i feel smart (lol)

thanks so much tom

EDIT: the result is so beautiful and smooth i wanna cry T-T

This post has been edited by Kanvus: 22 Jun, 2009 - 08:32 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:59AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month