some more按键是哪个呢

My approach to this problem turned out to be correct only in several programs. Why it isn't universal?
Works fine on:
Visual Studio Text Editor
Unfortunately, in some cases nothing happens(even if I click into a textbox area before execution of my program):
Google Chrome
GetLastError returns always 0, even using SendMessage instead of PostMessage.Could you point out my mistake?
#include &Windows.h&
#include &iostream&
int main()
HWND hCurrentW
Sleep(5000);
hCurrentWindow = GetForegroundWindow();
std::cout&&"GO!!!\n";
for(int i=0; i&500; i++) //simulate 500 keystrokes of 'E'.
PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL);
std::cout&&GetLastError()&&std::
system("Pause");
UPDATE after Maximus sugestion
#include &Windows.h&
#include &iostream&
int main()
HWND hCurrentW
Sleep(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow)
std::cout&&"Failed get set the window handle\n";
std::cout&&"GO!!!\n";
for(int i=0; i&500; i++)
PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45);
std::cout&&GetLastError()&&std::
system("Pause");
There is no difference in effect.
UPDATE after Rob Kennedy's comment and Hans Passant's answer
#include &Windows.h&
#include &iostream&
int main()
HWND hCurrentW
DWORD procID;
GUITHREADINFO currentWindowGuiThreadI
Sleep(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow)
std::cout&&"Failed get main the window handle\n";
GetWindowThreadProcessId(hCurrentWindow,&procID);
GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);
hCurrentWindow = currentWindowGuiThreadInfo.hwndF
if(!hCurrentWindow)
std::cout&&"Failed get the child window handle\n";
std::cout&&"GO!!!\n";
for(int i=0; i&500; i++)
PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
std::cout&&GetLastError()&&std::
system("Pause");
Now, "transparent" messages are sent every time. GetLastError() says:
ERROR_INVALID_WINDOW_HANDLE
Invalid window handle.
GetLastError() "fixed"
int main()
HWND hCurrentW
DWORD procID;
GUITHREADINFO currentWindowGuiThreadI
Sleep(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow)
std::cout&&"Failed get main the window handle\n";
GetWindowThreadProcessId(hCurrentWindow,&procID);
GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);
hCurrentWindow = currentWindowGuiThreadInfo.hwndF
if(!hCurrentWindow)
std::cout&&"Failed get the child window handle\n";
std::cout&&"GO!!!\n";
for(int i=0; i&500; i++)
if(!PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC))) std::cout&&GetLastError()&&std::
if(!PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC)))
std::cout&&GetLastError()&&std::
system("Pause");
...outputs 1400 thousand times. Except this, nothing has changed.
解决方案 This will of course happen when you post the message to the wrong window.
Which is certainly the case for Notepad.
It doesn't have just one window, something you can see with Spy++.
GetForegroundWindow() returns you a top-level window, the frame window for Notepad.
Inside of that frame window it has a child window, an EDIT control.
That window needs to get the messages.
You'll need to jump through a few hoops to get that window, the GetFocus() function returns the window with the focus but that only works if you call it from the process that owns the window.
When you do this out-of-process then you must first call GetWindowThreadProcessId() to get the ID of the thread that owns the foreground window.
Then you must call GetGUIThreadInfo(), the GUITHREADINFO.hwndFocus it returns is the window handle you need.
This is still not without trouble, you cannot control the keyboard state of the process.
In other words, the state of the Shift, Ctrl and Alt keys as well as any dead keys (like Alt+Gr on certain keyboard layouts).
Favor sending WM_CHAR for typing keys.
本文地址: &
我对这个问题的方法只有在几个程序才是正确的。为什么不是通用的?
Visual Studio文本编辑器
不幸的是,在某些情况下什么也没有发生(即使我在执行前点击文本框区域)
Google Chrome
GetLastError总是返回0,即使使用SendMessage,而不是PostMessage.Could你指出我的错误吗?
#include& Windows.h&
#include& iostream&
int main() { HWND hCurrentW
睡眠(5000);
hCurrentWindow = GetForegroundWindow();
std :: cout&&“GO !!! \\\”;
for(int i = 0; i <500; i ++)//模拟'E'的500次击键。
{ PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL); }
std :: cout&&& GetLastError()&& std ::
系统(“暂停”);
return 0; }
Maximus sugestion后更新 b
#include& Windows.h&
#include& iostream&
int main() { HWND hCurrentW
睡眠(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow) std :: cout&&“失败获取设置窗口句柄\\\”;
std :: cout&&“GO !!! \\\”;
for(int i = 0; i <500; i ++) { PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45); }
std :: cout&&& GetLastError()&& std ::
系统(“暂停”);
return 0; }
效果没有区别。
在Rob Kennedy的评论和Hans Passant的回答后更新
#include& Windows.h&
#include& iostream&
int main() { HWND hCurrentW
DWORD procID;
GUITHREADINFO currentWindowGuiThreadI
睡眠(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow) std :: cout&&“失败获取主窗口句柄\\\”;
GetWindowThreadProcessId(hCurrentWindow,& procID);
GetGUIThreadInfo(procID,& currentWindowGuiThreadInfo);
hCurrentWindow = currentWindowGuiThreadInfo.hwndF
if(!hCurrentWindow) std :: cout&&“失败获取子窗口句柄\\\”;
std :: cout&&“GO !!! \\\”;
for(int i = 0; i <500; i ++) { PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
PostMessage(hCurrentWindow,WM_KEYUP,0x45,MapVirtualKey(0x45,MAPVK_VK_TO_VSC)); }
std :: cout&&& GetLastError()&& std ::
系统(“暂停”);
return 0; }
现在,每次都会发送“透明”消息。 GetLastError()说:
) 窗口句柄无效。
GetLastError()“fixed”
int main() { HWND hCurrentW
DWORD procID;
GUITHREADINFO currentWindowGuiThreadI
睡眠(5000);
hCurrentWindow = GetForegroundWindow();
if(!hCurrentWindow) std :: cout&&“失败获取主窗口句柄\\\”;
GetWindowThreadProcessId(hCurrentWindow,& procID);
GetGUIThreadInfo(procID,& currentWindowGuiThreadInfo);
hCurrentWindow = currentWindowGuiThreadInfo.hwndF
if(!hCurrentWindow) std :: cout&&“失败获取子窗口句柄\\\”;
std :: cout&&“GO !!! \\\”;
for(int i = 0; i <500; i ++) {
if(!PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,MapVirtualKey(0x45,MAPVK_VK_TO_VSC )))std :: cout <&&& GetLastError()&& std ::
if(!PostMessage(hCurrentWindow,WM_KEYUP,0x45,MapVirtualKey(0x45,MAPVK_VK_TO_VSC)))std :: cout&&&&&&&& std :: }
系统(“Pause”);
return 0; }
...输出 1400 千次。
解决方案 当你将信息发布到错误的窗口时,这当然会发生。记事本肯定是这样。它不只有一个窗口,你可以看到与Spy ++。 GetForegroundWindow()返回一个顶级窗口,记事本的框架窗口。在框架窗口的内部,它有一个子窗口,一个EDIT控件。该窗口需要获取消息。
您需要跳过几个圈才能获得该窗口,GetFocus()函数返回具有焦点的窗口,这只有工作,如果你从拥有窗口的进程调用它。当你这样做的进程外,那么你必须首先调用GetWindowThreadProcessId()获取拥有前台窗口的线程的ID。然后你必须调用GetGUIThreadInfo(),它返回的GUITHREADINFO.hwndFocus是你需要的窗口句柄。
这还是没有麻烦,你不能控制键盘状态过程。换句话说,Shift,Ctrl和Alt键的状态以及任何死键(如某些键盘布局上的Alt + Gr)。喜欢发送WM_CHAR键入密钥。
本文地址: &
扫一扫关注官方微信

我要回帖

更多关于 some more 的文章

 

随机推荐