bool OnKeyHook(int code, WM wParam, KBDLLHOOKSTRUCT lParam, Hooker hooker) { if (lParam.vkCode == 44 && wParam == WM.KEYUP) { takeScreenshot(); } return false; }
void UpdateSysKeyStates(WM wParam, ref KBDLLHOOKSTRUCT lParam) { switch (wParam) { case WM.KEYDOWN: case WM.SYSKEYDOWN: if (lParam.vkCode == 160 || lParam.vkCode == 161) onShift = true; if (lParam.vkCode == 162 || lParam.vkCode == 163) onCtrl = true; if (lParam.vkCode == 164 || lParam.vkCode == 165) onAlt = true; break; case WM.KEYUP: case WM.SYSKEYUP: if (lParam.vkCode == 160 || lParam.vkCode == 161) onShift = false; if (lParam.vkCode == 162 || lParam.vkCode == 163) onCtrl = false; if (lParam.vkCode == 164 || lParam.vkCode == 165) onAlt = false; break; } }
static extern int CallNextHookEx(int hook, int code, WM wParam, ref KBDLLHOOKSTRUCT lParam);
/// <summary> /// キーのフック処理 /// </summary> /// <param name="code"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns>ignore CallNextHookEx() or not</returns> int OnKeyLLHook(int code, WM wParam, ref KBDLLHOOKSTRUCT lParam) { UpdateSysKeyStates(wParam, ref lParam); bool ignore = false; if (OnKeyHook != null) { ignore = OnKeyHook(code, wParam, lParam, this); } return ignore ? 1 : CallNextHookEx(0, code, wParam, ref lParam); }