A newb question about keyboard input

This is a discussion on A newb question about keyboard input within the Game Programming forums, part of the General Programming Boards category; I am making a 2d shooting game, and I want the user to be able to move the player with ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    126

    A newb question about keyboard input

    I am making a 2d shooting game, and I want the user to be able to move the player with the WSAD keys. The problem is that the user has to hold down the key for a second or two to get the player to start moving continuously. Is there a way to eliminate this problem without using DirectInput?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yep. You're using WM_CHAR or WM_KEYDOWN messages, right? Well, that's fine, but do this instead:
    Code:
    bool w,s,a,d;
    
    //stuff here
    
    case WM_KEYDOWN:
    switch(...)
    {
        case 'A':
            a = true;
    }
    break;
    
    case WM_KEYUP:
    switch(...)
    {
        case 'A':
            a = false;
    }
    Something like that anyways, and then in your code you do this:
    Code:
    int WINAPI WinMain(...)
    {
        (...)
        while(...)
        {
            //your message handling stuff
    
            if(a)
                --position.x;
        }
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Or you can just use "getch()" intead of whatever syntax you use for inputing the w, a, s, or d.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm assuming he's using Windows API. Getch() is console.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    Somewhere in the beginning:
    
    #define KEY_UP 0
    #define KEY_DOWN 1
    #define KEY_LEFT 2
    #define KEY_RIGHT 3
    
    bool KeyDown[4] = {false};
    Code:
    Message handler:
    
    case WM_KEYDOWN:
       switch(wParam)
       {
          case VK_UP:
             KeyDown[KEY_UP] = true;
             break;
    
          case VK_DOWN:
             KeyDown[KEY_DOWN] = true;
             break;
    
          case VK_LEFT:
             KeyDown[KEY_LEFT] = true;
             break;
    
          case VK_RIGHT:
             KeyDown[KEY_RIGHT] = true;
             break;
       }
       break;
    
    case WM_KEYUP:
       switch(wParam)
       {
          case VK_UP:
             KeyDown[KEY_UP] = false;
             break;
    
          case VK_DOWN:
             KeyDown[KEY_DOWN] = false;
             break;
    
          case VK_LEFT:
             KeyDown[KEY_LEFT] = false;
             break;
    
          case VK_RIGHT:
             KeyDown[KEY_RIGHT] = false;
             break;
       }
       break;
    Code:
    Somewhere in your game loop
    
    if(KeyDown[KEY_UP]) Player.Ypos--;
    if(KeyDown[KEY_DOWN]) Player.Ypos++;
    if(KeyDown[KEY_LEFT]) Player.Xpos--;
    if(KeyDown[KEY_RIGHT]) Player.Xpos++;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interacting with keyboard input.
    By Devils Child in forum C# Programming
    Replies: 4
    Last Post: 06-05-2009, 12:06 AM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  3. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  4. String input reliability question
    By WinterMute in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2007, 11:58 PM
  5. cin.getline question (user's input too long)
    By JerryL_MB in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2002, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21