c# - 使用 Qt 获取系统空闲时间

标签 c# qt winapi 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);
}

我还没有学会如何通过 DLL 导入或任何 C++ 等效项来引用 Windows API 函数。老实说,如果可能的话,我宁愿避免它们。此应用程序正在迁移到 Mac OSX,将来也可能迁移到 Linux。

是否有 Qt 特定的、平台无关的方法来获取系统空闲时间?这意味着用户在 X 时间内没有触摸鼠标或任何键。

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

最佳答案

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

  1. 无需我做特定于平台的事情(比如 DLL 导入,该死)它就可以跨平台工作
  2. 它满足我需要它的目的:确定某人是否正在积极使用该系统

是的,是的,我知道有些人可能没有鼠标或其他东西。我暂时称其为“低事件状态”。够好了。这是代码:

ma​​inwindow.h - 类声明

private:
    QPoint mouseLastPos;
    QTimer *mouseTimer;
    quint32 mouseIdleSeconds;

ma​​inwindow.cpp - 构造方法

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

//Connect and Start
connect(mouseTimer, SIGNAL(timeout()), this, SLOT(mouseTimerTick()));
mouseTimer->start(1000);

ma​​inwindow.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.
}

关于c# - 使用 Qt 获取系统空闲时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3911367/

相关文章:

qt - 如何在 QT 中的 TreeView 项中呈现复杂的小部件?

c++ - 无法在 Qt 中调用 PaintEvent (C++)

windows - 更改多表单应用程序中的窗口顺序

c++ - 子窗口不捕获 WM_MOUSEWHEEL 事件

c# - 元组列表到该元组 C# 列表中第一个元素的列表

c# - 使用存储库模式将 DynamicTableEntity 写入 Azure 表存储时出现问题

c# - 如何在 WebPartZone 中加载 highchart 脚本

qt - QDesktopServices::openUrl 在资源管理器中选择指定文件

c++ - 如何获取 WinRT/Windows 10 商店代码的 HRESULT 错误代码的说明?

c# - 为什么我的单例有两个不同的实例?