How to check in C++ if the system is active?

前端 未结 6 1699
温柔的废话
温柔的废话 2021-02-09 10:16

I\'m writing code that need to run only when there is no human activity on the PC, like when the screensaver is running. Any suggestions on how to do this in c++ under windows?<

6条回答
  •  暖寄归人
    2021-02-09 10:58

    Maybe there is confusion for some people here with plii.dwTime

    plii.dwTime gives the timestamp date of last input, it doesn't give time beteween now and the last input

    for this you have to sub it to GetTickCount() :

    LASTINPUTINFO plii;
    plii.cbSize = sizeof(LASTINPUTINFO);
    plii.dwTime = 0;
    
    if (GetLastInputInfo(&plii) != 0)
    {
        cout << "Last activity  : " <<  GetTickCount() - plii.dwTime << " (ms)" << endl;
    }
    else {
        cout << "GetLastInputInfo ERROR" << endl;
    }
    

提交回复
热议问题