問題描述
我正在嘗試捕獲全局鼠標(biāo)和鍵盤輸入.
I'm trying to capture global mouse and keyboard input.
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0) {
if (wParam == WM_RBUTTONDOWN) printf("right mouse down
");
if (wParam == WM_RBUTTONUP) printf("right mouse up
");
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
HHOOK mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
while(true) {
MSG msg;
if (PeekMessage(&msg,0,0,0,PM_REMOVE)) {
printf("msg recvd
");
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#ifdef TEST
Sleep(50);
#endif
}
所以這里一切正常,除非我 #define TEST
放入 Sleep
,鼠標(biāo)變得非常遲鈍,如果我突然只允許鼠標(biāo)每秒更新 20 次.如果沒有睡眠,我將 CPU 固定在 100%.但這暫時(shí)沒問題(如果我使用 GetMessage
就會(huì)消失).
So everything works here, except if I #define TEST
to put in the Sleep
, the mouse becomes incredibly sluggish, as might be expected if I suddenly only allow the mouse to update 20 times a second. And without the sleep, I am pegging the CPU at 100%. But that's okay for now (that goes away if I use GetMessage
).
現(xiàn)在據(jù)我所知,低級(jí)鉤子通過上下文切換到安裝它的進(jìn)程來工作,然后向進(jìn)程發(fā)送某種消息以讓它執(zhí)行鉤子回調(diào).不過,讓我有點(diǎn)困惑的是,為什么我的程序永遠(yuǎn)不會(huì)打印msg recvd",但每當(dāng)我單擊鼠標(biāo)右鍵時(shí),它就會(huì)打印鼠標(biāo)右鍵向下/向上".這使我得出結(jié)論,我的 MouseHookProc
在 PeekMessage
調(diào)用期間被調(diào)用.它恰好是某種特殊的消息,PeekMessage
返回 0.但我仍然需要調(diào)用 PeekMessage
或一些等效的.
Now as I understand it, the low-level hooks work by context-switching to the process which installed it, and then sending the process some kind of message to let it execute the hook callback. What confuses me a little, though, is why my program will never print "msg recvd", but it prints "right mouse down/up" whenever i click the right mouse button. This leads me to conclude that my MouseHookProc
is being invoked during the PeekMessage
call. It just happens to be some kind of special message and PeekMessage
returns 0. But I still need to call PeekMessage
or some equivalent.
由于我的程序需要做很多事情,我顯然不能通過調(diào)用另一個(gè)需要 50 毫秒返回的函數(shù)來減輕我的消息泵循環(huán)(調(diào)用 PeekMessage
的那個(gè)).我如何多線程處理我的程序以保持鼠標(biāo)響應(yīng),同時(shí)做一些繁重的工作?在多線程的win32程序中,仍然只有一個(gè)消息隊(duì)列,對(duì)嗎?
Since my program needs to do a bunch of things, I clearly can't weigh down my message pumping loop (the one that calls PeekMessage
) by calling another function that takes, say 50ms to return. How might I multithread my program to maintain mouse responsiveness while simultaneously doing a little heavy lifting? In a multithreaded win32 program, there is still just one message queue, right?
更新:在閱讀了 MS 的文檔后,我想我知道我應(yīng)該做什么是正確的.我應(yīng)該在我的應(yīng)用程序中生成一個(gè)線程,它調(diào)用 SetWindowsHookEx
來注冊(cè)鼠標(biāo)鉤子,然后坐在它自己的消息循環(huán)中,系統(tǒng)將負(fù)責(zé)將鼠標(biāo)更新發(fā)送到這個(gè)線程.它可以在 MouseHookProc
中自由地做任何它想做的事情,而我的應(yīng)用程序的其余部分將獨(dú)立運(yùn)行.
Update: After reading up on MS's documentation I think I know what the right thing for me to do is. I should just spawn a thread in my application which calls SetWindowsHookEx
to register the mouse hook, and then sit around in its own message loop, and the system will take care of sending the mouse updates to this thread. It will be free to do whatever it wants within the MouseHookProc
, and the rest of my application will run independently.
推薦答案
問題在于你的消息循環(huán),因?yàn)槟闶褂?PeekMessage(),它消耗了 100% 的 CPU 周期.即使您不輪詢消息,Windows 也知道如何使鉤子保持活動(dòng)狀態(tài),請(qǐng)使用 GetMessage() 來解決您的問題.使用 Sleep(1) 也可以解決您的問題,但在這里不是必需的.
The problem is your message loop, it burns 100% CPU cycles because you use PeekMessage(). Windows knows how to keep the hook alive even if you don't poll for messages, use GetMessage() to solve your problem. Using Sleep(1) will solve your problem too but is not necessary here.
為什么必須將 SetWindowsHookEx 與 Windows 消息隊(duì)列一起使用
這篇關(guān)于了解低級(jí)鍵鼠鉤子(win32)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!