首先,這不是能自動幫你清潔鍵鼠的工具 XD
它只是鎖定鍵盤滑鼠讓你在開機的時候可以清潔而已

KMClear - 鍵鼠清潔輔助器

支援平台:Windows 7, 2008, XP, 2003
環境需求:.Net FrameWork 3.5
檔案下載:KMClear.exe(11KB)
源碼下載:KMClear.zip(112KB – Visual C# 2008 Express)

這類程式有兩個重點要處理,一個是全螢幕執行,另一個就是鍵盤攔截。
全螢幕執行比較簡單,完全不需要 API:

private void Form1_Load(object sender, EventArgs e)
{
    this.SetVisibleCore(false);
    this.FormBorderStyle = FormBorderStyle.None;
    this.Left = (Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2);
    this.Top = (Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);
    this.SetVisibleCore(true);
}

另一個則是需要借助 Low-level API 的鍵盤攔截:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace KMClear
{
    internal class HookAPI
    {
        /* API Assign */
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, uint threadId);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr idHook);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr hKeyboardHook = IntPtr.Zero;
        private HookProc KeyboardHookProcedure;

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;

        /// 
        /// Start Keyboard Hook Process
        /// 
        public void HookStart()
        {
            if (hKeyboardHook == IntPtr.Zero)
            {
                using (Process curProcess = Process.GetCurrentProcess())
                {
                    using (ProcessModule curModule = curProcess.MainModule)
                    {
                        KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                        hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(curModule.ModuleName), 0);
                        if (hKeyboardHook == IntPtr.Zero)
                        {
                            HookStop();
                            throw new Exception("SetWindowsHookEx failed.");
                        }
                    }
                }
            }
        }

        /// 
        /// Stop Keyboard Hook Process
        /// 
        public void HookStop()
        {
            bool retKeyboard = true;
            if (hKeyboardHook != IntPtr.Zero)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = IntPtr.Zero;
            }
            if (!(retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
        }

        /// 
        /// Keyboard Hook Process
        /// 
        /// 
        /// 
        /// 
        /// 
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine((System.Windows.Forms.Keys)vkCode);
                return new IntPtr(1);
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }              

    }
}