计算屏幕上的按键数量

时间:2012-08-03 11:09:50

标签: c# keypress

如何计算c#应用程序中全屏按下的总键数? 就像应用程序在后面运行而我正在输入...然后我可以看到我按下了多少键。谢谢。

5 个答案:

答案 0 :(得分:24)

MSDN Blogs上找到:

实测值:

   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            _hookID = SetHook(_proc);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            UnhookWindowsHookEx(_hookID);

        }

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;


        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private delegate IntPtr LowLevelKeyboardProc(
            int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine((Keys)vkCode);
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

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

答案 1 :(得分:1)

我会解决你的应用程序运行的原因(将代码移动到后台工作者?)

如果您在执行此操作后仍需要全局keypresscount,请查看在C#中挂起keypress应用程序的此demoproject

http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook

答案 2 :(得分:0)

也许它会给你一些想法:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Return)
  {
     textBlock1.Text = "You Entered: " + textBox1.Text;
  }
}

答案 3 :(得分:0)

在此处下载演示项目表单:

Keylogger Project

并根据需要进行自定义更改。

答案 4 :(得分:0)

我使用下面的ProcessKeyEventArgs在我的C#Windows窗体中捕获Print Screen次按键。

我所做的只是修改它是添加一个计数器并递增它。

private long counter = 0;

protected override bool ProcessKeyEventArgs(ref Message msg) {
  counter++;
  return base.ProcessKeyEventArgs(ref msg);
}