可以通过windows api 函数GetLastInputInfo 或者 全局钩子HOOK来实现。
下面就针对GetLastInputInfo 写了个DEMO,判断鼠标键盘空闲时间超过15分站则自动弹出视频播放窗口播放视频。
新建windows 应用程序项目,代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace APPDEMO
{
static class Program
{
private static VedioForm vf = null;
private static System.Windows.Forms.Timer timer1 = null;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//启用定时器
if (timer1 == null)
{
timer1 = new Timer();
}
timer1.Interval = 60*1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
Application.Run(new MainForm());
}
private static void timer1_Tick(object sender, EventArgs e)
{
//判断空闲时间是否超过15分钟,超过则自动弹出视频播放窗口
if (GetIdleTick() / 1000 >= 15*60)
{
ShowVidioForm();
}
}
/// <summary>
/// 打开视频播放窗口
/// </summary>
private static void ShowVidioForm()
{
try
{
if (vf == null)
{
vf = new VedioForm();
}
vf.ShowDialog();
}
catch
{
}
}
/// <summary>
/// 获取鼠标键盘空闲时间
/// </summary>
/// <returns></returns>
public static long GetIdleTick()
{
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
if (!GetLastInputInfo(ref lastInputInfo)) return 0;
return Environment.TickCount - (long)lastInputInfo.dwTime;
}
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
/// <summary>
/// 调用windows API获取鼠标键盘空闲时间
/// </summary>
/// <param name="plii"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}
}
下面就针对GetLastInputInfo 写了个DEMO,判断鼠标键盘空闲时间超过15分站则自动弹出视频播放窗口播放视频。
新建windows 应用程序项目,代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace APPDEMO
{
static class Program
{
private static VedioForm vf = null;
private static System.Windows.Forms.Timer timer1 = null;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//启用定时器
if (timer1 == null)
{
timer1 = new Timer();
}
timer1.Interval = 60*1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
Application.Run(new MainForm());
}
private static void timer1_Tick(object sender, EventArgs e)
{
//判断空闲时间是否超过15分钟,超过则自动弹出视频播放窗口
if (GetIdleTick() / 1000 >= 15*60)
{
ShowVidioForm();
}
}
/// <summary>
/// 打开视频播放窗口
/// </summary>
private static void ShowVidioForm()
{
try
{
if (vf == null)
{
vf = new VedioForm();
}
vf.ShowDialog();
}
catch
{
}
}
/// <summary>
/// 获取鼠标键盘空闲时间
/// </summary>
/// <returns></returns>
public static long GetIdleTick()
{
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
if (!GetLastInputInfo(ref lastInputInfo)) return 0;
return Environment.TickCount - (long)lastInputInfo.dwTime;
}
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
/// <summary>
/// 调用windows API获取鼠标键盘空闲时间
/// </summary>
/// <param name="plii"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}
}