使用Qt获得系统空闲时间

joc*_*ull 5 c# winapi qt cross-platform dllimport

我几周前就是Qt的新手.我正在尝试用C++重写一个C#应用程序,并且现在有很大一部分.我目前面临的挑战是找到一种检测系统空闲时间的方法.

使用我的C#应用​​程序,我从某处看起来像这样的代码:

public struct LastInputInfo
{
    public uint cbSize;
    public uint dwTime;
}

[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LastInputInfo plii);

/// <summary>
/// Returns the number of milliseconds since the last user input (or mouse movement)
/// </summary>
public static uint GetIdleTime()
{
    LastInputInfo lastInput = new LastInputInfo();
    lastInput.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInput);
    GetLastInputInfo(ref lastInput);

    return ((uint)Environment.TickCount - lastInput.dwTime);
}
Run Code Online (Sandbox Code Playgroud)

我还没有学会如何通过DLL Imports或C++等价物引用Windows API函数.老实说,如果可能,我宁愿避免使用它们.此应用程序也将在未来转向Mac OSX和Linux.

有没有Qt特定的,与平台无关的方式来获得系统空闲时间?意味着用户没有触摸鼠标或任何键的X时间.

提前感谢您提供的任何帮助.

joc*_*ull 2

由于似乎没有人知道,而且我不确定这是否可能,因此我决定设置一个低间隔轮询计时器来检查鼠标的当前 X、Y。我知道这不是一个完美的解决方案,但是......

  1. 它可以跨平台工作,而不需要我做特定于平台的事情(比如 DLL 导入,恶心)
  2. 它达到了我需要它的目的:确定某人是否正在积极使用该系统

是的,是的,我知道在某些情况下,某人可能没有鼠标或其他东西。我现在称之为“低活动状态”。够好了。这是代码:

mainwindow.h -类声明

private:
    QPoint mouseLastPos;
    QTimer *mouseTimer;
    quint32 mouseIdleSeconds;
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp -构造函数方法

//Init
mouseTimer = new QTimer();
mouseLastPos = QCursor::pos();
mouseIdleSeconds = 0;

//Connect and Start
connect(mouseTimer, SIGNAL(timeout()), this, SLOT(mouseTimerTick()));
mouseTimer->start(1000);
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp -类主体

void MainWindow::mouseTimerTick()
{
    QPoint point = QCursor::pos();
    if(point != mouseLastPos)
        mouseIdleSeconds = 0;
    else
        mouseIdleSeconds++;

    mouseLastPos = point;

    //Here you could determine whatever to do
    //with the total number of idle seconds.
}
Run Code Online (Sandbox Code Playgroud)