Thread: register 2 simultaneous keypress'

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    register 2 simultaneous keypress'

    I have written a program in opengl using c++ and i want to be able to fly along my scene by using the keyboard so far i have managed to do this using various functions however i would like to be able to push two buttons at once so i can move in two seperate directions at the same time. Currently i use a switch statement that listens for key strokes any ideas on how i can make it listen for two?

    Code:
    void keyboard(unsigned char key, int x, int y)
    {
      char c='#';
      
      switch(key)
        {
    		case('w'):
    			moveFlat(1);
    			break;
    		case('s'):
    			moveFlat(-1);
    			break;
    		case('q'):
    			moveUp(1);
    			break;
        }
      glutPostRedisplay();
    }
    This is the code i use to listen for key strokes. Many Thanks

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well obviously a switch won't work since it cannot account for two keys at the same time.

    Code:
    ...
    if (KeyDown(VK_W))
    {
         Player->Walk(FORWARD);
    }
    
    if (KeyDown(VK_S))
    {
        Player->Walk(BACKWARD);
    }
    ...

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    Ive found that:

    Code:
    if (KeyDown(VK_W)&&KeyDown(VK_D)
    {
         movement=NE;
    }
    works just fine for me

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might also try something like:

    Code:
    int x_inc = 0, y_inc = 0;
    if (KEY_LEFT) x_inc -= 1;
    if (KEY_RIGHT) x_inc += 1;
    if (KEY_UP) y_inc -= 1;
    if (KEY_DOWN) y_inc += 1;
    move(position, x_inc, y_inc);
    I think breaking movement down into the x and y component is a bit more flexible that having separate functions for horizontal and vertical (and possibly diagonal) movement or having to deal with otherwise rather unhelpful enumerations of directions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    I get.....

    Code:
    C:\Users\J\Desktop\openGL\OpenGame1\Main.cpp(452) : error C2065: 'KeyDown' : undeclared identifier
    C:\Users\J\Desktop\openGL\OpenGame1\Main.cpp(452) : error C2065: 'VK_W' : undeclared identifier
    C:\Users\J\Desktop\openGL\OpenGame1\Main.cpp(457) : error C2065: 'VK_A' : undeclared identifier
    am i missing an include or do i have to define them somewhere?

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    Well those examples were just for reference, it depends on what platform your programming for:

    GLUT, WIN32 API, SDL, wxWidgets etc...

    You can look up the keyboard commands for whatever platform your using, or wrapper...

    However unfortunately I'm not much of a help in that area

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The examples I gave were simply that - examples. VK_W and VK_S are defined in Windows as virtual key codes for W and S. DirectInput uses these but remaps the constants to a different name.

    The example was to show that a series of if statements will handle multiple key presses - provided your keyboard code correctly sets and tracks the state of the keyboard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. register variables
    By BEN10 in forum C Programming
    Replies: 9
    Last Post: 04-17-2009, 07:20 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. register file
    By axon in forum Tech Board
    Replies: 0
    Last Post: 11-20-2003, 09:07 AM
  4. difference between register int and normal int
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-25-2003, 04:01 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM