-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalKeyboardHook.cs
More file actions
105 lines (91 loc) · 3.93 KB
/
Copy pathGlobalKeyboardHook.cs
File metadata and controls
105 lines (91 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using ShiftHappens;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Input;
public class GlobalKeyboardHook : IDisposable
{
private static string _internalBuffer = "";
// These are the Win32 "Standards" - they haven't changed in forever.
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private LowLevelKeyboardProc _proc;
private IntPtr _hookID = IntPtr.Zero;
public int vkCode;
// This event is what your UI will subscribe to
public event EventHandler<KeyEventArgs> KeyboardPressed;
public GlobalKeyboardHook()
{
_proc = HookCallback;
_hookID = SetHook(_proc);
}
private IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static readonly Random _rng = new Random();
private const string SYMBOL_POOL = "!@#$%^&?";
private string GetRandomSymbols(int length)
{
char[] result = new char[length];
for (int i = 0; i < length; i++)
{
result[i] = SYMBOL_POOL[_rng.Next(SYMBOL_POOL.Length)];
}
return new string(result);
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
// 1. RAW LOGIC - No WPF events involved
if (vkCode >= 65 && vkCode <= 90) // A-Z
{
_internalBuffer += (char)vkCode;
}
else if (vkCode == 0x20 || vkCode == 0x0D) // Space or Enter
{
if (_internalBuffer.Equals("SHITHOLE", StringComparison.OrdinalIgnoreCase) ||
_internalBuffer.Equals("FUCKING", StringComparison.OrdinalIgnoreCase) || _internalBuffer.Equals("RETARDED", StringComparison.OrdinalIgnoreCase) || _internalBuffer.Equals("FUCK", StringComparison.OrdinalIgnoreCase) || _internalBuffer.Equals("RETARD", StringComparison.OrdinalIgnoreCase))
{
// Generate symbols matching the word length + the space/enter (1)
string randomized = GetRandomSymbols(_internalBuffer.Length + 1);
InputSimulator.EraseAndReplace(_internalBuffer.Length + 1, randomized);
}
_internalBuffer = "";
}
else if (vkCode == 0x08 && _internalBuffer.Length > 0) // Backspace
{
_internalBuffer = _internalBuffer.Substring(0, _internalBuffer.Length - 1);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
// --- Win32 API Imports (The "Legacy" Power) ---
[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);
public void Dispose()
{
UnhookWindowsHookEx(_hookID);
}
}
// Simple Helper for the Event
public class KeyEventArgs : EventArgs
{
public int KeyCode { get; }
public KeyEventArgs(int keyCode) => KeyCode = keyCode;
}