QT/C++: Getting LASTINPUTINFO to work

QT/C++: Getting LASTINPUTINFO to work

本文关键字:LASTINPUTINFO to work Getting C++ QT      更新时间:2023-10-16
嗨,我正在

通过QT学习C++,我正在尝试让LASTINPUTINFO工作。下面是我为了解它的工作原理而编写的代码,但它似乎只返回一个值,并且在我进行任何输入时都不会更改。

想解释我做错了什么吗?也许提供一个工作示例,以便我掌握。

我正在尝试在 Windows 10 专业版 64 位上运行它。

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
using namespace std;
test()
{
    LASTINPUTINFO lastii;
    lastii.cbSize = sizeof(LASTINPUTINFO);
    return lastii.dwTime;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    while (true) {
        qDebug() << test();
        sleep(1);
    }
    return a.exec();
}

此处的示例输出。

138899896
138899896
138899896
138899896
138899896
138899896
138899896

固定代码以供参考。感谢安德斯。

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
#include <iostream>
using namespace std;
test()
{
    LASTINPUTINFO lastii;
    lastii.cbSize = sizeof(LASTINPUTINFO);
    GetLastInputInfo(&lastii);

    return (GetTickCount() - lastii.dwTime) / 1000;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    while (true) {
        cout<<test()<<"n";
        sleep(1);
    }
    return a.exec();
}

LASTINPUTINFO不是类,而是一个简单的C结构。你实际上必须调用一个函数来填充它:

DWORD test() {
  LASTINPUTINFO lastii;
  lastii.cbSize = sizeof(LASTINPUTINFO);
  GetLastInputInfo(&lastii);
  return lastii.dwTime;
}