Timers in windowless win32api applications

From Code Trash
Jump to: navigation, search

At the bottom you can find a reference of an article i created with the same code with explanation. Though i had used it for another purpose this code explains the purpose of the title i used.


#include <windows.h>
 
void getidle();
void press();
 
DWORD previous = 0;
 
//int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
int main()
{
 
    SetTimer(NULL, 1, 20000, (TIMERPROC) getidle);
    MSG msg;
 
 
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return 0;
}
 
//void getidle(HWND hwnd, UINT ui, INT i, DWORD dw)
void getidle()
{                      
    LASTINPUTINFO info; 
    info.cbSize = sizeof(info);
    GetLastInputInfo(&info);
    DWORD idleSeconds = (GetTickCount() - info.dwTime)/1000;
    previous = idleSeconds;
 
    if(previous > 20)press();    
}
 
 
void press()
{
      // Simulate a key press
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | 0,
                      0 );
 
      // Simulate a key release
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                      0);
 
      // Simulate a key press
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | 0,
                      0 );
 
      // Simulate a key release
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                      0);
 
}


Reference

Windowless Timers example, code and explanation