首页 > 解决方案 > 我想获取鼠标、键盘事件的空闲时间

问题描述

如果在我什么都不做的情况下鼠标和键盘在一定时间内没有引发事件,我想在 c# 中显示一个名为“注销”的弹出窗口。

我发现我必须使用挂钩。所以我试了一下。但是,我可以获得鼠标空闲时间的值,但无法获得键盘空闲时间的值。键盘空闲时间始终为 0。

这是我写的代码。

public partial class Form1 : Form
{

    private Timer timer;
    private InputSource lastInput;
    private KeyboardInput keyboard;
    private MouseInput mouse;

    public Form1()
    {
        InitializeComponent();


        keyboard = new KeyboardInput();
        keyboard.KeyBoardKeyPressed += keyboard_KeyBoardKeyPressed;

        mouse = new MouseInput();
        mouse.MouseMoved += mouse_MouseMoved;

        lastInput = new InputSource();

        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += Timer1_Tick;
        timer.Start();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        if (GetIdleTime() > 10000)
        {
            timer1.Enabled = false;
            MessageBox.Show("test");
            timer1.Enabled = true;
        }
    }
    private string FormatDateTime(DateTime dateTime)
    {
        return dateTime.ToString("HH:mm:ss");
    }

    private void keyboard_KeyBoardKeyPressed(object sender, EventArgs e)
    {
        Console.WriteLine(GetIdleTime());
        if (GetIdleTime() > 10000)
        {
            timer1.Enabled = false;
            MessageBox.Show("test");
            timer1.Enabled = true;
        }

        label_keyboard.Text = FormatDateTime(lastInput.GetLastInputTime());
    }

    private void mouse_MouseMoved(object sender, EventArgs e)
    {
        Console.WriteLine(GetIdleTime());
        if (GetIdleTime() > 10000)
        {
            timer1.Enabled = false;
            MessageBox.Show("mouse test");
            timer1.Enabled = true;
        }
        label_mouse.Text = FormatDateTime(lastInput.GetLastInputTime());
    }

    private void Timer1_Tick(object sender, EventArgs e)
    {
        //label_lastinput.Text = FormatDateTime(lastInput.GetLastInputTime());
        label_present.Text = FormatDateTime(DateTime.Now);
    }


}

class InputSource
{
    [DllImport("User32.dll")]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    [DllImport("Kernel32.dll")]
    private static extern uint GetLastError();

    public DateTime GetLastInputTime()
    {
        var lastInputInfo = new LASTINPUTINFO();
        lastInputInfo.dwTime = 0;
        lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);

        GetLastInputInfo(ref lastInputInfo);

        //if (Environment.TickCount - lastInputInfo.dwTime > 10000)
        //{
        //    MessageBox.Show("test");
        //}
        return DateTime.Now.AddMilliseconds(-(Environment.TickCount - lastInputInfo.dwTime));
    }

    public static uint GetIdleTime()
    {
        var lastinput = new LASTINPUTINFO();
        lastinput.cbSize = (uint)Marshal.SizeOf(lastinput);

        GetLastInputInfo(ref lastinput);
        return ((uint)Environment.TickCount - lastinput.dwTime);
    }
    public static long GetTickCount()
    {
        return Environment.TickCount;
    }

    public static long GetLastInput()
    {
        var lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut);
        if (!GetLastInputInfo(ref lastInPut))
        {
            throw new Exception(GetLastError().ToString());
        }
        return lastInPut.dwTime;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct LASTINPUTINFO
    {
        public uint cbSize;
        public uint dwTime;
    }
}

如果我想正确获得键盘和鼠标空闲时间,我应该怎么做?

标签: c#winformskeyboardhookmouse

解决方案


推荐阅读