繁体   English   中英

QT / C ++:使LASTINPUTINFO工作

[英]QT/C++: Getting LASTINPUTINFO to work

嗨,我正在通过QT学习C ++,我正在努力使LASTINPUTINFO工作。 下面是我制作的代码,以查看其工作原理,但似乎只返回一个值,并且无论何时输入任何内容都不会改变。

愿意解释我做错了什么吗? 也许可以提供一个可行的示例,以便我掌握。

我正在尝试在Windows 10 Pro 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

固定代码供参考。 多亏了安德斯(Anders)。

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM