解决方案 »

  1.   

    换成GetKeyState竟然取到了正确值,对比上面GetKeyboardState的那个错误值,顿时也是醉了。。GetKeyState:
    msdn:The key status returned from this function changes as a thread reads key messages from its message queue->reads key messages from its message queue  从自己的消息队列么? 那为啥 GetKeyState可以取到,GetKeyboardState取不到。。
      

  2.   

    GetKeyState
    The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off — alternating each time the key is pressed). SHORT GetKeyState(
      int nVirtKey   // virtual-key code
    );
     
    Parameters
    nVirtKey 
    Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code. 
    If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key. Return Values
    The return value specifies the status of the given virtual key, as follows: If the high-order bit is 1, the key is down; otherwise, it is up. 
    If the low-order bit is 1, the key is toggled. A key, such as the caps lock key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. 
    Res
    The key status returned from this function changes as a given thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information. An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated. To retrieve state information for all the virtual keys, use the GetKeyboardState function. An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the shift, ctrl, or alt keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys. VK_LSHIFT VK_RSHIFT 
    VK_LCONTROL VK_RCONTROL 
    VK_LMENU VK_RMENU 
    These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. Windows CE: The GetKeyState function can only be used to check the down state of the following virtual keys:VK_CONTROLVK_SHIFTVK_MENUVK_LCONTROLVK_LSHIFTVK_LMENUVK_RCONTROLVK_RSHIFTVK_RMENUGetKeyState can only be used to check the toggled state of the VK_CAPITAL virtual key. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.See Also
    Keyboard Input Overview, Keyboard Input Functions, GetAsyncKeyState, GetKeyboardState, MapVirtualKey, SetKeyboardState  
    GetKeyboardState
    The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer. BOOL GetKeyboardState(
      PBYTE lpKeyState   // pointer to array to receive status data
    );
     
    Parameters
    lpKeyState 
    Pointer to the 256-byte array that will receive the status data for each virtual key. 
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, callGetLastError. Res
    An application can call this function to retrieve the current status of all the virtual keys. The status changes as a thread removes keyboard messages from its message queue. The status does not change as keyboard messages are posted to the thread's message queue, nor does it change as keyboard messages are posted to or retrieved from message queues of other threads. (Exception: Threads that are connected viaAttachThreadInput share the same keyboard state.)When the function returns, each member of the array pointed to by the lpKeyState parameter contains status data for a virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the caps lock key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. To retrieve status information for an individual key, use the GetKeyState function. To retrieve the current state for an individual key regardless of whether the corresponding keyboard message has been retrieved from the message queue, use the GetAsyncKeyState functionAn application can use the virtual-key code constants VK_SHIFT, VK_CONTROL and VK_MENU as indices into the array pointed to by lpKeyState. This gives the status of the shift, ctrl, or alt keys without distinguishing between left and right. An application can also use the following virtual-key code constants as indices to distinguish between the left and right instances of those keys. VK_LSHIFT VK_RSHIFT 
    VK_LCONTROL VK_RCONTROL 
    VK_LMENU VK_RMENU 
    These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.See Also
    Keyboard Input Overview, Keyboard Input Functions, GetKeyState, GetAsyncKeyState, MapVirtualKey, SetKeyboardState  
      

  3.   

    GetAsyncKeyState
    The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. SHORT GetAsyncKeyState(
      int vKey   // virtual-key code
    );
     
    Parameters
    vKey 
    Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes. 
    Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Res section for further information. Return Values
    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus. Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero. Res
    The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON) 
     
    which returns TRUE if the mouse buttons have been swapped.You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right. Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys. Code Meaning 
    VK_LSHIFT VK_RSHIFT 
    VK_LCONTROL VK_RCONTROL 
    VK_LMENU VK_RMENU 
    These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. Windows CE: The GetAsyncKeyState function supports the left and right virtual key constants, so you can determine whether the left key or the right key was pressed. These constants are VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, VK_LSHIFT, and VK_RSHIFT.The least significant bit of the return value is not valid in Windows CE, and should be ignored.GetAsyncKeyState will return the current key state even if a window in another thread or process currently has the keyboard focus. You can also use the VK_LBUTTON virtual-key code constant to determine the state of the stylus tip, (up/down), on the touch-screen.QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.See Also
    Keyboard Input Overview, Keyboard Input Functions, GetKeyboardState, GetKeyState,GetSystemMetrics, MapVirtualKey, SetKeyboardState  
      

  4.   

    Key Status
    While processing a keyboard message, an application may need to determine the status of another key besides the one that generated the current message. For example, a word-processing application that allows the user to press shift+end to select a block of text must check the status of the shift key whenever it receives a keystroke message from the end key. The application can use the GetKeyState function to determine the status of a virtual key at the time the current message was generated; it can use the GetAsyncKeyState function to retrieve the current status of a virtual key. The keyboard layout maintains a list of names. The name of a key that produces a single character is the same as the character produced by the key. The name of a noncharacter key such as tab and enter is stored as a character string. An application can retrieve the name of any key from the device driver by calling the GetKeyNameText function.