■ 키보드 후킹을 사용해 WIN + C 키를 누를 경우 메모장을 실행하는 방법을 보여준다.
※ 윈도우즈 10 버전에서 코타나 실행도 방지한다.
▶ MainApplication.xaml.cs
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows; namespace TestProject; /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 저수준 키보드 프로시저 - LowLevelKeyboardProcedure(code, wordParameter, longParameter) /// <summary> /// 저수준 키보드 프로시저 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private delegate IntPtr LowLevelKeyboardProcedure(int code, IntPtr wordParameter, IntPtr longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 윈도우즈 후킹 설정하기 (확장) - SetWindowsHookEx(hookID, lowLevelKeyboardProcedure, moduleHandle, threadID) /// <summary> /// 윈도우즈 후킹 설정하기 (확장) /// </summary> /// <param name="hookID">후킹 ID</param> /// <param name="lowLevelKeyboardProcedure">저수준 키보드 프로시저</param> /// <param name="moduleHandle">모듈 핸들</param> /// <param name="threadID">쓰레드 ID</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int hookID, LowLevelKeyboardProcedure lowLevelKeyboardProcedure, IntPtr moduleHandle, uint threadID); #endregion #region 윈도우즈 후킹 해제하기 (확장) - UnhookWindowsHookEx(hookHandle) /// <summary> /// 윈도우즈 후킹 해제하기 (확장) /// </summary> /// <param name="hookHandle">후킹 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hookHandle); #endregion #region 다음 후킹 호출하기 (확장) - CallNextHookEx(hookHandle, code, wordParameter, longParameter) /// <summary> /// 다음 후킹 호출하기 (확장) /// </summary> /// <param name="hookHandle">후킹 핸들</param> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hookHandle, int code, IntPtr wordParameter, IntPtr longParameter); #endregion #region 모듈 핸들 구하기 - GetModuleHandle(moduleName) /// <summary> /// 모듈 핸들 구하기 /// </summary> /// <param name="moduleName">모듈명</param> /// <returns>모듈 핸들</returns> [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string moduleName); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 저수준 키보드 프로시저 /// </summary> private static LowLevelKeyboardProcedure _lowLevelKeyboardProcedure = HookCallback; /// <summary> /// 후킹 ID /// </summary> private static IntPtr _hookID = IntPtr.Zero; /// <summary> /// WIN 키 PRESS 여부 /// </summary> private static bool _isWINKeyPressed = false; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WH_KEYBOARD_LL /// </summary> private const int WH_KEYBOARD_LL = 13; /// <summary> /// WM_KEYDOWN /// </summary> private const int WM_KEYDOWN = 0x0100; /// <summary> /// WM_KEYUP /// </summary> private const int WM_KEYUP = 0x0101; /// <summary> /// WM_SYSKEYDOWN /// </summary> private const int WM_SYSKEYDOWN = 0x0104; /// <summary> /// WM_SYSKEYUP /// </summary> private const int WM_SYSKEYUP = 0x0105; /// <summary> /// VK_LWIN /// </summary> private const int VK_LWIN = 0x5B; /// <summary> /// VK_RWIN /// </summary> private const int VK_RWIN = 0x5C; /// <summary> /// VK_C /// </summary> private const int VK_C = 0x43; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 후킹 설정하기 - SetHook(lowLevelKeyboardProcedure) /// <summary> /// 후킹 설정하기 /// </summary> /// <param name="lowLevelKeyboardProcedure">저수준 키보드 프로시저</param> /// <returns>처리 결과</returns> private static IntPtr SetHook(LowLevelKeyboardProcedure lowLevelKeyboardProcedure) { using(Process process = Process.GetCurrentProcess()) { using(ProcessModule processModule = process.MainModule) { nint moduleHandle = GetModuleHandle(processModule.ModuleName); return SetWindowsHookEx(WH_KEYBOARD_LL, lowLevelKeyboardProcedure, moduleHandle, 0); } } } #endregion #region 후킹 콜백 처리하기 - HookCallback(code, wordParameter, longParameter) /// <summary> /// 후킹 콜백 처리하기 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private static IntPtr HookCallback(int code, IntPtr wordParameter, IntPtr longParameter) { if(code >= 0) { int vkCode = Marshal.ReadInt32(longParameter); if(wordParameter == (IntPtr)WM_KEYDOWN || wordParameter == (IntPtr)WM_SYSKEYDOWN) { if(vkCode == VK_LWIN || vkCode == VK_RWIN) { _isWINKeyPressed = true; } else if(vkCode == VK_C && _isWINKeyPressed) { Process.Start("notepad.exe"); return (IntPtr)1; // 이 주요 이벤트의 추가 처리를 방지한다. } } else if(wordParameter == (IntPtr)WM_KEYUP || wordParameter == (IntPtr)WM_SYSKEYUP) { if(vkCode == VK_LWIN || vkCode == VK_RWIN) { _isWINKeyPressed = false; } } } return CallNextHookEx(_hookID, code, wordParameter, longParameter); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStartup(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); _hookID = SetHook(_lowLevelKeyboardProcedure); } #endregion #region 종료시 처리하기 - OnExit(e) /// <summary> /// 종료시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnExit(ExitEventArgs e) { UnhookWindowsHookEx(_hookID); base.OnExit(e); } #endregion } |