0% found this document useful (0 votes)
5 views1 page

Script

The document contains a C# code snippet for a Windows Forms application that blocks specific keyboard keys (Esc, F6, Left Shift, Right Shift) using a low-level keyboard hook. It defines a class 'KeyBlockerApp' that sets up the hook and overrides the visibility of the form. The application runs in the background, intercepting and blocking the specified key presses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Script

The document contains a C# code snippet for a Windows Forms application that blocks specific keyboard keys (Esc, F6, Left Shift, Right Shift) using a low-level keyboard hook. It defines a class 'KeyBlockerApp' that sets up the hook and overrides the visibility of the form. The application runs in the background, intercepting and blocking the specified key presses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Add-Type -ReferencedAssemblies "System.Windows.

Forms" -TypeDefinition @"


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class KeyBlockerApp : Form {


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

public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr


lParam);

protected override void SetVisibleCore(bool value) {


base.SetVisibleCore(false); // Ẩn cửa sổ form
}

public static void Start() {


_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(null),
0);
Application.Run(new KeyBlockerApp());
UnhookWindowsHookEx(_hookID);
}

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {


if (nCode >= 0) {
int vkCode = Marshal.ReadInt32(lParam);

// 27 = Esc, 117 = F6, 160 = Left Shift, 161 = Right Shift


if (vkCode == 27 || vkCode == 117 || vkCode == 160 || vkCode == 161) {
return (IntPtr)1; // Chặn phím
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

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

[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

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

[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
"@

[KeyBlockerApp]::Start()

You might also like