예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
                }
            }
        }
예제 #4
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));
        }
예제 #5
0
        /// <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));
        }
예제 #6
0
 public static extern IntPtr CallNextHookEx(IntPtr hookId, int nCode, IntPtr wParam, [In] KBLLHOOKSTRUCT lParam);