private static void KeyDown(KBLLHOOKSTRUCT keyInfo) { Keys key = (Keys)keyInfo.vkCode; #region Strart & Stop Recording //Start recording if (key == Keys.F1 && Keys.Control == Control.ModifierKeys && !isRecoding) { notifyIcon.ShowBalloonTip(1000, "Recording", "Mouse and keyboard input is now being recorded", ToolTipIcon.None); isRecoding = true; recordFile = new RecordFile(Application.StartupPath + "\\Profile1.rec", true); recordFile.EndOffStream += new EventHandler(StopReplay); startTime = keyInfo.time; return; } //Stop recording else if (key == Keys.F1 && Keys.Control == Control.ModifierKeys && isRecoding) { notifyIcon.ShowBalloonTip(1000, "Stoped recording", "Input recording has now stopped", ToolTipIcon.None); isRecoding = false; startTime = 0; recordFile.WriteLControlUp(); recordFile.StopWrite(); return; } #endregion #region Strart & Stop Replay if (!isRecoding) { //Start replay if (key == Keys.F1 && Keys.Shift == Control.ModifierKeys && !isReplaying) { notifyIcon.ShowBalloonTip(1000, "Replaying", "Mouse and keyboard input is now being replayed", ToolTipIcon.None); isReplaying = true; recordFile.StartRead(); currentInstruction = recordFile.ReadNextInstruction(); timerReplay.Start(); return; } //Stop replay else if (key == Keys.F1 && Keys.Shift == Control.ModifierKeys && isReplaying) { StopReplay(new object(), new EventArgs()); return; } } #endregion if (isRecoding) { RecordInstruction instruction = new RecordInstruction(); instruction.instructionType = 0; instruction.timing = keyInfo.time - startTime; instruction.keyCode = keyInfo.vkCode; instruction.scanCode = keyInfo.scanCode; recordFile.WriteInstruction(instruction); } }
private static void KeyUp(KBLLHOOKSTRUCT keyInfo) { Keys key = (Keys)keyInfo.vkCode; if (isRecoding) { RecordInstruction instruction = new RecordInstruction(); instruction.instructionType = 1; instruction.timing = keyInfo.time - startTime; instruction.keyCode = keyInfo.vkCode; instruction.scanCode = keyInfo.scanCode; recordFile.WriteInstruction(instruction); } }
public static int LowLevelKeyboardHookProc(int nCode, int wParam, IntPtr lParam) { try { KBLLHOOKSTRUCT KeyboardHookStruct = (KBLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBLLHOOKSTRUCT)); if (nCode < 0) { return(CallNextHookEx(0, nCode, wParam, lParam)); } else { if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) { if (KeyboardHookStruct.scanCode != lastScanCode) // иначе запавшая клавиша тут тоже будет считаться работой пользователя { lastKeyDownTime = DateTime.Now.Ticks; lastScanCode = KeyboardHookStruct.scanCode; } } if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) { if (KeyboardHookProcEvent != null) { KeyboardHookProcEvent(KeyboardHookStruct.scanCode); } lastKeyDownTime = DateTime.Now.Ticks; lastScanCode = -1; } return(CallNextHookEx(0, nCode, wParam, lParam)); } } catch { try { return(CallNextHookEx(0, nCode, wParam, lParam)); } catch { return(0); } } }
//The funtion called when keyboard event occurs private static IntPtr KeyBoardProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { KBLLHOOKSTRUCT hookStruct = (KBLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBLLHOOKSTRUCT));// gets the structure pointed at by lParam if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN) { KeyDown(hookStruct); } if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP) { KeyUp(hookStruct); } } return(CallNextHookEx(_keyHookID, nCode, wParam, lParam)); }
/// <summary> /// Hook process messages. /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> static IntPtr HookProcFunction(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode == 0) { KBLLHOOKSTRUCT pKBLLHOOKSTRUCT = (KBLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBLLHOOKSTRUCT)); switch (wParam.ToInt32()) { case WM_KEYDOWN: KeyboardDown?.Invoke(null, new KeyEventArgs((Keys)pKBLLHOOKSTRUCT.vkCode)); //MessageBox.Show("OKAY, " + pKBLLHOOKSTRUCT.vkCode); // <--- DEBUG break; case WM_KEYUP: KeyboardUp?.Invoke(null, new KeyEventArgs((Keys)pKBLLHOOKSTRUCT.vkCode)); break; default: break; } } return(API.CallNextHookEx(hHook, nCode, wParam, lParam)); }
public static extern IntPtr CallNextHookEx(IntPtr hookId, int nCode, IntPtr wParam, [In] KBLLHOOKSTRUCT lParam);