PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Extra/HookSample2/HookSample2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 294 lines | 224 code | 53 blank | 17 comment | 11 complexity | c47b9500777e84abde7ccfca848efea1 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module HookSample2;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm : max, min;
  9. import std.conv;
  10. import std.exception;
  11. import std.math;
  12. import std.range;
  13. import std.stdio;
  14. import std.string;
  15. import std.utf : count, toUTFz;
  16. auto toUTF16z(S)(S s)
  17. {
  18. return toUTFz!(const(wchar)*)(s);
  19. }
  20. pragma(lib, "gdi32.lib");
  21. import core.sys.windows.winbase;
  22. import core.sys.windows.windef;
  23. import core.sys.windows.winuser;
  24. import core.sys.windows.wingdi;
  25. alias std.algorithm.min min;
  26. alias std.algorithm.max max;
  27. // http://nehe.gamedev.net/article/msdn_virtualkey_codes/15009/
  28. extern (Windows)
  29. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  30. {
  31. int result;
  32. try
  33. {
  34. Runtime.initialize();
  35. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  36. Runtime.terminate();
  37. }
  38. catch (Throwable o)
  39. {
  40. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  41. result = 0;
  42. }
  43. return result;
  44. }
  45. __gshared HHOOK keyHook;
  46. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  47. {
  48. //~ keyHook = enforce(SetWindowsHookEx(WH_KEYBOARD,
  49. //~ &KeyboardProc,
  50. //~ cast(HINSTANCE)null, // DLL handle, must be null for current thread handlers
  51. //~ GetCurrentThreadId())); // Thread ID, or 0 if used for all desktop threads
  52. keyHook = enforce(SetWindowsHookEx(WH_KEYBOARD_LL,
  53. &LowLevelKeyboardProc,
  54. cast(HINSTANCE)null, // DLL handle, must be null for current thread handlers
  55. 0)); // Thread ID, or 0 if used for all desktop threads. Must be 0 for LowLevel hook.
  56. string appName = "KeyView1";
  57. HWND hwnd;
  58. MSG msg;
  59. WNDCLASS wndclass;
  60. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  61. wndclass.lpfnWndProc = &WndProc;
  62. wndclass.cbClsExtra = 0;
  63. wndclass.cbWndExtra = 0;
  64. wndclass.hInstance = hInstance;
  65. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  66. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  67. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  68. wndclass.lpszMenuName = NULL;
  69. wndclass.lpszClassName = appName.toUTF16z;
  70. if (!RegisterClass(&wndclass))
  71. {
  72. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  73. return 0;
  74. }
  75. hwnd = CreateWindow(appName.toUTF16z, // window class name
  76. "Keyboard Message Viewer #1", // window caption
  77. WS_OVERLAPPEDWINDOW, // window style
  78. CW_USEDEFAULT, // initial x position
  79. CW_USEDEFAULT, // initial y position
  80. CW_USEDEFAULT, // initial x size
  81. CW_USEDEFAULT, // initial y size
  82. NULL, // parent window handle
  83. NULL, // window menu handle
  84. hInstance, // program instance handle
  85. NULL); // creation parameters
  86. ShowWindow(hwnd, iCmdShow);
  87. UpdateWindow(hwnd);
  88. while (GetMessage(&msg, NULL, 0, 0))
  89. {
  90. TranslateMessage(&msg);
  91. DispatchMessage(&msg);
  92. }
  93. return msg.wParam;
  94. }
  95. extern(Windows)
  96. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  97. {
  98. scope (failure) assert(0);
  99. static int cLinesMax, cLines;
  100. static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
  101. static MSG[] msgArr;
  102. static int msgCount;
  103. static RECT rectScroll;
  104. enum szTop = "Message Key Char Repeat Scan Ext ALT Prev Tran";
  105. enum szUnd = "_______ ___ ____ ______ ____ ___ ___ ____ ____";
  106. enum szFormat = ["%-13s %3s %-15s%1s%6s %4s %3s %3s %4s %4s",
  107. "%-13s 0x%04X%1s%s %6s %4s %3s %3s %4s %4s"];
  108. enum szYes = "Yes";
  109. enum szNo = "No";
  110. enum szDown = "Down";
  111. enum szUp = "Up";
  112. enum szMessage =
  113. [
  114. "WM_KEYDOWN", "WM_KEYUP",
  115. "WM_CHAR", "WM_DEADCHAR",
  116. "WM_SYSKEYDOWN", "WM_SYSKEYUP",
  117. "WM_SYSCHAR", "WM_SYSDEADCHAR"
  118. ];
  119. HDC hdc;
  120. int iType;
  121. PAINTSTRUCT ps;
  122. string szBuffer;
  123. wchar[32] szKeyName;
  124. wchar[] keyName;
  125. int keyLength;
  126. TEXTMETRIC tm;
  127. switch (message)
  128. {
  129. case WM_CREATE:
  130. case WM_DISPLAYCHANGE:
  131. {
  132. // Get maximum size of client area
  133. cxClientMax = GetSystemMetrics(SM_CXMAXIMIZED);
  134. cyClientMax = GetSystemMetrics(SM_CYMAXIMIZED);
  135. hdc = GetDC(hwnd);
  136. scope(exit) ReleaseDC(hwnd, hdc);
  137. // Get character size for fixed-pitch font
  138. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  139. GetTextMetrics(hdc, &tm);
  140. cxChar = tm.tmAveCharWidth;
  141. cyChar = tm.tmHeight;
  142. cLinesMax = cyClientMax / cyChar;
  143. cLines = 0;
  144. goto case WM_SIZE;
  145. }
  146. case WM_SIZE:
  147. {
  148. if (message == WM_SIZE)
  149. {
  150. cxClient = LOWORD(lParam);
  151. cyClient = HIWORD(lParam);
  152. }
  153. // Calculate scrolling rectangle
  154. rectScroll.left = 0;
  155. rectScroll.right = cxClient;
  156. rectScroll.top = cyChar;
  157. rectScroll.bottom = cyChar * (cyClient / cyChar);
  158. InvalidateRect(hwnd, NULL, TRUE);
  159. return 0;
  160. }
  161. case WM_KEYDOWN:
  162. case WM_KEYUP:
  163. case WM_CHAR:
  164. case WM_DEADCHAR:
  165. case WM_SYSKEYDOWN:
  166. case WM_SYSKEYUP:
  167. case WM_SYSCHAR:
  168. case WM_SYSDEADCHAR:
  169. {
  170. msgArr ~= MSG(hwnd, message, wParam, lParam);
  171. cLines = min(cLines + 1, cLinesMax);
  172. // Scroll up
  173. ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
  174. break;
  175. }
  176. case WM_PAINT:
  177. {
  178. hdc = BeginPaint(hwnd, &ps);
  179. scope(exit) EndPaint(hwnd, &ps);
  180. SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  181. SetBkMode(hdc, TRANSPARENT);
  182. TextOut(hdc, 0, 0, szTop.toUTF16z, szTop.count);
  183. TextOut(hdc, 0, 0, szUnd.toUTF16z, szUnd.count);
  184. foreach (index, myMsg; lockstep(iota(0, min(cLines, cyClient / cyChar - 1)), retro(msgArr)))
  185. {
  186. iType = myMsg.message == WM_CHAR ||
  187. myMsg.message == WM_SYSCHAR ||
  188. myMsg.message == WM_DEADCHAR ||
  189. myMsg.message == WM_SYSDEADCHAR;
  190. keyLength = GetKeyNameText(myMsg.lParam, szKeyName.ptr, szKeyName.length);
  191. keyName = szKeyName[0..keyLength];
  192. szBuffer = format(szFormat[iType],
  193. szMessage[myMsg.message - WM_KEYFIRST],
  194. myMsg.wParam,
  195. (iType ? "" : keyName.dup),
  196. (iType ? to!string(cast(char*)&myMsg.wParam) : ""),
  197. LOWORD(myMsg.lParam),
  198. HIWORD(myMsg.lParam) & 0xFF,
  199. (0x01000000 & myMsg.lParam ? szYes : szNo),
  200. (0x20000000 & myMsg.lParam ? szYes : szNo),
  201. (0x40000000 & myMsg.lParam ? szDown : szUp),
  202. (0x80000000 & myMsg.lParam ? szUp : szDown)
  203. );
  204. TextOut(hdc, 0, (cyClient / cyChar - 1 - index) * cyChar, szBuffer.toUTF16z, szBuffer.count);
  205. }
  206. return 0;
  207. }
  208. case WM_DESTROY:
  209. {
  210. PostQuitMessage(0);
  211. return 0;
  212. }
  213. default:
  214. }
  215. return DefWindowProc(hwnd, message, wParam, lParam);
  216. }
  217. WORD[WORD] keyMap;
  218. shared static this()
  219. {
  220. // A => B
  221. keyMap[0x41] = 0x42;
  222. }
  223. extern(Windows)
  224. LRESULT LowLevelKeyboardProc(int code, WPARAM wParam, LPARAM lParam)
  225. {
  226. auto kbs = cast(KBDLLHOOKSTRUCT*)lParam;
  227. // generate a new message
  228. if (!(kbs.flags & LLKHF_INJECTED))
  229. {
  230. INPUT input;
  231. input.type = INPUT_KEYBOARD;
  232. input.ki.dwFlags = (wParam == WM_KEYDOWN) ? 0 : KEYEVENTF_KEYUP;
  233. stderr.writefln("kbs: %s", *kbs);
  234. //~ stderr.writefln("scancode: %s alt: %s, vk: %s", kbs.scanCode, kbs.flags & LLKHF_ALTDOWN, kbs.vkCode);
  235. // replace key
  236. input.ki.wVk = keyMap.get(cast(WORD)kbs.vkCode, cast(WORD)kbs.vkCode);
  237. SendInput(1, &input, INPUT.sizeof);
  238. return -1;
  239. }
  240. return CallNextHookEx(keyHook, code, wParam, lParam);
  241. }