查看文章 |
这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多,但就是没有很好解决问题。 之前找到一个能正确发送的code:(Alt+A) PostMessage(hWnd,WM_SYSKEYDOWN,VK_MENU,0); PostMessage(hWnd,WM_SYSKEYDOWN,0x41,0); Sleep(50); PostMessage(hWnd,WM_SYSKEYUP,0x41,0); PostMessage(hWnd,WM_SYSKEYUP,VK_MENU,0); 有人解释说,按下组合键的时候系统是发两条消息的 但是看到Win32 SDK,感觉上就发一次就可以了…… 偶然间又看到最后一个参数的说明,有所发现!先看WM_SYSKEYDOWN的help The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter. WM_SYSKEYDOWN Parameters nVirtKey Value of wParam. Specifies the virtual-key code of the key being pressed. lKeyData Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table: Value Description 之前曾经修改过keyData的16-23位为VK_MENU,第30位参数为1,但没效果 请看位29的说明!! The value is 1 if the ALT key is down while the key is pressed; 当值为1时表示ALT键被按下!这不正是我需要的吗?于是把29位设置为1,函数调用变成 PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1<<29); 经过测试,发现这个就是Alt+A的效果!!原来这么简单,但为什么很多人弄得那么复杂,我当时查找的时候也是迷惘啊,浪费了N多小时。 类似有个WM_SYSKEYUP,WM_SYSCHAR(这个不知道干什么用)
|