简体   繁体   English

QT / C ++:使LASTINPUTINFO工作

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

Hi I am learning C++ through QT and I'm on the part where I'm trying to get LASTINPUTINFO to work. 嗨,我正在通过QT学习C ++,我正在努力使LASTINPUTINFO工作。 Below is the code I have made to see how it works but it seems to be only returning a single value and doesn't ever change whenever I make any inputs. 下面是我制作的代码,以查看其工作原理,但似乎只返回一个值,并且无论何时输入任何内容都不会改变。

Care to explain what I'm doing wrong? 愿意解释我做错了什么吗? And maybe provide a working example so I could get a grasp. 也许可以提供一个可行的示例,以便我掌握。

I'm trying to run it on Windows 10 Pro 64-bit. 我正在尝试在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();
}

Example output here. 示例输出在这里。

138899896
138899896
138899896
138899896
138899896
138899896
138899896

Fixed code for reference. 固定代码供参考。 Thanks to Anders. 多亏了安德斯(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 is not a class, it is a simple C struct. LASTINPUTINFO不是类,它是一个简单的C结构。 You actually have to call a function to fill it: 您实际上必须调用一个函数来填充它:

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