Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;
Using System. Reflection;
Using System. Diagnostics;
Namespace TestWin
{
Public partial class Form1: Form
{
// Delegate
Public delegate int HookProc (int nCode, int wParam, IntPtr lParam );
Static int hHook = 0;
Public const int WH_KEYBOARD_LL = 13;
// The LowLevel keyboard is intercepted. If it is WH_KEYBOARD = 2, it cannot intercept the system keyboard. Acrobat Reader will obtain the keyboard before you intercept it.
HookProc KeyBoardHookProcedure;
// Keyboard Hook Structure Function
[StructLayout (LayoutKind. Sequential)]
Public class KeyBoardHookStruct
{
Public int vkCode;
Public int scanCode;
Public int flags;
Public int time;
Public int dwExtraInfo;
}
# Region DllImport
// Set the hook
[DllImport ("user32.dll")]
Public static extern int SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId );
[DllImport ("user32.dll", CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
// Remove the hook
Public static extern bool UnhookWindowsHookEx (int idHook );
[DllImport ("user32.dll")]
// Call the next hook
Public static extern int CallNextHookEx (int idHook, int nCode, int wParam, IntPtr lParam );
[DllImport ("kernel32.dll")]
Public static extern int GetCurrentThreadId ();
[DllImport ("kernel32.dll")]
Public static extern IntPtr GetModuleHandle (string name );
# Endregion
# Region custom events
Public void Hook_Start ()
{
// Install the keyboard hook
If (hHook = 0)
{
KeyBoardHookProcedure = new HookProc (KeyBoardHookProc );
// HHook = SetWindowsHookEx (2,
// KeyBoardHookProcedure,
// GetModuleHandle (Process. GetCurrentProcess (). MainModule. ModuleName), GetCurrentThreadId ());
HHook = SetWindowsHookEx (WH_KEYBOARD_LL,
KeyBoardHookProcedure,
GetModuleHandle (Process. GetCurrentProcess (). MainModule. ModuleName), 0 );
// If the hook fails to be set.
If (hHook = 0)
{
Hook_Clear ();
// Throw new Exception ("Hook setting failed! ");
}
}
}
// Cancel the hook event
Public void Hook_Clear ()
{
Bool retKeyboard = true;
If (hHook! = 0)
{
RetKeyboard = UnhookWindowsHookEx (hHook );
HHook = 0;
}
// If you fail to remove the hook.
If (! RetKeyboard) throw new Exception ("UnhookWindowsHookEx failed .");
}
// Add the desired information here
Public static int KeyBoardHookProc (int nCode, int wParam, IntPtr lParam)
{
If (nCode> = 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct) Marshal. PtrToStructure (lParam, typeof (KeyBoardHookStruct ));
If (kbh. vkCode = (int) Keys. S & (int) Control. ModifierKeys = (int) Keys. Control) // intercept F8
{
MessageBox. Show ("the shortcut key has been intercepted! Cannot be saved! ");
Return 1;
}
If (kbh. vkCode = (int) Keys. Y
& (Int) Control. ModifierKeys = (int) Keys. Control + (int) Keys. Alt) // Ctrl + Alt + Y
{
// MessageBox. Show ("cannot be saved all! ");
Return 1;
}
If (kbh. vkCode = (int) Keys. X)
{
MessageBox. Show ("cannot be saved all! ");
Return 1;
}
If (kbh. vkCode = (int) Keys. LWin)
{
// MessageBox. Show ("the system start menu cannot be used! ");
Return 1;
}
}
Return CallNextHookEx (hHook, nCode, wParam, lParam );
}
# Endregion
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Hook_Start ();
}
Private void button2_Click (object sender, EventArgs e)
{
Hook_Clear ();
}
}
}