虚拟键盘的消息队列,进程间GetKeyboardState

yikoda 2014-10-15 02:48:15
在一个进程里改变键盘状态如大小写,在第二个进程里,第一次是取不到正确的大小写状态的。

查了好多资料,看到一句这个话 :GetKeyState returns the virtual key state. In other words, GetKeyState reports the state of the keyboard based on the messages you have retrieved from your input queue. This is not the same as the physical keyboard state:

If the user has typed ahead, GetKeyState doesn't report those changes until you use the PeekMessage function or the GetMessage function to retrieve the message from your input queue.


If the user has switched to another program, then the GetKeyState function will not see the input that the user typed into that other program, since that input was not sent to your input queue.

在两个进程中,虚拟键盘是两个消息队列,新的进程在消息队列没有过来之前,是收不到正确的键盘消息,,希望能给有大神指点,可以手动的在期望的进程里,让消息队列和虚拟键盘连接起来。。。表达的不太好。

...全文
222 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-10-15
  • 打赏
  • 举报
回复
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.
赵4老师 2014-10-15
  • 打赏
  • 举报
回复
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 Remarks 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. Remarks 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老师 2014-10-15
  • 打赏
  • 举报
回复
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. Remarks 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_CONTROL VK_SHIFT VK_MENU VK_LCONTROL VK_LSHIFT VK_LMENU VK_RCONTROL VK_RSHIFT VK_RMENU GetKeyState 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. Remarks 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 function An 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
yikoda 2014-10-15
  • 打赏
  • 举报
回复
换成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取不到。。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧