简体   繁体   中英

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. 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.

#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.

#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. You actually have to call a function to fill it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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