通过判断鼠标键盘的输入来获取 Windows 空闲时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <QCoreApplication> #include <QDebug>
#include <windows.h> #include <winuser.h> #include <sysinfoapi.h> #include <thread>
void DoSomething() { qDebug() << "start do..."; std::this_thread::sleep_for(std::chrono::seconds(10)); qDebug() << "end."; }
void PrintLastInput() { int t = 5; while (1) { std::this_thread::sleep_for(std::chrono::seconds(1)); LASTINPUTINFO input; div_t iTime = {0}; input.cbSize = sizeof(LASTINPUTINFO);
if (GetLastInputInfo(&input)) { iTime = div((GetTickCount() - input.dwTime), 1000); qDebug() << iTime.quot; if (iTime.quot >= 5) { DoSomething(); break; } } } }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv);
std::thread([](){ qDebug() << "start thread: PrintLastInput..."; PrintLastInput(); }).detach();
while (1) { qDebug() << "sleep..."; std::this_thread::sleep_for(std::chrono::seconds(5)); }
return a.exec(); }
|