c# - 检测用户事件

标签 c# windows

我需要创建一个程序来监视计算机的事件。例如鼠标移动、鼠标点击或键盘输入。我不需要记录计算机正在使用中发生的事情。如果他们的计算机有一段时间未使用,即 15 分钟,我需要触发一个事件。

有什么方法可以让我收到这些事件的通知?

最佳答案

谢谢LordCover。此代码来自here .此类为您控制键盘和鼠标控件。您可以在这样的计时器中使用:

private void timer1_Tick(object sender, EventArgs e)
{
    listBox1.Items.Add(Win32.GetIdleTime().ToString());
    if (Win32.GetIdleTime() > 60000) // 1 minute
    {
        textBox1.Text = "SLEEPING NOW";
    }
}

控制的主要代码。粘贴到您的表单代码。

internal struct LASTINPUTINFO
{
    public uint cbSize;
    public uint dwTime;
}

public class Win32
{
    [DllImport("User32.dll")]
    public static extern bool LockWorkStation();

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

    [DllImport("Kernel32.dll")]
    private static extern uint GetLastError();

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

    public static long GetLastInputTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        if (!GetLastInputInfo(ref lastInPut))
        {
            throw new Exception(GetLastError().ToString());
        }

        return lastInPut.dwTime;
    }
}

关于c# - 检测用户事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5244943/

相关文章:

c# - 在 Windows Phone 8.1 上连接和断开蓝牙 BLE

mysql - 静默安装 MySql 作为服务

c++ - 奇怪的 GetTickCount 问题

c# - 如何根据调用方法的调用(按钮)发送者(按钮#)操作变量名称?

c# - 在使用 ResourceManager.GetString() 时如何避免使用魔术字符串?

c# - "No credentials are available in the security package"连接Oracle时

c++ - CreateIoCompletionPort 的并发线程数

windows - 无论如何,Apache ant 都会删除目录

python - 有没有办法在 Python 中编辑包含图像的 xlsx 工作簿?

c# - Linq、Where 扩展方法、Lambda 表达式和 Bool 的