public int HookProc(int code, int wParam, ref WinAPI.KeyboardHookStruct lParam) { // we only look for keyups and downs if (code >= 0 && (wParam == WinAPI.WM_KEYDOWN || wParam == WinAPI.WM_SYSKEYDOWN || wParam == WinAPI.WM_KEYUP || wParam == WinAPI.WM_SYSKEYUP)) { // create out async handler and call it. if we needed to return "handled" chage this to ".Invoke()" KeyboardHookEventArgs kea = new KeyboardHookEventArgs((Keys)lParam.vkCode); m_hookedAsync.BeginInvoke(wParam, kea, null, null); } return WinAPI.CallNextHookEx(m_hook, code, wParam, ref lParam); }
/// <summary> /// Entry point for our actuall Keyboard hook. Check the keys and call User event if neccessary. /// </summary> /// <param name="code">key code</param> /// <param name="kea">EventArgs holding modifiers</param> public void KeyCallback(int code, KeyboardHookEventArgs kea) { // key and modifers match? if (!m_hookedKeys.ContainsKey(kea.Key) || m_hookedKeys[kea.Key] != kea.Modifiers) { return; } // call user events if ((code == WinAPI.WM_KEYDOWN || code == WinAPI.WM_SYSKEYDOWN) && KeyDown != null) { KeyDown(this, kea); } else if ((code == WinAPI.WM_KEYUP || code == WinAPI.WM_SYSKEYUP) && KeyUp != null) { KeyUp(this, kea); } }
/// <summary> /// A hotkey keyboard event occured, e.g. "Ctrl-Alt-C" /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Hotkey_KeyDown(object sender, KeyboardHookEventArgs e) { // avoid multiple keypresses being sent if (Monitor.TryEnter(m_sendingKeys) == true) { try { // get keyboard sender KeyboardSender keysend = new KeyboardSender(this.Config.AutoLogin.WindowTitle, this.Config.AutoLogin.ProcessName, this.Config.AutoLogin.WindowTitleRegex); // get the script and execute it string script = (string.IsNullOrEmpty(this.Config.AutoLogin.AdvancedScript) == false ? this.Config.AutoLogin.AdvancedScript : "{CODE}{ENTER 4000}"); // send the whole script keysend.SendKeys(this, script, Authenticator.CurrentCode); // mark event as handled e.Handled = true; } finally { Monitor.Exit(m_sendingKeys); } } }