C # source code for shielding system keyboard messages

Source: Internet
Author: User

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 ();

}
}
}

 

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.