gpt4 book ai didi

c# - 检测用户事件

转载 作者:可可西里 更新时间:2023-11-01 12:41:06 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com