问题
I'm working on a task for my game program, in which I want to hide my mouse after 10 seconds from my screen. However I'm able to check the mouse move condition... Here is my code..
using namespace std;
HHOOK g_hMouseHook;
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
PMSLLHOOKSTRUCT pmll = (PMSLLHOOKSTRUCT) lParam;
switch (wParam)
{
case WM_MOUSEMOVE:
printf("Mouse has been moved\n");
break;
}
}
return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
}
int _tmain(int argc, _TCHAR* argv[])
{
MSG msg;
g_hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, LowLevelMouseProc, GetModuleHandle(NULL), 0 );
if (!g_hMouseHook)
printf("err: %d\n", GetLastError());
while ( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(g_hMouseHook);
return (int) msg.wParam;
}
I'm doing it by using hook and it is displaying the mouse movement successfully. Now I'm searching for an option with which I can also check that my mouse has not been moved. *one thing I'm making it on console.
I've changed the mouse cursor or the hide by using LoadCursorfromFile and it's working properly.
Please share your opinions on to check when the mouse is not moving.
回答1:
Perhaps GetLastInputInfo
is what you need MSDN here.
For example, to get the elapsed milliseconds since the last mouse move or key press, you might have a function like this:
DWORD GetIdleTime()
{
LASTINPUTINFO pInput;
pInput.cbSize = sizeof(LASTINPUTINFO);
if (!GetLastInputInfo(&pInput))
{
// report error, etc.
}
// return idle time in millisecs
return pInput.dwTime;
}
回答2:
Call TrackMouseEvent
to set up an idle time of 5000 milliseconds. You'll get a WM_HOVER
message after 5 seconds. You could reset the timer on every keypress, but that's a bit inefficient. Instead, on every keypress you should update a lastKeyPressedTime
variable. Now, if WM_HOVER
arrives after 5 seconds, you check to see if the last keypress is 5 seconds ago. If it is, you have neither keyboard nor mouse input an can remove the mouse.
If you had keyboard input in the last 5 seconds while the mouse was idle, you should reset the TrackMouseEvent
. If you're lazy, reset it to 5 seconds again. If you're being accurate, you have to get a bit more creative.
来源:https://stackoverflow.com/questions/20611382/how-to-check-mouse-is-not-moved-from-last-5-seconds