1

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.

NetCoder89
  • 23
  • 6

2 Answers2

3

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Is it possible to do it by hook? – NetCoder89 Dec 17 '13 at 04:24
  • Brittle solution, that doesn't work reliably, and certainly not across processes. Set up a timer with your initial 5s, call `GetLastInputInfo` when it expires, hide the cursor, if `dwTime > 5000`, or set a new timer with a timeout of `5000 - dwTime`. – IInspectable Dec 18 '13 at 12:56
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; 
} 
Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • Any suggestion on when/where to call this function? Every second? – egur Dec 16 '13 at 14:19
  • @egur Yes, as you desire, every second, every half-second, whatever. – Roger Rowland Dec 16 '13 at 14:23
  • This function takes into consideration ALL input events, not just mouse events. If he wants to check just the mouse input, this function would not be good. – Paladine Dec 16 '13 at 15:20
  • @Paladine yes, this is true - as I stated in my answer *"...since the last mouse move or key press"*. – Roger Rowland Dec 16 '13 at 15:21
  • @Roger,, I've done this using LASTINPUTINFO. But I'll have to change the cursor image every time,,means replace mouse by a blank cursor on hide and again an image on show..Do you think its a good solution? – NetCoder89 Dec 17 '13 at 04:22
  • @NetCoder89 unless you have a console app, `ShowCursor` is the best way - [see this answer](http://stackoverflow.com/a/16147847/2065121) for examples. – Roger Rowland Dec 17 '13 at 05:42
  • @RogerRowland, i'VE CONSOLE app, so I'm trying to do it by LoadCursor and SetSystemCursor()..can you please see this as I'm here.. [Error](http://stackoverflow.com/questions/20655350/hide-and-show-mouse-cursor-after-a-particular-interval-of-time) – NetCoder89 Dec 18 '13 at 11:28