Design of keyboard hook in C #

Source: Internet
Author: User

During the idle time, I learned the keyboard hook and used the C # language to design and implement it. below is the class diagram I designed:

There are two types of keyboard hooks: Global hooks and private hooks. Here I will design them in two categories:

 

Public delegate int HOOKPROC (int nCode, int wParam, int lParam );
Public enum HookType
{
WH_KEYBOARD = 2, // Private Hook
WH_KEYBOARD_LL = 13 // global hook
}

 

 

 

Hook base class implementation Public abstract class hook
{
// Set the hook
[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
Public static extern int SetWindowsHookEx (HookType idHook, HOOKPROC lpfn, IntPtr hInstance, int threadId );
// Hook Extraction
[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
Public static extern bool UnhookWindowsHookEx (int idHook );

[DllImport ("kernel32")]
Public static extern int GetCurrentThreadId ();

/// <Summary>
/// Hook handling delegate
/// </Summary>
Public HOOKPROC proc;
/// <Summary>
/// Hook type
/// </Summary>
Public & n

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.