check for keypresses in WM_KEYDOWN

This is a discussion on check for keypresses in WM_KEYDOWN within the Windows Programming forums, part of the Platform Specific Boards category; is this a good way to do this? Code: case WM_KEYDOWN: { int KEY_LEFT = GetKeyState(VK_LEFT); if(LOWORD(KEY_LEFT)) { MessageBox(hWnd, "Pressed", ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    check for keypresses in WM_KEYDOWN

    is this a good way to do this?
    Code:
    case WM_KEYDOWN:
    		{
    		    int KEY_LEFT = GetKeyState(VK_LEFT);
    			if(LOWORD(KEY_LEFT))
    			{
    				MessageBox(hWnd, "Pressed", "Pressed", MB_OK);
    			}
    		}

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    I don't know. Did it work



    ITSA
    Socket Library!

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    try this:
    Code:
    bool bKeys[256];
    
    ....
    case WM_KEYDOWN:
    {     bKeys[wParam] = true;
           return(0);
    }
    case WM_KEYUP:
    {     bKeys[wParam] = false;
           return(0);
    }
    ....

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Well, here is what I have which works GOOD except 1 thing. If you are holding down a direction, then hold down another (to go diagonal for example), there is a slight pause before you start to go in the diagonal direction.
    Code:
    case WM_KEYDOWN:
    		{
    			RECT rcClient;
    			HDC hDC = GetDC(hWnd);
    		    GetClientRect(hWnd, &rcClient);
    
    		    switch((int)wParam)
    			{
                case VK_LEFT:
    				KEY[LEFT] = 1;
    				break;
                case VK_RIGHT:
    				KEY[RIGHT] = 1;
    				break;
    			case VK_UP:
    				KEY[UP] = 1;
    				break;
    			case VK_DOWN:
    				KEY[DOWN] = 1;
    				break;
    			case VK_ESCAPE:
    				PostQuitMessage(0);
    				break;
    			}
    
    			UpdateShip(&rcClient, KEY);
    			DrawShip(hDC, &rcClient);
    		}
    		break;
    	case WM_KEYUP:
    		{
    		    switch((int)wParam)
    			{
                case VK_LEFT:
    				KEY[LEFT] = 0;
    				break;
                case VK_RIGHT:
    				KEY[RIGHT] = 0;
    				break;
    			case VK_UP:
    				KEY[UP] = 0;
    				break;
    			case VK_DOWN:
    				KEY[DOWN] = 0;
    				break;
    			}
    		}
            break;

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Oh, and is it good practice to draw and update the player ship in the WM_KEYDOWN case? well, not is it GOOD practice, is it BAD practice?

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    The only thing you should do in the case of a key press is just change its state. Also I would update anything like movement or position here. Drawing should be done independently of input, so you should be drawing in your main loop.

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Main loop meaning? WM_TIMER? Msg loop?

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Originally posted by Leeman_s
    Main loop meaning? WM_TIMER? Msg loop?
    no, by main loop I mean in your message dispatch loop in WinMain, unless your updating based on time, but i would use bool for and set it to true when its time to update, false after updating (reset to true when timer goes off again......)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

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