Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Windows.Forms;
- namespace KeyboardHook
- {
- public sealed class SimpleKeyboardHook : IDisposable
- {
- public SimpleKeyboardHook(Action<Keys> handler)
- {
- if (null == handler) { throw new ArgumentNullException("handler"); }
- this._handler = handler;
- var t = new Thread(this.ListenerThread) { IsBackground = true, Name = "KeyboardListener" };
- t.Start();
- }
- public void Dispose()
- {
- if (!this._disposed)
- {
- UnhookWindowsHookEx(this._id);
- this._disposed = true;
- GC.SuppressFinalize(this);
- }
- }
- public static void BreakOnF12(Keys keys)
- {
- if (keys == Keys.F12)
- {
- Debugger.Break();
- }
- }
- private void ListenerThread()
- {
- using (var currentProcess = Process.GetCurrentProcess())
- using (var mainModule = currentProcess.MainModule)
- {
- if (null == mainModule) { throw new InvalidOperationException("Unable to determine main module for the current process"); }
- this._id = SetWindowsHookEx(
- WH_KEYBOARD_LL,
- this.HookCallback,
- GetModuleHandle(mainModule.ModuleName), 0);
- }
- Application.Run();
- }
- private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- var vkCode = Marshal.ReadInt32(lParam);
- this._handler((Keys)vkCode);
- }
- return CallNextHookEx(this._id, nCode, wParam, lParam);
- }
- private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr 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);
- private const int WH_KEYBOARD_LL = 13;
- private const int WM_KEYDOWN = 0x0100;
- private IntPtr _id;
- private readonly Action<Keys> _handler;
- private volatile bool _disposed;
- }
- static class Program
- {
- private static void GoToSleep(object sender, EventArgs args)
- {
- Thread.Sleep(10000);
- }
- [STAThread]
- static void Main()
- {
- using (new SimpleKeyboardHook(SimpleKeyboardHook.BreakOnF12))
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- var form = new Form { Text = "Sleepy form", Size = new Size(160,80), Padding = new Padding(6) };
- var btn = new Button { Dock = DockStyle.Fill, Text = "Sleep", Location = new Point(10, 10) };
- btn.Click += GoToSleep;
- form.Controls.Add(btn);
- Application.Run(form);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment