Thread: Window message loop (Keys)

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Window message loop (Keys)

    Hey, I'm trying to get input for my program from the keyboard. I'm used to using GetAsyncKeyState(//) which seems like a good way to do things. But, I want my program to detect for pressed keys outside of a timer, preferably using the message loop.

    I've been looking at:
    Code:
    //ON_WM_KEYDOWN( )
    afx_msg void OnKeyDown(
       UINT nChar,
       UINT nRepCnt,
       UINT nFlags 
    );
    And

    Code:
    //ON_WM_CHAR( )
    afx_msg void OnChar(
       UINT nChar,
       UINT nRepCnt,
       UINT nFlags 
    );
    Msdn states I should call OnChar after OnKeyDown function to get the pressed character. But I dont get how this works. Do I add WM_KEYDOWN to my messaging loop, call OnKeyDown then OnChar? Also, how do I fill out nFlags?

    [quote]

    **OnKeyDown**
    nChar
    Specifies the virtual key code of the given key. For a list of of standard virtual key codes, see Winuser.h
    nRepCnt
    Repeat count (the number of times the keystroke is repeated as a result of the user holding down the key).
    nFlags
    Specifies the scan code, key-transition code, previous key state, and context code, as shown in the following list: Value Description
    0–7 Scan code (OEM-dependent value).
    8 Extended key, such as a function key or a key on the numeric keypad (1 if it is an extended key).
    9–10 Not used.
    11–12 Used internally by Windows.
    13 Context code (1 if the ALT key is held down while the key is pressed; otherwise 0).
    14 Previous key state (1 if the key is down before the call, 0 if the key is up).
    15 Transition state (1 if the key is being released, 0 if the key is being pressed).

    For a WM_KEYDOWN message, the key-transition bit (bit 15) is 0 and the context-code bit (bit 13) is 0.
    "For a WM_KEYDOWN message..." (Shown in the bottom of the above quote) I dont get how you'd set bit 15 to 0. (Scan code | 15(0) | 13(0))... I know thats completely off though >.>.'

    **OnChar**
    nChar
    Contains the character code value of the key.
    nRepCnt
    Contains the repeat count, the number of times the keystroke is repeated when user holds down the key.
    nFlags
    Contains the scan code, key-transition code, previous key state, and context code, as shown in the following list: Value Meaning
    0-15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user holding down the key.
    16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM)
    24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
    25-28 Used internally by Windows.
    29 Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.
    30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
    31 Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.
    Once again, I'm lost as to what to set the parameters of this.

    And one last thing, why call OnKeyDown then OnChar if you need to specify a certain key for OnKeyDown. It could just as easily return a boolean and there would be no need for OnChar. Yet, it returns a ?afx_msg? or does it return void? The function announcement is confusing.

    Thank you!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    This is a common way to do that:
    Code:
    bool KeysPressed[256] = {false};
    
    // ...
    case WM_KEYDOWN:
      KeysPressed[LOWORD(wprm)] = true;
      break;
    case WM_KEYUP:
      KeysPressed[LOWORD(wprm)] = false;
      break;
    // ...

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Mind iterating what wprm is ?

  4. #4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Window does not work.
    By RealityFusion in forum Windows Programming
    Replies: 6
    Last Post: 10-06-2005, 09:34 AM
  3. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  4. window message help!
    By jdude in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2004, 08:05 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM