Thread: Using Arrow Keys in Windows

  1. #1
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039

    Using Arrow Keys in Windows

    First let me tell you what I'm trying to accomplish. I am making a windows app where the user hits the up arrow key and somthing happens...obviously I am starting to lean towards a windows game. Anyway, here is my WM_MOVE message, I think this is where my action is:

    Code:
    case WM_MOVE:
             {
             // extract the position
             int xpos = LOWORD(lparam);
             int ypos = HIWORD(lparam);
    
             // get a graphics context
             hdc = GetDC(hwnd);
    
             // set the foreground color to green
             SetTextColor(hdc, RGB(0,255,0));
             
             // set the background color to black
             SetBkColor(hdc, RGB(0,0,0));
             
             // set the transparency mode to OPAQUE
             SetBkMode(hdc, OPAQUE);
    
             // get the up arrow key from the user
             char key=getch();
    		 if(key == VK_UP){
             TextOut(hdc, 0,0, "Hi", strlen("Hi"));
    		 }
             // release the dc back
             ReleaseDC(hwnd, hdc);
    
             } break;
    if I get rid of 'if key == VK_UP' stuff then the Textout thing works. But with it, I press the up arrow key and it doesn't work. Could someone please tell me why? thanks.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    look for the WM_KEYDOWN message

    like so...

    case WM_KEYDOWN:
    // the key pressed is stored in wParam
    // do something or set a flag for the key(i recommend the flag and doing the actions elsewhere)

    case WM_KEYUP:
    // the key pressed is stored in wParam
    // reset the flag, or finish what your doing...

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    thanks
    Last edited by Xterria; 10-21-2001 at 02:36 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You might also try DirectInput for keyboard handling -- it allows you to capture keystrokes bypassing windows messaging entirely. It would allow you to directly read the state of any key, with less hassle.

  5. #5
    bobish
    Guest
    you can also put this a the top of you program

    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

    and then have statements like this
    if (KEY_DOWN(VK_SPACE))
    in your main event loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. Arrow Keys
    By drdroid33 in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2002, 07:07 PM
  5. Arrow keys in console?
    By SyntaxBubble in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 06:12 PM